|
#include "stc12c5a60s2.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
unsigned char DATA=0;
#define FOSC 24000000L //System frequency
#define BAUD 9600 //UART baudrate
#define S2RI 0x01 //S2CON.0
#define S2TI 0x02 //S2CON.1
#define S2RB8 0x04 //S2CON.2
#define S2TB8 0x08 //S2CON.3
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
S2CON = 0x50; //8-bit variable UART
BRT = -(FOSC/32/BAUD); //Set auto-reload vaule of baudrate generator
AUXR = 0x14; //Baudrate generator work in 1T mode
IE2 = 0x01; //Enable UART2 interrupt
EA = 1; //Open master interrupt switch
SendString("STC12C5A60S2\r\nUart2 Test !\r\n");
while(1)
{
SendData(DATA);
}
}
/*----------------------------
UART2 interrupt service routine
----------------------------*/
void Uart2() interrupt 8
{
if (S2CON & S2RI)
{
S2CON &= ~S2RI; //Clear receive interrupt flag
DATA=S2BUF;
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
S2BUF = dat; //Send data to UART2 buffer
busy = 1;
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}
|
|