Re: SPI using 89c51ED2
sorry..
Here's the code I've used.
I am using the KEIL uVision 3 IDE. I've also checked whether the SPI interrupt is coming or not? it's giving the interrupt.
//dsp7758.c
#pragma NOAREGS
void ReadDSP7758(unsigned char address, unsigned char length)
{
idata unsigned char index=0x00;
SPIStart();
SPIWrite(address);
HOLD
while(index<length)
{
SPIBUF[index]=SPIRead();
index++;
HOLD
}
SPIStop();
}
#pragma AREGS
#pragma NOAREGS
void WriteDSP7758(unsigned char address, unsigned char length)
{
unsigned char index=0x00;
SPIStart();
SPIWrite(address);
HOLD
while(index<length)
{
SPIWrite(SPIBUF[index]);
index++;
HOLD
}
SPIStop();
}
#pragma AREGS
//spi.c
unsigned char serial_data;
bit transmit_completed= 0;
unsigned char SPIBUF[3];
xdata unsigned char temp_value;
void it_SPI(void) interrupt 9 using 1 /* interrupt address is 0x004B */
{
switch ( SPSTA ) /* read and clear spi status register */
{
case 0x80:
serial_data=SPDAT; /* read receive data */
transmit_completed=1;/* set software flag */
temp_value++;
break;
case 0x10:
/* put here for mode fault tasking */
temp_value=0;
break;
case 0x40:
/* put here for overrun tasking */
temp_value=0;
break;
}
}
//#pragma AREGS
void SPIStart()
{
SPCON |= 0x10; /* Master mode */
P1_1=1; /* enable master */
//SPCON |= 0x82; /* Fclk Periph/128 */
SPCON |= 0x81; /* Fclk Periph/64 */
SPCON &= ~0x08; /* CPOL=0; transmit mode example */
SPCON |= 0x04; /* CPHA=1; transmit mode example */
IEN1 |= 0x04; /* enable spi interrupt */
SPCON |= 0x40; /* run spi */
EA=1; /* enable interrupts */
CS=0; //Chip Select of ADE7758
}
void SPIStop()
{
CS=1;
SPCON &= ~0x40; /* stop spi */
}
#pragma AREGS
//====================================================
void SPIWrite(unsigned char spidata)
{
SPDAT=spidata; /* send an example data */
while(!transmit_completed);/* wait end of transmition */
transmit_completed = 0; /* clear software transfert flag */
}
#pragma AREGS
//==================================================================================================================================
#pragma NOAREGS
unsigned char SPIRead()
{
SPDAT=0x00; /* data is send to generate SCK signal */
while(!transmit_completed);/* wait end of transmition */
transmit_completed = 0; /* clear software transfert flag */
//data_save = serial_data; /* save receive data */
return serial_data; /* save receive data */
}
#pragma AREGS
//main.c
void main()
{
SPIBUF[0]=0x40;
WriteDSP7758(WR_APHASE,1);
while(1)
{
ReadDSP7758(RD_WAVMODE,1);
test_value=SPIBUF[0];
DisplayPage(); //displays value of WAVMODE and also the
// temp_value which is incremented in the SPI interrupt
}
}