51单片机——串口通信编程
1.发送一个字符a给PC
#include "reg52.h"
#include "intrins.h"
sfr AUXR = 0x8E;
void UartInit(void)
{
AUXR = 0x1;
SCON = 0x40;
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
}
void Delay1000ms()
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
char data_msg = 'a';
UartInit();
while(1){
Delay1000ms();
SBUF = data_msg;
}
}
2.发送字符串给PC
#include "reg52.h"
#include "intrins.h"
sfr AUXR = 0x8E;
void UartInit(void)
{
AUXR = 0x01;
SCON = 0x40;
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
}
void Delay1000ms()
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI);
TI = 0;
}
void sendString(char* str)
{
while( *str != '\0'){
sendByte(*str);
str++;
}
}
void main()
{
UartInit();
while(1){
Delay1000ms();
sendString("chenlichen shuai\r\n");
}
}
3.PC通过串口点亮LED
#include "reg52.h"
#include "intrins.h"
sfr AUXR = 0x8E;
sbit D5 = P3^7;
void UartInit(void)
{
AUXR = 0x01;
SCON = 0x50;
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
}
void Delay1000ms()
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI);
TI = 0;
}
void sendString(char* str)
{
while( *str != '\0'){
sendByte(*str);
str++;
}
}
void main()
{
char cmd;
D5 = 1;
UartInit();
while(1){
Delay1000ms();
sendString("chenlichen shuai\r\n");
if(RI == 1){
RI = 0;
cmd = SBUF;
if(cmd == 'o'){
D5 = 0;
}
if(cmd == 'c'){
D5 = 1;
}
}
}
}
4.PC串口中断控制LED
#include "reg52.h"
#include "intrins.h"
sfr AUXR = 0x8E;
sbit D5 = P3^7;
char cmd;
void UartInit(void)
{
AUXR = 0x01;
SCON = 0x50;
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
EA = 1;
ES = 1;
}
void Delay1000ms()
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI);
TI = 0;
}
void sendString(char* str)
{
while( *str != '\0'){
sendByte(*str);
str++;
}
}
void main()
{
D5 = 1;
UartInit();
while(1){
Delay1000ms();
sendString("chenlichen shuai\r\n");
}
}
void Uart_Handler() interrupt 4
{
if(RI)
{
RI = 0;
cmd = SBUF;
if(cmd == 1){
D5 = 0;
}
if(cmd == 0){
D5 = 1;
}
}
if(TI);
}
5.PC发送字符串点灯
#include "reg52.h"
#include "intrins.h"
#include <string.h>
#define SIZE 12
sfr AUXR = 0x8E;
sbit D5 = P3^7;
char cmd[SIZE];
void UartInit(void)
{
AUXR = 0x01;
SCON = 0x50;
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
EA = 1;
ES = 1;
}
void Delay1000ms()
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void sendByte(char data_msg)
{
SBUF = data_msg;
while(!TI);
TI = 0;
}
void sendString(char* str)
{
while( *str != '\0'){
sendByte(*str);
str++;
}
}
void main()
{
D5 = 1;
UartInit();
while(1){
Delay1000ms();
sendString("chenlichen shuai\r\n");
}
}
void Uart_Handler() interrupt 4
{
static int i = 0;
if(RI)
{
RI = 0;
cmd[i] = SBUF;
i++;
if(i == SIZE){
i = 0;
}
if(strstr(cmd,"en")){
D5 = 0;
i = 0;
memset(cmd,'\0',SIZE);
}
if(strstr(cmd,"se")){
D5 = 1;
i = 0;
memset(cmd,'\0',SIZE);
}
}
if(TI);
}