Jump to content
IGNORED

Where is the mistake in the code of configuring DAC?


Recommended Posts

I am trying to configure DAC in master mode using DSPIC33FJ16MC304 to interface DAC IC MCP4822(datasheet). However, the SPITBF is always 1. I have tried my best but could not understand the reason. Although in documentation, it is wrote that when data is written to SPIBUF, SPITBF is set by the hardware. And when data is transferred into to the shift register, SPITBF is cleared by the hardware. I am new user of DSPIC33. Can anybody figure out where is the mistake in spi_ini() function. Because same configurations of DAC() function I have used with TMS320F28335 and worked fine. But I am stucked here with DSPIC33.

 

#include "P33fj16mc304.h"  

void DAC_ini();  
void DAC(unsigned int d1, unsigned int d2);  
_FOSCSEL(IESO_ON  & FNOSC_FRCPLL);  
_FOSC(POSCMD_NONE & OSCIOFNC_OFF & FCKSM_CSECME);  

unsigned int b=0,c=0, d=0, d1=0;  
int k=0;  


int main(void)   
{  

_PLLPRE = 0;  
_PLLDIV = 41;  
_PLLPOST = 0;  
TRISBbits.TRISB0=0;  
TRISBbits.TRISB1=0;  
TRISAbits.TRISA8=0;  
TRISAbits.TRISA9=0;  
 DAC_ini();    
 while(1)  
{  
    b=b+1;    
    c=c+3;    
    DAC(b,c);  

}  
return(0);  
}  

void DAC_ini()  
{  
SPI1CON1bits.SPRE=0b100;    // Secondary clock pre scalar 4:1  
SPI1CON1bits.PPRE=0b11;     // Primary clock pre scaler   1:1  
SPI1CON1bits.DISSCK = 0;    //SCK pin is controlled by SPI module  
SPI1CON1bits.DISSCK = 0;    // Internal serial clock is enabled  
SPI1CON1bits.DISSDO = 0;    // SDOx pin is controlled by the module  
SPI1CON1bits.MODE16 = 1;    // Communication is word-wide (16 bits)  
SPI1CON1bits.MSTEN = 1;     // Master mode enabled  
SPI1CON1bits.SMP = 0;       // Input data is sampled at the middle of data        output time  
SPI1CON1bits.CKE = 0;       // Serial output data changes on transition from  
SPI1CON1bits.CKP = 0;       // Idle state for clock is a low level;  
SPI1STATbits.SPIEN = 1;     // Enable SPI module  
SPI1STATbits.SPITBF = 0;    //clear TX buffer full flag  
SPI1STATbits.SPIRBF = 0;    //clear RX buffer full flag  
RPOR0bits.RP0R=0b01000;     // RP0 Pin tied to SPI1 clock output  
RPOR0bits.RP1R=0b00111;     // RP1 Pin tied to SPI1 Data output  
}//~  

void DAC(unsigned int d1,  unsigned int d2)   
{  
PORTAbits.RA9=0;            // chip select on  
d = (d1 & 0xfff) | 0x1000;  // make 16 bit data for DAC IC MCP4822  
SPI1BUF = d;                // write data to the buffer  
 while(SPI1STATbits.SPITBF=1);     // wait for the data to be moved in shift register   
d=SPI1BUF;   
PORTAbits.RA9=1;            // chip select Off  

for(k=0; k++; k=5);  
PORTAbits.RA8=0;            // LDA set Low  
for(k=0; k++; k=5);   
PORTAbits.RA8=1;            // LDA Moves low to high  

for(k=0; k++; k=5);  

PORTAbits.RA9=0;  
d = (d2 & 0xfff) | 0x9000;  
d = SPI1BUF;  
while(SPI1STATbits.SPITBF=1);  
SPI1BUF = d1;  
PORTAbits.RA9=1;  

for(k=0; k++; k=5);  
PORTAbits.RA8=0;  
for(k=0; k++; k=5);  
PORTAbits.RA8=1;  

 }  

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share



×
×
  • Create New...