#include
#include
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
unsigned char FIFO;
char flag=0;
void delayms(uint z)
{
uint a,b;
for(a=z;a>0;a--)
for(b=120;b>0;b--);
}
void init_uart()
{
TRISC=0x80;
TXSTA=0X24; //enable the serial port to send 8-bit data
RCSTA=0X90; //enable the serial port to work and receive continuously
SPBRG=25; //Set baud rate to 9600BPS 4000000/9600/16-1=25
RCIE=1; //receive interrupt
GIE=1; //global interrupt
PEIE=1; //external interrupt
}
void IO_INIT(void)
{
ADCON1=0xFF; //Disable AD conversion function (second function)
TRISE = 0;
TRISC = 0X00;
TRISD = 0;
PORTD = 0;
TRISA = 0;
TRISB = 0X00;
PORTB = 0;
//PORTE = 0XFF;
RE0 = 0;
RE1=0;
RE2=0;
RBPU=0;
}
void Send_Date(unsigned char date)
{
TXREG=date;
delayms(2);
while(TXIF==0);
TXIF=0;
}
// --------------------------------------------------------------------------------------------------
// Function name: serial() serial port receive interrupt handling function
// Function: Receive data into the data buffer
// --------------------------------------------------------------------------------------------------
void interrupt usart()
{
if(RCIF) // determine if it is a serial receive interrupt
{
RCIF=0;
FIFO=RCREG; // Receive data and store
flag=1;
}
}
void main()
{
uint x;
IO_INIT();// IO initialization
init_uart();
while(1)
{
/// /* 2018.7.2 Sending program
if(flag)// data received send
{
flag=0;
Send_Date(FIFO);
delayms(5);
RCIF=0;
}
delayms(100);
if(x++>15)//Send data once in a while
{
x=0;
Send_Date('T');
Send_Date('e');
Send_Date('s');
Send_Date('t');
Send_Date('o');
Send_Date('k');
Send_Date(0x0d);
Send_Date(0x0a);
delayms(5);
RCIF=0;
}
}
}