#include <reg51.h> #include <stdio.h> #include <stdlib.h> int a; char c; #define FOSC 11059200L #define BAUD 9600 void Usart_Init() { TMOD &= 0x0F; TMOD |= 0x20; SCON = 0x50; TH1 = (256 - FOSC / 12 / BAUD); TL1 = TH1; TR1 = 1; // 启动定时器 1 ES = 1; // 允许串口中断 EA = 1; // 开启总中断 } void Usart_SendChar(char c) { TI = 0; // 清除发送完成标志 SBUF = c; while (!TI); // 等待发送完成 } void Usart_String(char *s) { while (*s) { Usart_SendChar(*s++); } } char Usart_ReceiveChar() { while (!RI); // 等待接收完成 RI = 0; // 清除接收完成标志 return SBUF; } int Usart_ReceiveString(char *s) { char c; while ((c = Usart_ReceiveChar()) != '\r') { *s++ = c; Usart_SendChar(c); // 回显 } *s = 0; } 修改为主机AT89C51通过P3.1口向从机AT89C51P3.0口输出数据的头文件
时间: 2024-01-26 12:02:07 浏览: 149
#include <reg51.h>
#include <stdio.h>
#include <stdlib.h>
int a;
char c;
#define FOSC 11059200L
#define BAUD 9600
sbit TX = P3^1; // 将TX引脚定义为P3.1
sbit RX = P3^0; // 将RX引脚定义为P3.0
void Usart_Init()
{
TMOD &= 0x0F;
TMOD |= 0x20;
SCON = 0x50;
TH1 = (256 - FOSC / 12 / BAUD);
TL1 = TH1;
TR1 = 1;
ES = 1;
EA = 1;
}
void Usart_SendChar(char c)
{
TI = 0;
SBUF = c;
while (!TI);
}
void Usart_String(char *s)
{
while (*s)
{
Usart_SendChar(*s++);
}
}
char Usart_ReceiveChar()
{
while (!RI);
RI = 0;
return SBUF;
}
int Usart_ReceiveString(char *s)
{
char c;
while ((c = Usart_ReceiveChar()) != '\r') {
*s++ = c;
Usart_SendChar(c);
}
*s = 0;
}
阅读全文