Connect PIC16F877A to DAC chip MCP4921

Digital and analog are an integral part of electronics. Most devices have both ADC and DAC and are used whenever a signal needs to be converted from analog to digital or digital to analog. Moreover, real-world signals such as sound and light are also analog in nature, so whenever these real-world signals have to be used, the digital signal has to be converted to analog, such as producing sound using a speaker or controlling a light source.
Another type of DAC is a pulse width modulator (PWM). A PWM takes in a digital word and generates a digital pulse with a variable pulse width. When this signal is passed through a filter, the result will be purely analog. The signal in an analog signal can have many types of data.
In this article, we will connect Microchip’s PIC16F877 microcontroller to ADAC MCP4921 to achieve digital-to-analog conversion. In this article, we will convert digital signals to analog signals and display the input digital value and output analog value on a 16×2 LCD. It will provide 1V, 2V, 3V, 4V, and 5V as the final analog output.
DACs can be used in many applications such as motor control, controlling brightness of LED lights, frequency amplifiers, video encoders, data acquisition systems, etc.
MCP4921 DAC (Digital-to-Analog Converter)
The MCP4921 is a 12-bit DAC, so the MCP4921 will provide 12-bit output resolution. DAC resolution means the number of digital bits that can be converted into an analog signal. We can get the value from the formula. For 12 bits, it is equal to 4096. This means that a DAC with a 12-bit resolution can produce 4096 different outputs.
By using this value, a single analog step voltage can be easily calculated. In order to calculate the step size, a reference voltage is needed. Since the logic voltage of this device is 5V, the step voltage is 5/4095 (because the starting point of the digital is not 1, it is 0, so it is 4096-1), which is 0.00122100122 millivolts. Therefore, changing 1 bit will change the analog output by 0.00122100122.
So that’s it for the conversion part. The MCP4921 is an 8-pin IC. The pinout diagram and description can be found below.

The MCP4921 IC communicates with the microcontroller through SPI communication. For SPI communication, the device must be a master, which submits data or commands to the external device connected as a slave. In a SPI communication system, multiple slave devices can be connected with a single master.
To submit data and commands, it is important to understand the command register.
The following figure shows the command register,
The command register is a 16-bit register. Bits 15 to 12 are used for configuration commands. The figure above clearly shows the data input and configuration. The data determined by bits D11 to D0 of the register together is 0011. It is necessary to submit 16-bit data 0011 xxxx xxxx xxxx, where the first 4 bits of the MSB are configuration and the rest are LSB. It will be clearer by looking at the write command timing diagram.

According to the timing diagram and datasheet, the CS pin is low during the entire command write to the MCP4921. Now it is time to connect the device with hardware and write the code.
Required Components
● MCP4921
● PIC16F877A microcontroller
● 20 MHz crystal
● Display 16×2 character LCD.
● Multimeter
● Breadboard
● 5V power supply
Connection Schematic
Given below is the circuit diagram for interfacing DAC4921 with PIC microcontroller:

The circuit is made in a breadboard.

Code Description
The complete code for converting digital signal to analog signal using PIC16F877A is provided at the end of this article. As always, we first need to set the configuration bits in the PIC microcontroller.
The following lines of code are used to include the LCD and SPI header files and also declare the XTAL frequency and CS pin connections for the DAC.
SPI_Initialize_Master() is slightly modified according to the other configurations required for this article . The SSPSTAT register is configured to sample the input data at the end of the data output time and the SPI clock is configured to transition from the active clock state mode to the idle clock state mode for transmission. Everything else is the same.
Likewise, some modifications have been made to the SPI_Write() function. The data transfer will take place after clearing the buffer to ensure flawless data transfer over SPI.
The important part of this program is the MCP4921 driver. It is a bit difficult because it combines the commands and digital data together to provide a full 16-bit data over the SPI. However, the logic is clearly shown in the code comments.
In the main function, a “for loop” is used to create digital data for 1V, 2V, 3V, 4V, and 5V outputs. The digital value will be calculated based on the output voltage / 0.0012210012210012 millivolts.
Testing the PIC’s digital-to-analog conversion
Use a multimeter to test the built circuit. In the following figure, the output voltage and digital data are displayed on the LCD. The multimeter shows a close reading.

Code
Here is the complete code used in this article:
main












