Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

can any one help in SPI

Status
Not open for further replies.

akhileshchidare

Member level 3
Joined
Oct 4, 2005
Messages
59
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,758
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

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);
}
 

nagi

Newbie level 5
Joined
Sep 29, 2005
Messages
8
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,362
awatthr calibration

Hi;

I think you don't need configure SDI pin as i/p pin. it will be configured automatically.

just wondering did you calibrate IC.

You can debug your spi communication, data by sending the data that you received from ADE 7758, to hyper terminal.

It would be better to store calibration parameters in devise EEPROM.

cheers
nagi
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top