akhileshchidare
Member level 3

writespi() and readspi()) are not
i have written a code for SPI communication with ADE7758.
i want to know whether i am doin it right or not...
the code is compiling with out any errors... but logically is that correct? i am accessing ADE registers thru SPI from the PIC controller....
thanx
akhilesh
i have written a code for SPI communication with ADE7758.
i want to know whether i am doin it right or not...
the code is compiling with out any errors... but logically is that correct? i am accessing ADE registers thru SPI from the PIC controller....
thanx
akhilesh
Code:
#include"p18f6490.h"
#define CONFIG UNPROTECT & BOREN & MCLRDIS & PWRTEN & WDTDIS & INTCLK & IESODIS & FCMDIS
/*************** AnalogDevices AD7758 *******************/
/* for reading the watt-hour registers*/
#define AWATTHR 0x01
#define BWATTHR 0x02
#define CWATTHR 0x03
/* for reading the VAR-HR registers*/
#define AVARHR 0x04
#define BVARHR 0x05
#define CVARHR 0x06
/* for reading the VA-HR registers*/
#define AVAHR 0x07
#define BVAHR 0x08
#define CVAHR 0x09
/* for reading the I-RMS registers*/
#define AIRMS 0x0A
#define IRMS 0x0B
#define CIRMS 0x0C
/* for reading the V-RMS registers*/
#define AVRMS 0x0D
#define BVRMS 0x0E
#define CVRMS 0x0F
#define SS TRISFbits.TRISF7
#define SCK TRISCbits.TRISC3
#define SDI TRISCbits.TRISC4
#define SDO TRISCbits.TRISC5
unsigned char writeSPI(unsigned char data_out);
unsigned char readSPI(void);
void spi_init();
void spi_init()
{
SSPSTATbits.CKE=1; /*data transmitted on fallling edge*/
SSPCON1bits.CKP=1; /*clock idle state high*/
TRISFbits.TRISF7=1; /*define SS pin as input*/
TRISC &=0xFD; /*define SDo as output*/
TRISC |=0x10; /*define SDI as input*/
}
void main()
{
unsigned char data_out; /* address to be sent to the communiccation register*/
unsigned char read_data; /* data to be read to the respective register*/
unsigned int data;
void spi_init(); /* SPI initialization*/
writeSPI(data_out);
writeSPI(AWATTHR);
data = readSPI();
writeSPI(BWATTHR);
data =readSPI();
writeSPI(CWATTHR);
data = readSPI();
writeSPI(AVARHR );
data = readSPI();
writeSPI(BVARHR );
data = readSPI();
writeSPI(CVARHR );
data = readSPI();
while(1);
}
unsigned char writeSPI(unsigned char data_out)
{
SSPBUF =data_out;
if(SSPCON1&0x80)
return(-1);
else
{
while(!SSPSTATbits.BF);
}
return(0);
}
unsigned char readSPI(void)
{
SSPBUF=0x00;
while (!SSPSTATbits.BF);
return(SSPBUF);
}