SivaHari
Newbie level 3
Hai,
I recently interfaced the DAC mcp 4921 with pic16f877a microcontroller using SPI. THE Process is working fine, when i given the input
directly to DAC (ie : value as 600 without using forloop). but if i given the input using for loop and adding each time value 100 using macros the output suddenly increasing...
I found little bit of error from that... during loop running time the clock generating normally eight to nine clock and then nine to ten clock. due to this problem the input latching incorrectly in DAC register. here i attach my program, kindly someone replay me...
Thank you..
I recently interfaced the DAC mcp 4921 with pic16f877a microcontroller using SPI. THE Process is working fine, when i given the input
directly to DAC (ie : value as 600 without using forloop). but if i given the input using for loop and adding each time value 100 using macros the output suddenly increasing...
I found little bit of error from that... during loop running time the clock generating normally eight to nine clock and then nine to ten clock. due to this problem the input latching incorrectly in DAC register. here i attach my program, kindly someone replay me...
Thank you..
Code:
#include <stdio.h>
#include <stdlib.h>
#include <htc.h>
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_OFF & CP_OFF);
#define _XTAL_FREQ 20000000
#define CS RD7
#define LDAC RD3
#define TEN_VOLT_VALUE 100.00
void SPI_init();
void SPI_single_data_send(unsigned char);
void delay(unsigned int);
void Start_RPM(unsigned int rpm);
void DAC_RST();
unsigned int TEMP,j,b;
unsigned int value = 2500;
char higher_byte,lower_byte;
char DAC_buff[2];
//const int TEN_VOLT_VALUE = 25;
int main()
{
TRISD = 0x00;
TRISC = 0x00;
CS = 1;
SPI_init();
//CS = 1;
delay(100);
//Start_RPM(100);
while(1)
{
Start_RPM(600);
// Start_RPM();
//for(VALUE=0;VALUE<4095;VALUE+=100)
//{
//TEMP = VALUE/1.098;
//TEMP = (TEMP >> 8);
/* TEMP = value >> 8;
higher_byte |= TEMP;
higher_byte &= 0x0F;
higher_byte |= 0x30;
lower_byte = ((value)&0xff);
DAC_buff[0] = higher_byte;
DAC_buff[1] = lower_byte;
// LDAC = 1;
delay(50);
CS = 0;
delay(25);
for(j=0;j<2;j++)
{
SPI_single_data_send(DAC_buff[j]);
}
delay(50);
CS = 1;
delay(50);
// LDAC = 0;delay(100);
// LDAC = 1;
delay(65000);
//}*/
// NOP();
;
}
}
void SPI_init()
{
SSPEN = 0;
SMP = 0;
CKP = 0;
CKE = 0;
SSPM0 = 0;
SSPM1 = 0;
SSPM2 = 0;
SSPM3 = 0;
SSPEN = 1;
}
void SPI_single_data_send(unsigned char x)
{
SSPBUF = x;
while(!SSPIF);
SSPIF = 0;
}
void delay(unsigned int z)
{
unsigned int i;
for(i=0;i<=z;i++)
{
NOP();
}
}
void Start_RPM(unsigned int rpm)
{
unsigned int Dac_var,num = 0;
int five_volt,ten_volt=0;
float DAC_volt = 0;
// five_volt = (rpm % 10);
ten_volt = (rpm / 100);
DAC_volt = 0;
higher_byte = 0;
lower_byte = 0;
// DAC_volt = ten_volt;
for(Dac_var = 1;Dac_var <= ten_volt; Dac_var++)
{
DAC_volt += TEN_VOLT_VALUE;
num = (int)DAC_volt;
// num = rpm;
if(num >3688)
{
num = 3688;
}
TEMP = (num >> 8);
higher_byte |= TEMP;
higher_byte &= 0x0F;
higher_byte |= 0x30;
lower_byte = ((num >> 0)&0xff);
DAC_buff[0] = higher_byte;
DAC_buff[1] = lower_byte;
LDAC = 1;
delay(50);
CS = 0;
// delay(50); //delay(300);
for(j=0;j<2;j++)
{
SPI_single_data_send(DAC_buff[j]);
}
delay(50); //delay(100);
CS = 1;
delay(50);
LDAC = 0;
delay(100);
LDAC = 1;
delay(65000);
delay(65000);
delay(65000);
delay(65000);
delay(65000);
// delay(65000);
// DAC_RST();
}
}
Last edited by a moderator: