[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 |
void uartSetInit(unsigned long f_osc,unsigned long f_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 |
void uartInit() |
31 |
{ |
32 |
uartSetInit(11059200,9600,1,0); |
33 |
} |
34 |
void uartSendByte( char ch) //发送中断一般用查询 |
35 |
{ |
36 |
while (TI); |
37 |
SBUF=ch; |
38 |
while (!TI); |
39 |
TI=0; |
40 |
} |
41 |
void uartSendString( char str[]) |
42 |
{ |
43 |
while (*str) |
44 |
{ |
45 |
uartSendByte(*str); |
46 |
str++; |
47 |
} |
48 |
} |
49 |
void serInt() interrupt 4 |
50 |
{ |
51 |
if (RI) |
52 |
{ |
53 |
/*handle received byte*/ |
54 |
RI=0; |
55 |
} |
56 |
} |
57 |
#endif |