51单片机串口

[C/C++]代码

01 /*******************************
02 ********** 使用timer1 ********************
03 ********** 换行符 \r\n ************
04 *******************************/
05
06
07 #ifndef __UART_H__
08 #define __UART_H__
09 #include<reg52.h>
10
11
12 /*******************************
13 ** 8位uart可变波特率
14 *******************************/
15 voiduartSetInit(unsignedlongf_osc,unsignedlongf_baud,bit ren,bit doubleBaud)//晶振频率,波特率,是否允许接收,是否波特率倍速
16 {
17 TMOD=0x20;//设置定时器1为工作方式2
18 TH1=256-f_osc/32/12/f_baud;
19 TL1=256-f_osc/32/12/f_baud;
20 TR1=1;
21 PCON=(char)doubleBaud<<7;
22 SCON=0X40;//SM0=0;SM1=1;
23 REN=ren;
24 ES=1;
25 // EA=1; //总中断最后在主函数中开
26 }
27 /*******************************
28 ** 一般晶振 ****************
29 *******************************/
30 voiduartInit()
31 {
32 uartSetInit(11059200,9600,1,0);
33 }
34 voiduartSendByte(charch)//发送中断一般用查询
35 {
36 while(TI);
37 SBUF=ch;
38 while(!TI);
39 TI=0;
40 }
41 voiduartSendString(charstr[])
42 {
43 while(*str)
44 {
45 uartSendByte(*str);
46 str++;
47 }
48 }
49 voidserInt() interrupt 4
50 {
51 if(RI)
52 {
53 /*handle received byte*/
54 RI=0;
55 }
56 }
57 #endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值