- #include "stc32g.h" //头文件见下载软件
- #include "intrins.h"
- #define FOSC 11059200UL //定义为无符号长整型,避免计算溢出
- #define BRT (65536 - (FOSC / 115200+2) / 4)
- //加 2 操作是为了让 Keil 编译器
- //自动实现四舍五入运算
- bit busy;
- char wptr;
- char rptr;
- char buffer[16];
- void UartIsr() interrupt 4
- {
- if (TI)
- {
- TI = 0;
- busy = 0;
- }
- if (RI)
- {
- RI = 0;
- buffer[wptr++] = SBUF;
- wptr &= 0x0f;
- }
- }
- void UartInit()
- {
- SCON = 0x50;
- T2L = BRT;
- T2H = BRT >> 8;
- S1BRT = 1;
- T2x12 = 1;
- T2R = 1;
- wptr = 0x00;
- rptr = 0x00;
- busy = 0;
- }
- void UartSend(char dat)
- {
- while (busy);
- busy = 1;
- SBUF = dat;
- }
- void UartSendStr(char *p)
- {
- while (*p)
- {
- UartSend(*p++);
- }
- }
- void main()
- {
- EAXFR = 1; //使能访问 XFR
- CKCON = 0x00; //设置外部数据总线速度为最快
- WTST = 0x00; //设置程序代码等待参数,
- //赋值为 0 可将 CPU 执行程序的速度设置为最快
- P0M0 = 0x00;
- P0M1 = 0x00;
- P1M0 = 0x00;
- P1M1 = 0x00;
- P2M0 = 0x00;
- P2M1 = 0x00;
- P3M0 = 0x00;
- P3M1 = 0x00;
- P4M0 = 0x00;
- P4M1 = 0x00;
- P5M0 = 0x00;
- P5M1 = 0x00;
- UartInit();
- ES = 1;
- EA = 1;
- UartSendStr("Uart Test !\r\n");
- while (1)
- {
- if (rptr != wptr)
- {
- UartSend(buffer[rptr++]);
- rptr &= 0x0f;
- }
- }
- }
复制代码
|