#include "reg51.h" #include "intrins.h" #define FOSC 11059200UL #define BRT (65536 - FOSC / 115200 / 4) sfr AUXR = 0x8e; sfr T2H = 0xd6; sfr T2L = 0xd7; sfr S2CON = 0x9a; sfr S2BUF = 0x9b; sfr IE2 = 0xaf; sfr P0M1 = 0x93; sfr P0M0 = 0x94; sfr P1M1 = 0x91; sfr P1M0 = 0x92; sfr P2M1 = 0x95; sfr P2M0 = 0x96; sfr P3M1 = 0xb1; sfr P3M0 = 0xb2; sfr P4M1 = 0xb3; sfr P4M0 = 0xb4; sfr P5M1 = 0xc9; sfr P5M0 = 0xca; bit busy; char wptr; char rptr; char buffer[16]; void Uart2Isr() interrupt 8 { if (S2CON & 0x02) { S2CON &= ~0x02; busy = 0; } if (S2CON & 0x01) { S2CON &= ~0x01; buffer[wptr++] = S2BUF; wptr &= 0x0f; } } void Uart2Init() { S2CON = 0x10; T2L = BRT; T2H = BRT >> 8; AUXR = 0x14; wptr = 0x00; rptr = 0x00; busy = 0; } void Uart2Send(char dat) { while (busy); busy = 1; S2BUF = dat; } void Uart2SendStr(char *p) { while (*p) { Uart2Send(*p++); } } void main() { P0M0 = 0x00; P0M1 = 0x00; P1M0 = 0x00; P1M1 = 0x00; P2M0 = 0x00; P2M1 = 0x00; P3M0 = 0x00; P3M1 = 0x00; P4M0 = 0x00; P4M1 = 0x00; P5M0 = 0x00; P5M1 = 0x00; Uart2Init(); IE2 = 0x01; EA = 1; Uart2SendStr("Uart Test !\r\n"); while (1) { if (rptr != wptr) { Uart2Send(buffer[rptr++]); rptr &= 0x0f; } } } 修改此程序使发送者发送字母o接收者连接的板子中端口P2.5灯开始亮其余灯灭
时间: 2023-08-10 12:02:48 浏览: 142
intrins.h.rar_INTRINS_Keil C51 intrins.h_c51 _cro_intrins.h xiaz
5星 · 资源好评率100%
将程序中的 P2M1 和 P2M0 的值改为 0x20,即可将 P2.5 设置为推挽输出模式。然后在主函数中加入以下代码,即可实现发送字母o时点亮 P2.5 灯,停止发送时熄灭 P2.5 灯的功能:
```
while (1) {
if (rptr != wptr) {
if (buffer[rptr] == 'o') {
P2_5 = 0; // 点亮 P2.5 灯
} else {
P2_5 = 1; // 熄灭 P2.5 灯
}
Uart2Send(buffer[rptr++]);
rptr &= 0x0f;
}
}
```
需要注意的是,程序中的 P2_5 是对 P2.5 引脚进行操作的宏定义,需要根据具体的单片机型号进行修改。
阅读全文