NOORPURI
Junior Member level 3
- Joined
- Jun 29, 2006
- Messages
- 25
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,549
I am trying SPI and UART transmission using combined interrupt . In simulation it seems 100 # right but in real it does not work.
/***********************************************/
#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;}
/*-------------SPI ISR PART---------------*/
if (SPSR==0x80) /* read and clear spi status register */
{
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 = 0xD0 /* 11010000*/
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 'U' by UART
SEND_CHAR_SPI(0x55); // SEND 'U' by SPI P1_0=~P1_0;
}
}
/***********************************************/
#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;}
/*-------------SPI ISR PART---------------*/
if (SPSR==0x80) /* read and clear spi status register */
{
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 = 0xD0 /* 11010000*/
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 'U' by UART
SEND_CHAR_SPI(0x55); // SEND 'U' by SPI P1_0=~P1_0;
}
}