at89c51双机通信keil代码
时间: 2024-02-15 08:59:24 浏览: 80
以下是基于AT89C51单片机的双机通信Keil代码示例:
发送端代码:
```c
#include <reg51.h>
#define BAUDRATE 9600
#define FOSC 11059200L
#define TIMER_1_PRESCALER 12
sbit LED = P1^0;
sbit TXD = P3^1;
void init_serial() {
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = TL1 = -(FOSC / (TIMER_1_PRESCALER * 32 * BAUDRATE));
TR1 = 1;
SCON = 0x50;
}
void send_byte(unsigned char byte) {
SBUF = byte;
while (!TI);
TI = 0;
}
void main() {
unsigned char data = 0x12;
init_serial();
while (1) {
send_byte(data);
LED = ~LED;
}
}
```
接收端代码:
```c
#include <reg51.h>
#define BAUDRATE 9600
#define FOSC 11059200L
#define TIMER_1_PRESCALER 12
sbit LED = P1^0;
sbit RXD = P3^0;
void init_serial() {
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = TL1 = -(FOSC / (TIMER_1_PRESCALER * 32 * BAUDRATE));
TR1 = 1;
SCON = 0x50;
ES = 1;
EA = 1;
}
void receive_byte() interrupt 4 {
unsigned char byte;
if (RI) {
byte = SBUF;
RI = 0;
LED = ~LED;
}
}
void main() {
init_serial();
while (1);
}
```
这段代码实现了基于AT89C51单片机的双机通信,其中一个单片机发送数字,另一个单片机接收并用LED灯进行提示。
阅读全文