Hello,
I am trying to build a voltage sweep, 0 to 5V using a PIC 18f4620 and the TLC5615 DAC. I checked over and over and I don't seem to be able to find the error in the code. I have tried to change it into bit shift, one by one but it actually gets even worse. I should get just a single sweep, instead I get this:
:
The code is the following:
#include<p18F4620.h>
#include<delays.h>
#include<math.h>
#include<spi.h>
#include<timers.h>
void DAC(unsigned int daconv); //Voltage function init.
void main(void)
{
char i;
OSCCON=0x66;
OSCTUNE=0x00;
Delay10KTCYx(100);
TRISB=0;
TRISC=0X10; //set RC4 bit as input ,others output
SSPSTAT=0X40; //CKE=1 falling edge send data(according to TCL5615)
SSPCON1=0X20; //SSPEN=1 CKE=0SPI main control mode Fosc/4
for (i=0;i<1024;i++)
{
DAC(i);
//produce voltage
}
}
//*********************************************************************************************************
//**************DIGITAL TO ANALOG CONVERSION FUNCTION***************************************
void DAC(unsigned int daconv)
{
int value1,value2;
char temp; //define a temp reg
PORTCbits.RC3=0; //SCLK low
Delay10TCYx(10);
PORTCbits.RC2=1; //cs disable
Delay10TCYx(10);
PORTCbits.RC2=0; //cs enable.
Delay10TCYx(10);
value1=daconv>>6; //shift bits
SSPBUF=value1; //send high nibble.
while(!SSPSTATbits.BF); //wait for sending finished.
PIR1bits.SSPIF=0X00;
temp=SSPBUF; //clear receive register
value2=daconv<<4;
SSPBUF=value2; //send the low 6 bits.
while(!SSPSTATbits.BF); //wait for sending finished.
PIR1bits.SSPIF=0X00;
PORTC=0X04; //cs disable through RC2
}
It is my first time with DAC's.
I really hope someone can help me with this one.
Thank you in advance.
Mau