malik_zohaib
Full Member level 5

Hi guys I'm facing problem interfacing serial dac with picf2550. i tried simulation, it works but hardware is not working. Actually I'm trying to generate modefied sine and cos waves.
I think there might be something I'm doing wrong with configuration bits or may be SPI is different.
Need suggestion.
P.S: i tried the same code with a few changes with PIC18F452 its works. than why its not working with 2550???
here is code n schematic:

I think there might be something I'm doing wrong with configuration bits or may be SPI is different.
Need suggestion.
P.S: i tried the same code with a few changes with PIC18F452 its works. than why its not working with 2550???
here is code n schematic:
Code:
#include<htc.h>
//__CONFIG(1,PLLDIV4 & CPUDIV_OSC1_PLL2 & USBDIV_2 & IESO_OFF & FCMEN_OFF & FOSC_HSPLL_FOSC = INTOSC_EC);
__CONFIG(1,PLLDIV2 & IESO_OFF & FCMEN_OFF & FOSC_HS);
__CONFIG(2,VREGEN_OFF & BOR_OFF & BORV_0 & PWRT_ON & WDT_OFF);
__CONFIG(3, MCLRE_ON & PBADEN_OFF & LPT1OSC_OFF & CCP2MX_ON);
__CONFIG(4,DEBUG_ON & LVP_OFF & STVREN_OFF & XINST_OFF);
#define _XTAL_FREQ 8000000
#define chip_sel RB3
void dac(unsigned int, unsigned int);
const unsigned int sine [28]={2048,2503,2936,3324,3648,3892,4044,4095,4044,3892,3648,3324,2936,2503,2048,1592,1159,771,447,203,51,0,51,203,447,771,1159,1592}; //works well modified sine wave
unsigned char count=0,new_sector=0, old_sector=0,i=0, j=7; //14
unsigned int value=0,time=0;
unsigned long time_div=0;
void main(){
TRISB=5;
PORTB=0;
TRISC=0;
PORTC=0;
PORTA=0;
TRISA=0;
ADCON1=0x0f;
CMCON=0x07;
GIE=1;
PEIE=1;
TMR2IP=1;
TMR2IE=1;
PR2=100; //hv to adjust this value for exact frequency
TMR2=0;
SSPSTAT=0xC0; //Status Register SSPSTAT=11000000
SSPCON1=0x20; //Enables serial port pins & set the SPI clock as clock = FOSC/4
T2CON=0x04;
while(1);
}
void dac(unsigned int data, unsigned int dac_sel)
{
unsigned int lower_bits;
unsigned int upper_bits;
//first obtain the upper 8 bits
upper_bits = data/256; // obtain the upper 4 bits
upper_bits = (dac_sel) | upper_bits; // append 48 to the above 4 bits for DACa o/p n 176 for DACb o/p
//now obtain the lower 8 bits
lower_bits = 255 & data; // ANDing separates the lower 8 bits
chip_sel=0;
SSPBUF=upper_bits; // sending the upper 8 bits serially
while(!SSPSTATbits.BF); // wait until the upper 8 bits are sent
SSPBUF=lower_bits; // sending the lower 8 bits serially
while(!SSPSTATbits.BF); // wait until the lower 8 bits are sent
chip_sel=1;
}
void interrupt tc_int(){
if(TMR2IE && TMR2IF){
dac(sine[i],48);
dac(sine[j],176);
i++;if(i>=28)i=0;
j++;if(j>=28)j=0;
TMR2IF=0;
}
}
