51单片机串行通信实验

img


实现单片机和 PC 串口之间数据互相通信,即单片机可接收 PC 发送的数字字符(0~9),并在单个数码管上显示,按下单片机的 K1 键后,单片机可向 PC 发送字符串(如“我是姓名,学号为,告诉你这是由 8051 单片机发送的字符串!”)。

https://m.baidu.com/video/page?pd=video_page&nid=14627336098292084624&sign=15160492325724764946&word=51%E5%8D%95%E7%89%87%E6%9C%BA%E5%92%8CPC%E4%B8%B2%E5%8F%A3%E4%B9%8B%E9%97%B4%E6%95%B0%E6%8D%AE%E4%BA%92%E7%9B%B8%E9%80%9A%E4%BF%A1%2C%E5%8D%B3%E5%8D%95%E7%89%87%E6%9C%BA%E5%8F%AF%E6%8E%A5%E6%94%B6PC%E5%8F%91%E9%80%81%E7%9A%84%E6%95%B0%E5%AD%97%E5%AD%97%E7%AC%A6(0~9)&oword=51%E5%8D%95%E7%89%87%E6%9C%BA%E5%92%8CPC%E4%B8%B2%E5%8F%A3%E4%B9%8B%E9%97%B4%E6%95%B0%E6%8D%AE%E4%BA%92%E7%9B%B8%E9%80%9A%E4%BF%A1%2C%E5%8D%B3%E5%8D%95%E7%89%87%E6%9C%BA%E5%8F%AF%E6%8E%A5%E6%94%B6PC%E5%8F%91%E9%80%81%E7%9A%84%E6%95%B0%E5%AD%97%E5%AD%97%E7%AC%A6(0~9)&atn=index&frsrcid=4185&ext=%7B%22jsy%22%3A1%7D&top=%7B%22sfhs%22%3A1%2C%22_hold%22%3A2%7D&sl=2&fr0=B&fr1=B&ms=1&lid=7664647368229221063&_t=1655048151624

#include "reg51.h"
#include "intrins.h"

typedef unsigned char BYTE;
typedef unsigned int WORD;

#define FOSC 11059200L //System frequency
#define BAUD 9600 //UART baudrate

/Define UART parity mode/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity

#define PARITYBIT NONE_PARITY //Testing even parity

sbit bit9 = P2^2; //P2.2 show UART data bit9
bit busy;

void SendData(BYTE dat);
void SendString(char *s);

void main()
{
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif

TMOD = 0x20;            //Set Timer1 as 8-bit auto reload mode
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
TR1 = 1;                //Timer1 start run
ES = 1;                 //Enable UART interrupt
EA = 1;                 //Open master interrupt switch

SendString("我是姓名,学号为xxXX,告诉你这是由8051单片机发送的字符串!");
while(1);

}

/----------------------------
UART interrupt service routine
----------------------------
/
void Uart_Isr() interrupt 4
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
P0 = SBUF; //P0 show UART data
bit9 = RB8; //P2.2 show parity bit
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}

/----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------
/
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //Set parity bit to 0
#endif
}
busy = 1;
SBUF = ACC; //Send data to UART buffer
}

/----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------
/
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}