#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
void delay(uint timer)
{
uchar j = 124;
while(timer--)
{
while(j--);
}
}
void timer1_init()
{
TMOD = 0x20;
TL1 = 0xFD;
TH1 = 0xfd;
TR1 = 1;
}
void serial_init()
{
PCON = 0;
SCON = 0X40;
}
void main()
{
uchar send_data = 0;
uchar i = 0;
timer1_init();
serial_init();
IE = 0x00;
while(1)
{
send_data = 'A';
for(i=0;i<26;i++)
{
SBUF = send_data;
while(TI==0);
TI=0;
send_data ++;
delay(100);
}
delay(3000);
}
}
串口收发
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
void timer1_init()
{
TMOD = 0x20;
TL1 = 0xFD;
TH1 = 0xfd;
TR1 = 1;
}
void serial_init()
{
PCON = 0X80;
SCON = 0X50;
}
void main()
{
uchar tmp_data = 0;
timer1_init();
serial_init();
IE = 0x00;
while(1)
{
while(RI==0);
RI=0;
tmp_data = SBUF;
tmp_data+=32;
SBUF = tmp_data;
while(TI==0);
TI=0;
}
}
串口中断实现收发
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
void timer1_init()
{
TMOD = 0x20;
TL1 = 0xFD;
TH1 = 0xfd;
TR1 = 1;
}
void serial_init()
{
PCON = 0X80;
SCON = 0X50;
ES = 1;
}
void main()
{
uchar tmp_data = 0;
timer1_init();
serial_init();
EA=1;
while(1)
{
;
}
}
void serial_inter() interrupt 4
{
uchar tmp_data = 0;
if(RI==1)
{
RI = 0;
tmp_data = SBUF;
tmp_data += 32;
SBUF = tmp_data;
}
if(TI==1)
{
TI=0;
}
}
深入串口通信
delay.h
#include <main.h>
#ifndef _DELAY_H
#define _DELAY_H
void delay(uint timer);
#endif
delay.c
#include "main.h"
void delay(uint timer)
{
uchar j = 124;
while(timer--)
{
while(j--);
}
}
display.h
#ifndef _DISPLAY_H
#define _DISPLAY_H value
void disp_num(void);
void horse_led(void);
#endif
display.c
#include "reg51.h"
#include "main.h"
#include "delay.h"
uchar code seg[] = {0xC0,0xF9,0xA4,0xB0,0x99,
0x92,0x82,0xF8,0x80,0x90,
0xff,0x0c};
void disp_num()
{
uchar i = 0;
for (i = 0; i < 10; i++)
{
P0 = seg[i];
delay(500);
}
}
void horse_led(void)
{
uchar i = 0;
for(i = 0; i < 8; i++)
{
P1 = ~(1<<i);
delay(300);
}
}
serial.h
#ifndef _SERIAL_H
#define _SERIAL_H value
void deal_protocol(void);
#endif
serial.h
#include <reg51.h>
#include "main.h"
uchar g_pSendBUFF[6];
uchar g_pRecvBUFF[6];
extern uchar g_ucRecvNum;
extern uchar g_ucStat;
void serial_inter() interrupt 4
{
static uchar sSendNum = 0;
uchar tmp = 0;
if(RI == 1)
{
RI = 0;
tmp = SBUF;
if(g_ucRecvNum==0&&tmp==0xeb)
{
g_pRecvBUFF[0] = tmp;
g_ucRecvNum++;
}else
{
if (g_ucRecvNum > 0)
{
g_pRecvBUFF[g_ucRecvNum] = tmp;
g_ucRecvNum++;
}
}
// RI = 0;
// tmp = SBUF;
// SBUF = g_pRecvBUFF[g_ucRecvNum];
}
if (TI == 1)
{
TI = 0;
sSendNum++;
if (sSendNum < 6)
{
SBUF = g_pSendBUFF[sSendNum];
} else {
sSendNum = 0;
}
}
}
void deal_protocol(void)
{
uchar chk_sum = 0;
if (g_ucRecvNum >= 6)
{
g_ucRecvNum = 0;
if(g_pRecvBUFF[1] != 0x06)
{
return;
}
if(g_pRecvBUFF[5] != 0x90)
{
return;
}
chk_sum = g_pRecvBUFF[1] + g_pRecvBUFF[2] + g_pRecvBUFF[3];
if (chk_sum != g_pRecvBUFF[4])
{
return;
}
if(g_pRecvBUFF[2] == 0x01)
{
switch(g_pRecvBUFF[3])
{
case 1:
g_ucStat = 1;
g_pSendBUFF[2] = 0x01;
g_pSendBUFF[3] = 0;
break;
case 2:
g_ucStat = 2;
g_pSendBUFF[2] = 0x01;
g_pSendBUFF[3] = 0;
default:
break;
}
} else
{
g_pSendBUFF[2] = 0x02;
g_pSendBUFF[3] = g_ucStat;
}
g_pSendBUFF[0] = 0xeb;
g_pSendBUFF[1] = 0x06;
g_pSendBUFF[4] = g_pSendBUFF[1] + g_pSendBUFF[2] + g_pSendBUFF[3];
g_pSendBUFF[5] = 0x90;
SBUF = g_pSendBUFF[0];
}
}