PC机与单片机通信RS232协议文档格式.docx
《PC机与单片机通信RS232协议文档格式.docx》由会员分享,可在线阅读,更多相关《PC机与单片机通信RS232协议文档格式.docx(18页珍藏版)》请在冰豆网上搜索。
![PC机与单片机通信RS232协议文档格式.docx](https://file1.bdocx.com/fileroot1/2022-11/24/6f78e80c-22d5-4244-8887-f95885a0e3dd/6f78e80c-22d5-4244-8887-f95885a0e3dd1.gif)
11110010
0xb0
4
00110110
10010011
01101100
0x99
5
01101101
01001001
10110110
0x92
6
01111101
01000001
10111110
0x82
7
00000111
00011111
11100000
0xf8
8
01111111
00000001
11111110
0x80
9
01101111
00001001
11110110
0x90
A
01110111
00010001
11101110
0x88
B
01111100
11000001
00111110
0x83
C
00111001
01100011
10011100
0xc6
D
01011110
10000101
01111010
0xa1
E
01111001
01100001
10011110
0x86
F
01110001
10001110
0x8e
ASCII常用代码表
字符
代码
32
52
72
H
92
\
112
p
33
!
53
73
I
93
]
113
q
34
”
54
74
J
94
^
114
r
35
#
55
75
K
95
_
115
s
36
$
56
76
L
96
`
116
t
37
%
57
77
M
97
a
117
u
38
&
58
:
78
N
98
b
118
v
39
’
59
;
79
O
99
c
119
w
40
(
60
<
80
P
100
d
120
x
41
)
61
=
81
Q
101
e
121
y
42
*
62
>
82
R
102
f
122
z
43
+
63
?
83
S
103
g
123
{
44
64
@
84
T
104
h
124
|
45
-
65
85
U
105
i
125
}
46
.
66
86
V
106
j
126
~
47
/
67
87
W
107
k
48
68
88
X
108
l
49
69
89
Y
109
m
50
70
90
Z
110
n
51
71
G
91
[
111
o
[C语言源程序]
#include"
reg52.h"
//包函8051内部资源的定义
unsignedchardat;
//用于存储单片机接收发送缓冲寄存器SBUF里面的内容
sbitgewei=P2^4;
//个位选通定义
sbitshiwei=P2^5;
//十位选通定义
sbitbaiwei=P2^6;
//百位选通定义
unsignedcharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,};
//1~10
voidDelay(unsignedinttc)//延时程序
while(tc!
=0)
{unsignedinti;
for(i=0;
i<
100;
i++);
tc--;
voidLED()//LED显示接收到的数据(十进制)
gewei=0;
P0=table[dat%10];
Delay(10);
gewei=1;
shiwei=0;
P0=table[dat/10];
shiwei=1;
baiwei=0;
P0=table[dat/100];
baiwei=1;
///////功能:
串口初始化,波特率9600,方式1/////////
voidInit_Com(void)
TMOD=0x20;
PCON=0x00;
SCON=0x50;
TH1=0xFd;
TL1=0xFd;
TR1=1;
/////主程序功能:
实现接收数据并把接收到的数据原样发送回去///////
voidmain()
Init_Com();
//串口初始化
while
(1)
{
if(RI)//扫描判断是否接收到数据,
dat=SBUF;
//接收数据SBUF赋与dat
RI=0;
//RI清零。
SBUF=dat;
//在原样把数据发送回去(接收数据为发送数据的ASCII码,如发送q显示为113)
LED();
//显示接收到的数据
///这一个例子是以扫描的方式编写的,还可以以中断的方式编写,请大家思考//////
[实验任务]PC控制单片机IO口输出
if(dat==0x00)//如果PC发送十六进制00,单片机P1口全亮。
P1=0x00;
elseif(dat==0x01)
P1=0x01;
elseif(dat==0x02)
P1=0x02;
elseif(dat==0x03)
P1=0x03;
elseif(dat==0x04)
P1=0x04;
}
[实验任务]单片机控制实训指导及综合应用实例
//包函AT89S52内部资源的定义(注视参看书160页)
#defineucharunsignedchar//宏定义
ucharled[10]={0xbf,0x06,0xdb,0xcf,0x66,0xed,0xfd,0x87,0xff,0xef};
//数字1~10,字符串
ucharled1[64];
//存储接收数据
unsignedinti,j,t,k;
sbitP32=P3^2;
voidscjs(void)interrupt4//定义中断
ES=0;
//使能串行口的中断
k=1;
led1[k-1]=SBUF;
//将接收数据送段码表
k++;
TH0=0x3c;
//t0定时50sm内接收不到数据跳过接收
TL0=0xb0;
TR0=1;
while(!
RI)
if(!
TF0)gotoFH;
FH:
TF0=0;
//TF0不清零不能重新接收
TR0=0;
P32=1;
for(j=1;
j<
k;
j++)
SBUF=led[j-1];
//数据送回给pc
TI);
TI=0;
P32=0;
delay(t)
ES=1;
SCON=0x50;
i<
t;
i++);
SCON=0x00;
voidmain(void)
inta,c;
TMOD=0x21;
TH1=0Xfd;
TL1=0xfd;
PCON=0x00;
IE=0x90;
TR1=1;
for(a=0;
a<
=k+1/k*8;
a++)
for(c=5;
c>
=0;
c--)
if(a+c<
k+1/k*8+1)
if(k==1)SBUF=led[a+c];
elseSBUF=led1[a+c];
elseSBUF=0x00;
delay(39000);
#defineucharunsignedchar
sbitP3_2=P3^2;
ucharzdzt=0x0c;
ucharzsgw=0x02;
ucharzsdw=0x00;
ucharbs=0;
ucharzqsbw=0;
ucharzqssw=0;
ucharzqsgw=0;
ucharyxsj=0x11;
ucharzs=20;
intzqs=0;
ucharzqsgzj=0x00;
ucharzqsdzj=0x00;
ucharzsscgw=0x1f;
ucharzsscdw=0x00;
ucharbzsj;
ucharztsj;
voidkey(void);
voidkeyprc();
voidbinbcd();
voiddisp();
voiddelay();
codeuchartab[13]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x7f,0x6f,0x77,0x7c,0x39};
codeucharzssc[160]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x7f,0x6f,0x77,0x7c,
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x7f,0x6f,0x77,0x7c,0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x7f,0x6f,0x77,0x7c,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x7f,
0x6f,0x77,0x7c,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x7f,0x6f,0x77,0x7c,0x3f,
0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x7f,0x6f,0x77,0x7c,0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x7f,0x6f,0x77,0x7c,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x7f,0x6f,
0x77,0x7c,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x7f,0x6f,0x77,0x7c,0x3f,0x06,
0x5b,0x4f,0x66,0x6d,0x7d,0x7f,0x6f,0x77,0x7c,};
voidkey(void)
TR0=0;
(P2&
0X01))
delay();
0X01));
zdzt=0x0a;
disp();
elseif(!
(P3&
0X08))
0X08));
zdzt=0x0b;
0X10))
0X10));
zs=zs+1;
keyprc();
0X02))
0X02));
zs=zs-1;
voidkeyprc()
if(zs<
20)
zs=20;
if(zs>
99)
zs=99;
zsgw=zs/10;
zsdw=zs%10;
voiddjzd(void)interrupt1
TH0=zssc[(zs-20)*2];
TL0=zssc[(zs-20)*2+1];
if(zdzt==0x0a)
yxsj=yxsj<
1|yxsj>
7;
elseif(zdzt==0x0b)
7|yxsj>
1;
elseif(zdzt==0x0c)
P1=0xff;
gotoLP;
P1=yxsj;
bs++;
if(bs==48)
bs=0;
zqs++;
LP:
binbcd();
voidbinbcd()
zqsbw=zqs/100;
zqssw=zqs%100/10;
zqsgw=zqs%10;
voiddisp()
TR1=0;
TMOD=0x01;
SBUF=tab[zqsgw];
SBUF=tab[zqssw];
SBUF=tab[zqsbw];
SBUF=tab[zsdw];
SBUF=tab[zsgw];
SBUF=tab[zdzt];
TMOD=0x02;
voiddelay()
intk;
for(k=0;
k<
1200;
k++);
voidsin()interrupt4
bzsj=SBUF;
RI);
ztsj=SBUF;
if(bzsj==0x30)
zdzt=ztsj-0x37;
if(bzsj==0x31)
if(ztsj==0x49)
zs++;
elsezs--;
P3_2=0;
zqs=0;
main()
TH1=0xfd;
IE=0X92;
PS=1;
TH0=0x1f;
TL0=0x00;
SP=0x60;
key();
if(zdzt==0x0c)
elseTR0=1;
}}
单片机给计算机发送数据:
unsignedcharfan;
inti,j;
voidDelay()//延时程序
i++)
for(j=0;
j<
j++);
voiddelays()
for(k=0;
if(dat==0x30)//如果PC发送十六进制00,单片机P1口全亮。
{P1=0x00;
Delay();
}
elseif(dat==0x31)
{P1=0x01;
elseif(dat==0x32)
{P1=0x03;
elseif(dat==0x33)
{P1=0x07;
elseif(dat==0x34)
{P1=0x0f;
elseif(dat==0x35)
{P1=0x1f;
elseif(dat==0x06)
{P1=0x3f;
elseif(dat==0x07)
{P1=0x7f;
elseif(dat==0x08)
{P1=0xff;
0x01))
{delays();
0x01));
fan=0x36;
0x02))
0x02));
fan=0x37;
0x04))
0x04));
fan=0x38;
0x08))
SBUF=dat;