This project demonstrates how to interface the MCP4921 12-bit Digital-to-Analog Converter (DAC) with an STM32F103C6 microcontroller via SPI communication.
The MCP4921 is designed for high accuracy and low noise performance, making it suitable for industrial applications such as sensor calibration, motor control, and signal generation. By integrating it with STM32, the system can generate precise analog signals from digital inputs, which can be monitored or used for control in real-time.
Overview of the MCP4921 DAC
The MCP4921 is a 12-bit, single-channel DAC that offers excellent accuracy:
- Typical Differential Nonlinearity (DNL): ±0.2 LSB
- Typical Integral Nonlinearity (INL): ±2 LSB
- Operating voltage: 2.7V to 5.5V
- Rail-to-rail output for full voltage range utilization
- SPI interface supporting clock speeds up to 20 MHz
- Selectable gain: 1x or 2x
- Fast settling time: 4.5 µs
- External reference voltage (VREF) input for precise output control
Applications
- The MCP4921 is versatile and ideal for:
- Set Point or Offset Trimming
- Sensor Calibration (pressure, temperature, etc.)
- Motor Control (speed/position adjustment)
- Portable/Battery-Powered Devices
- MCU Selection: STM32F103C6 (8 MHz clock)
- SPI1 Configuration:
-Mode: Transmit Only Master
-Prescaler: 4 (4.0 Mbps)
-CPOL: Low, CPHA: 1 Edge
- GPIO Configuration:
-PA4 → Output (Chip Select)
-PA8 → Output (LDAC, optional)
- Generate initialization code in STM32CubeIDE.
Key Functions
- DAC Initialization:
-DAC_MCP49x1_Init(&hdac, MCP4921, &hspi1, GPIOA, GPIO_PIN_4, NULL, 0);
-The MCP4921 DAC is initialized using the HAL SPI interface. This prepares the DAC for output and sets up the GPIO for chip select.
- Writing Values to DAC:
-DAC_MCP49x1_Output(&hdac, 0); // Output 0V (or minimum voltage)
-DAC_MCP49x1_Output(&hdac, 4095); // Output maximum voltage
-Use this function to send digital values to the DAC, which are converted into analog output voltages.
- Main Loop (Square Wave Example):
-
The main loop continuously outputs a square wave by alternating between the minimum and maximum DAC values with a delay of 100 ms.
while (1)
{
DAC_MCP49x1_Output(&hdac, 0);
HAL_Delay(100);
DAC_MCP49x1_Output(&hdac, 4095);
HAL_Delay(100);
}
Components
- STM32F103C6 microcontroller
- MCP4921 DAC
- MCP1525 Voltage Reference
- Oscilloscope
Connections
- SPI Pins: PA5 → SCLK, PA6 → MISO, PA7 → MOSI, PA4 → CS
- Connect VREF of MCP4921 to MCP1525
- Simulation Steps
Load the generated.hex file into STM32 in Proteus.
Observe the DAC output as a 0V → 2.5V square wave on the oscilloscope.
If you have any questions or suggestions don't hesitate to leave a comment below
Comments