23.10 Example Routines
23.10.1 UART1 interrupt mode and computer transceiver test - DMA
receive timeout interrupt
C language code
//Operating frequency for test is 11.0592MHz
/************* Function Description **************
UART1 works in full-duplex interrupt mode to send and receive data. PC sends data to the MCU, and the MCU will
automatically store the received data in the DMA space. When the content received at one time is full of the set DMA
space, the data in the storage space are output through the DMA automatic sending function of UART 1. Use UART
receive interrupt to judge timeout, if no new data is received and timeout, it means that a string of data has been received,
then outputs the received content, and clear the DMA space. If a timer is uased as baud rate generator, it is recommended
to use 1T mode (unless 12T is used for low baud rates), and select a clock frequency that is divisible by the baud rate
to improve accuracy.
When downloading, select the clock 22.1184MHz (users can modify the frequency by themselves).
******************************************/
#include "stdio.h"
#include "stc8a8k64d4.h"
#define MAIN_Fosc 22118400L // Define the main clock (accurately calculate 115200 baud rate)
#define Baudrate1 115200L
#define Timer0_Reload (65536UL -(MAIN_Fosc / 1000))
#define DMA_AMT_LEN 255 //Set total bytes to be transferred(0~255)
:
DMA_AMT_LEN+1
bit B_1ms; //1ms flag
bit DMATxFlag;
bit DMARxFlag;
bit BusyFlag;
u8 Rx_cnt;
u8 RX1_TimeOut;
u8 xdata DMABuffer[256];
void UART1_config(u8 brt);
void DMA_Config(void);
void UartPutc(unsigned char dat)
{
BusyFlag = 1;
SBUF = dat;
while(BusyFlag);
}
char putchar(char c)
{
UartPutc(c);
return c;
}