编程完成⼀个串口的收发数据程序。要求,51单片机与 PC 机进行串口通 信,PC 机发送字符给单片机,单片机统计收到的'A'字符的个数,在数码 管上显示个数。当收到 10 个'A'时发送字 符串 Test OK。';当收到 20 个 'A'时发送字符串'Uart OK !'。通信速率为 4800bps,单片机外接晶振频 率为 11.0592MHz。
时间: 2023-12-10 22:39:17 浏览: 84
这是一道涉及到硬件和嵌入式编程的问题,以下是一个基本的解决方案:
单片机部分:
```c
#include <reg51.h>
#include <intrins.h>
#define FREQ_OSC 11059200UL // 晶振频率
#define BAUD_RATE 4800UL // 通信速率
#define TIMER_PRESCALER 12 // 定时器预分频值
#define TIMER_RELOAD_VALUE (65536UL - (FREQ_OSC / TIMER_PRESCALER / BAUD_RATE))
sbit LED = P1^0; // 数码管显示位选控制引脚
sbit DIO = P2^0; // 数码管数据输入引脚
sbit CLK = P2^1; // 数码管时钟控制引脚
sbit RXD = P3^0; // 串口接收引脚
sbit TXD = P3^1; // 串口发送引脚
unsigned char count = 0; // 统计收到的'A'字符个数
void delay_us(unsigned int us) {
while (us--) {
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void delay_ms(unsigned int ms) {
while (ms--) {
delay_us(1000);
}
}
void send_char(unsigned char ch) {
unsigned char i;
for (i = 0; i < 8; i++) {
CLK = 0;
DIO = ch & 0x01;
ch >>= 1;
CLK = 1;
}
}
void display_number(unsigned char num) {
LED = 0;
send_char(num % 10);
LED = 1;
delay_ms(1);
LED = 0;
send_char(num / 10);
LED = 1;
delay_ms(1);
}
void init_serial() {
TMOD &= 0x0F;
TMOD |= 0x20; // Timer 1, mode 2
TH1 = TL1 = TIMER_RELOAD_VALUE;
TR1 = 1; // start timer 1
SM0 = 0;
SM1 = 1; // mode 1: 8-bit UART, variable baud rate
REN = 1; // enable receiver
EA = 1; // enable interrupts
ES = 1; // enable serial port interrupt
}
void main() {
init_serial();
while (1) {
display_number(count);
}
}
void serial_isr() interrupt 4 using 1 {
if (RI) {
RI = 0;
if (SBUF == 'A') {
count++;
if (count == 10) {
SBUF = 'T';
while (!TI);
TI = 0;
SBUF = 'e';
while (!TI);
TI = 0;
SBUF = 's';
while (!TI);
TI = 0;
SBUF = 't';
while (!TI);
TI = 0;
SBUF = ' ';
while (!TI);
TI = 0;
SBUF = 'O';
while (!TI);
TI = 0;
SBUF = 'K';
while (!TI);
TI = 0;
} else if (count == 20) {
SBUF = 'U';
while (!TI);
TI = 0;
SBUF = 'a';
while (!TI);
TI = 0;
SBUF = 'r';
while (!TI);
TI = 0;
SBUF = 't';
while (!TI);
TI = 0;
SBUF = ' ';
while (!TI);
TI = 0;
SBUF = 'O';
while (!TI);
TI = 0;
SBUF = 'K';
while (!TI);
TI = 0;
count = 0;
}
}
}
if (TI) {
TI = 0;
}
}
```
PC机部分:
```python
import serial
ser = serial.Serial('COM3', 4800, timeout=0.5)
ser.write(b'A' * 10)
print(ser.readline().decode().strip()) # Test OK
ser.write(b'A' * 10)
print(ser.readline().decode().strip()) # Uart OK !
ser.close()
```
请注意,这只是一个简单的示例程序,实际情况还需要考虑许多因素,例如数据传输的完整性和正确性、错误处理、通讯协议等等。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)