两个51单片机进行串口双向通信实现互相接收发送学号后四位并且显示在四位共阳数码管上,主机数码管最初显示学号2313,从机数码管最开始显示学号2305,按下按键实现两个数码管显示学号的替换
时间: 2023-07-03 13:14:07 浏览: 128
好的,这是一个关于串口通信和数字信号显示的问题。为了实现这个功能,需要编写两个程序,一个是主机程序,另一个是从机程序。
主机程序:
```c
#include <reg51.h>
#include <stdio.h>
#include <stdlib.h>
#define BAUDRATE 9600
#define FREQ_OSC 11059200
#define TH1_INIT (256 - FREQ_OSC / (32 * BAUDRATE))
sbit SCLK = P1^0;
sbit RCLK = P1^1;
sbit DIO = P1^2;
sbit KEY = P3^2;
unsigned char code led_table[] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99,
0x92, 0x82, 0xF8, 0x80, 0x90
};
unsigned char data num1 = 2, num2 = 3, num3 = 1, num4 = 3;
unsigned char data RXdata;
void delay(unsigned int t) {
while (t--);
}
void init_serial() {
TMOD |= 0x20; // timer1, mode2
TH1 = TH1_INIT;
TL1 = TH1_INIT;
TR1 = 1;
SM0 = 0; // mode1
SM1 = 1;
REN = 1; // enable receive
EA = 1; // enable interrupt
ES = 1;
}
void send_serial(unsigned char dat) {
SBUF = dat;
while (!TI);
TI = 0;
}
void send_string(unsigned char *str) {
while (*str) {
send_serial(*str++);
}
}
void display(unsigned char num1, unsigned char num2, unsigned char num3, unsigned char num4) {
unsigned char i;
unsigned char code seg[] = {0xFE, 0xFD, 0xFB, 0xF7};
unsigned char data digit[] = {num1, num2, num3, num4};
for (i = 0; i < 4; i++) {
DIO = 0;
P0 = seg[i];
DIO = 1;
P0 = led_table[digit[i]];
delay(10);
DIO = 0;
P0 = 0xFF;
}
}
void main() {
init_serial();
display(num1, num2, num3, num4);
while (1) {
if (!KEY) {
delay(1000);
if (!KEY) {
num1 = RXdata / 1000;
num2 = RXdata % 1000 / 100;
num3 = RXdata % 100 / 10;
num4 = RXdata % 10;
display(num1, num2, num3, num4);
}
}
}
}
void serial_isr() interrupt 4 {
if (RI) {
RI = 0;
RXdata = SBUF;
}
}
```
从机程序:
```c
#include <reg51.h>
#include <stdio.h>
#include <stdlib.h>
#define BAUDRATE 9600
#define FREQ_OSC 11059200
#define TH1_INIT (256 - FREQ_OSC / (32 * BAUDRATE))
sbit SCLK = P1^0;
sbit RCLK = P1^1;
sbit DIO = P1^2;
sbit KEY = P3^2;
unsigned char code led_table[] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99,
0x92, 0x82, 0xF8, 0x80, 0x90
};
unsigned char data num1 = 2, num2 = 3, num3 = 0, num4 = 5;
unsigned char data RXdata;
void delay(unsigned int t) {
while (t--);
}
void init_serial() {
TMOD |= 0x20; // timer1, mode2
TH1 = TH1_INIT;
TL1 = TH1_INIT;
TR1 = 1;
SM0 = 0; // mode1
SM1 = 1;
REN = 1; // enable receive
EA = 1; // enable interrupt
ES = 1;
}
void send_serial(unsigned char dat) {
SBUF = dat;
while (!TI);
TI = 0;
}
void send_string(unsigned char *str) {
while (*str) {
send_serial(*str++);
}
}
void display(unsigned char num1, unsigned char num2, unsigned char num3, unsigned char num4) {
unsigned char i;
unsigned char code seg[] = {0xFE, 0xFD, 0xFB, 0xF7};
unsigned char data digit[] = {num1, num2, num3, num4};
for (i = 0; i < 4; i++) {
DIO = 0;
P0 = seg[i];
DIO = 1;
P0 = led_table[digit[i]];
delay(10);
DIO = 0;
P0 = 0xFF;
}
}
void main() {
init_serial();
display(num1, num2, num3, num4);
while (1) {
if (!KEY) {
delay(1000);
if (!KEY) {
num1 = RXdata / 1000;
num2 = RXdata % 1000 / 100;
num3 = RXdata % 100 / 10;
num4 = RXdata % 10;
display(num1, num2, num3, num4);
}
}
}
}
void serial_isr() interrupt 4 {
if (RI) {
RI = 0;
RXdata = SBUF;
}
}
```
在两个单片机之间,需要连接串口通信的引脚:主机的P3.0和从机的P3.1分别接收和发送数据,从机的P3.0和主机的P3.1分别接收和发送数据。此外,还需要连接一个按键,按下按键后,两个数码管显示的学号会进行替换。
以上是代码实现,需要将代码下载到对应的单片机上进行测试。
阅读全文