MCP4921 12 bit DAC interfacing with PIC16F877 microcontroller via SPI Connectivity

In any real time project, MCP4921 12 bit DAC interfacing with PIC16F877 microcontroller via SPI Connectivity is very essential. Because in real world most of the input sensors and output devices are analog. So if we want to work with those we need to know the basic interfacing of those sensors and output devices with microcontroller. Here I give you tutorial project on “MCP4921 12 bit DAC interfacing with PIC16F877 microcontroller via SPI Connectivity”.

Project description:

 In this tutorial project we will see how we generate triangular Wave using MCP4921 12 bit DAC.  As we know that MCP4921 is a 12 bit DAC. But the control word we have to send 16 bit including data. In below we see the bit structure for MCP4921.

 MCP4921 12 bit DAC interfacing with PIC16F877 microcontroller via SPI Connectivity

From above picture it is clear that bit 12-15 is used for control word and bit 0 to bit 11 are used for send 12 bit data.

Now question is how we maintain that format to send every 12 bit data along with 4 bit control word at MSB?

Let take an example if we want to write data via DCAA with output gain 1X and power down control bit.

So for that our control word will be

Bit 15 = 0 (write to DACA )

Bit 14 = 0 (Un buffered)

Bit 13 = 1(Output Gain)

Bit 12 = 1 (Output power down control)

So control bit will be 0011 = 3 .

So if you want to write 12 bit data like 001000100010. Then you have to send 16 bit data 0011001000100010 (0x32 and 0x22).

So we can get maximum 4096 different value by 12 bit data format. If we use 5V reference voltage then we will get ( 5000/4096) .001220703125 mile volt per step.

So for sending 3 volt at output we have to send  2.5/ .001220703125 = 2048 (010000000000) along with 3(0011) at MSB. So the final 16 bit data will be 0011010000000000 ( 0x34 and 0x00).

Now question is how we will do that in c programming?

For that see my complete C program for MCP4921 12 bit DAC interfacing with PIC16F877 microcontroller via SPI Connectivity for generating triangular wave and know the tricks.


Embedded C Code
// Name : MCP4921 12 bit DAC interfacing with PIC16F877 microcontroller via SPI Connectivity

// Author : Subham Dutta
// Date : 03-09-14
// Website : www.nbcafe.in

// DAC module connections
sbit Chip_Select at RC0_bit;
sbit Chip_Select_Direction at TRISC0_bit;
void main() {
int value,h_b,l_b;
float num=0;
while(1)
{
for(num=0;num<=5;)
{
SPI1_Init();
//SPI
Chip_Select_Direction = 0; // Set CS# pin as Output
value=num/0.001220703125;
value=value|3 << 12; h_b=(value>>8)& 255;
l_b=value & 255;

Chip_Select = 0;
SPI1_Write(h_b);

SPI1_Write(l_b);

Chip_Select = 1;
num=num+.1;

}
if(num==5)
{
for(num=5;num>=0;)
{
num=num-.1;
SPI1_Init();
//SPI
Chip_Select_Direction = 0; // Set CS# pin as Output
value=num/0.001220703125;
value=value|3 << 12; h_b=(value>>8)& 255;
l_b=value & 255;

Chip_Select = 0;
SPI1_Write(h_b);

SPI1_Write(l_b);
Chip_Select = 1;

}
}
}
}

See below the Proteus simulation Circuit diagram of project MCP4921 12 bit DAC interfacing with PIC16F877 microcontroller via SPI Connectivity

MCP4921 12 bit DAC interfacing with PIC16F877 microcontroller via SPI Connectivity