This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

AT89S8252 Interrupt based SPI Communication

I am trying to send bytes from UART & SPI by involving ISR. Since there is no SPI simulation available in KEIL. For debuging purpose i toggle a pin after sening one byte from SPI port & then from UART.

In simulation it seems ok but in real scenario it does not work at all.

Before it i tried SPI in polling work which worked well.

The code is following

#include <AT898252.H>

unsigned char UART_RX=0;
unsigned char SPI_RX =0;
unsigned char SPI_RX_FLUSH=0;
bit UART_TRANSMIT=0;
bit SPI_TRANSMIT= 0;
bit SPI_RECV= 0;

void UART_SPI_ISR(void) interrupt 4
{ if(RI) {UART_RX=SBUF; RI=0; }

if(TI) {TI=0; UART_TRANSMIT=0;}

if (SPSR==0x80) { if(SPI_RECV==1) { SPI_RX=SPDR; SPI_RECV=0; }

if(SPI_TRANSMIT==1) { SPI_TRANSMIT=0; SPI_RX_FLUSH=SPDR; SS=1;}
/* set software flag */ }

}

void UART_SPI_INIT()
{ /*--- Initialize the serial port.----------*/

TMOD = 0x20; PCON=PCON|0x80; TH1=0xFF; // BAUD RATE 57600 TR1 = 1; SCON = 0x50;
/*--------- Initialize the SPI port------*/ SPCR = 0xD3; /* 11010011 */

ES=1; EA=1;
}

/*----------------------------------------------*/
//--------------------------------------------
//UART SEND CHAR FUNCTION
//--------------------------------------------
void SEND_CHAR_UART(unsigned char SND)
{

while((SPI_TRANSMIT)||(SPI_RECV)||(UART_TRANSMIT)); if(UART_TRANSMIT==0) { UART_TRANSMIT=1; SBUF=SND; }

}
/*-------------------------------------------*/
//--------------------------------------------
void SEND_CHAR_SPI(unsigned char SND)
{

while((SPI_TRANSMIT)||(SPI_RECV)||(UART_TRANSMIT)); if(SPI_TRANSMIT==0) { SPI_TRANSMIT=1; SPDR=SND; }

}

//--------------------------------------------
//SPI RECV CHAR FUNCTION
//--------------------------------------------
unsigned char RECV_CHAR_SPI()
{ unsigned char RECVD=0;

while((SPI_TRANSMIT)||(SPI_RECV)||(UART_TRANSMIT));

SPDR=0xFF; SPI_RECV=1;

while((SPI_TRANSMIT)||(SPI_RECV)||(UART_TRANSMIT)); if(SPI_RECV==0) { RECVD=SPI_RX; return RECVD; } }

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void main (void)
{

UART_SPI_INIT();

while (1) { SEND_CHAR_UART(0x55); SEND_CHAR_SPI(0x55); P1_0=~P1_0;

}

}

0