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.
Code:
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;
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(h_b);
SPI1_Write(l_b);
Chip_Select = 1;
num=num+.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;
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(h_b);
SPI1_Write(l_b);
Chip_Select = 1;
Chip_Select = 1;
}
}
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire