51系列串口通讯例程

51系列串口通讯例程.rar > serial.c,

1.// 题目:串口初始化C代码

2.// 要求:

3.// 1.波特率1200bps,无奇偶校验,停止位1,数据位8

4.// 2.定时器T1做波特率发生器

5.// 软件模拟测试说明:

6.// 1.这个测试采用查询方式进行串口通讯

7.// 2.将软件仿真环境的晶震设为11.0592 MHz

8.// 3.软件模拟全速运行, 观察波特率是1200bps

9.// 4.在串口1中输入数字或字母,可观察到通讯是否有误。

10.//#pragma src

11.#include

12.//串口初始化

13.void serial_init(void)

14.{

15.//ET1= 0;//CLR 0ABH ;禁止T1中断

16. TMOD= 0x20; //MOV 89H,#20H ;

timer 1 mode 2: 8-Bit reload(定时器T1 模式2: 8位自动初值重装)

17.TH1= 0xE8;//MOV 8DH,#0E8H

18.TL1= 0xE8;//MOV 8BH,#0E8H;1200bps,11.059

19. TR1 = 1; //SETB 8EH;启动定时器1

20. SCON = 0x50;// mode 1: 10-bit UART,

enable receiver

//(模式1: 10位异步发送/接收, 使能接收允许位)

21.//SM1 = 1; // ;串行口模式1

22.//SM0 = 0;

23.//REN = 1; //;允许串行中断接收

24. SM2 = 1; //SETB O9DH ;收到有效的停止位时才将RI置1

25. ES = 1; //SETB 0ACH ;允许串行中断

26. EA = 1; //SETB 0AFH ;总中断开

27.}

28.//中断方式处理串口数据

29.void serial(void) interrupt 4 using 1

30.{

31.if(RI)

32. {

33.// RI = 0;

34.// 串口接收, 采用临时缓冲

35. }

36.#if 0

37.if(TI)

38. {

39.// TI = 0;

40.// 串口发送, 没有必要使用中断方式

41. }

42.#endif

43.}

44.// 查询方式接收串口数据

45.unsigned char getchar(void)

46.{

47.while(!RI);// 没有收到串口数据则一直等待

48. RI=0;

49.return SBUF;

50.}

51.// 查询方式发送串口数据

52.void putchar(unsigned char ch)

53.{

54. SBUF=ch;

55.while(!TI);

56. TI=0;

57.}

58.code unsigned char HEX_TAB []="0123456789ABCDEF";

59.void puthex(unsigned char ch)

60.{

61. unsigned char i,j;

62. i=ch>>4;

63. j=ch&0x0f;

64. putchar(HEX_TAB[i]);

65. putchar(HEX_TAB[j]);

66.}

51系列串口通讯例程.rar > test.c

1.// 采用查询方式进行串口通讯的例子

2.// 采用这种方式的弊端:

3.// 1.如果对方没有发送数据,则本机无限等待,而不能做其它事情

4.// 2.如果对方发送数据而本机正做其它事情,那么有可能丢失数据。

5.// 因此实际上串口接收常采用中断方式,串口发送则不需要。

6.#include

7.extern void serial_init(void);

8.extern unsigned char getchar(void);

9.extern void putchar(unsigned char ch);

10.extern void puthex(unsigned char ch);

11.void main(void)

12.{

13. unsigned char i;

14. serial_init(); // 串口初始化

15.do {

16. i=getchar();// 如果没有收到串口数据则

一直等待(查询接收串口数据的弊端)

17. ++i;

18. --i; // 观察断点

19. putchar(i);//在串口显示接收到的ASCII

数据(就是把原来收到的数据再送回串口)

20. putchar(' ');

21. putchar(' ');

22. putchar('0');

23. putchar('x');

24. puthex(i); // 在串口以十六进制格式

显示接收的ASCII数据

25. putchar(' ');

26. putchar(' ');

27. ++i;

28. --i; // 观察断点

29. }while(1);

30.}

自编的C51串口程序

1.#include

2.#define F_CPU 11059200 //11.0592Mhz

3.#define UART_BAND_RATE 9600// 9600 band

4.typedef unsigned char uint8_t;

5.//typedef signed char int8_t;

6.typedef unsigned int uint16_t;

7.//typedef signed int int16_t;

8.typedef unsigned long uint32_t;

9.//typedef signed long int32_t;

10.//接收数据

11.uint8_t receive_start,receive_number=0,

bus_changed;

12.uint8_t receive_data[100];

13.uint8_t *uart_data_ptr;

14.uint8_t uart_counter;

15.void sei(void)

16.{

17. EA = 1;

18.}

19./*

20.void cli(void)

21.{

22. EA = 0;

23.}

24.*/

25.void uart_init(void)

26./* initialize uart */

27.{

28.// TH1 = 256-((F_CPU*(SMOD+1))/(384*UART_BAND_RATE));//mode 1 baud (SMOD=0 OR 1)

29. SCON = 0x50; // mode 1: 8-bit UART, enable receiver

30. TMOD |= 0x20; // timer 1 mode 2: 8-Bit reload

31. PCON |= 0x80; //SMOD=1,baud*2

32. TH1 = 0xfA; // 11.0592M,value 9600 baud TR1 = 1;

// timer 1 run

33. ES = 1; // UART INTERRUPT open

34.}

35.void delay(uint8_t delay_time)

36.{

37. uint8_t i,j,k;

38.for(i=0;i<255;i++)

39. {

40.for(j=0;j<200;j++)

41. {

42.for(k=0;k

度 (receive_data[1]+2)) if(receive_number="="接收协议:开始,长度receive_data[1],没结

束 receive="SBUF;" RI="0;" receive; uint8_t 接收中

断 (RI) SBUF="*uart_data_ptr;" (--uart_counter) uart_data_ptr++; TI="0;"输出中

断 (TI) 4 interrupt uart_isr(void) void ;>1)) {

43. receive_start=0;

44. bus_changed=1;

45. }

46./////////////////////////

47. }

48.}

49.void uart_send(uint8_t *p,uint8_t size) /* send buffer to uart */

50.{

51. uart_data_ptr = p;

52. uart_counter=size;

53. TI = 0;

54. SBUF = *uart_data_ptr;

55.}

56.void execute_cmd(void)

57.{

58.if(receive_data[2]==0x01)

59. {

60. P1 = 0x01; //p1.0=1;

61. }

62.if(receive_data[2]==0x02)

63. {

64. P1 = 0x02; //p1.1=1;

65. }

66.if(receive_data[2]==0x03)

67. {

68. P1 = 0x04; //p1.2=1;

69. }

70.if(receive_data[2]==0x04)

71. {

72. P1 = 0x08; //p1.3=1;

73. }

74.}

75.int main(void)

76.{

77.

78. uart_init();

79. sei(); //允许中断

80.for (;;)

81. {

82.if(bus_changed == 1)//已接收

83.// if(receive_number >= 3)

84. {

85. execute_cmd(); // 执行命令

86. uart_send("ok",2);

87. delay(1);

88.//接收到数据,发送"ok"

89. uart_send(receive_data,r

eceive_number);

90. bus_changed=0;

91. receive_number=0;

92. }

93.// uart_send("yes",3);

94. delay(1);

95. }

96.}

UART.rar > LED_check.c

#include

#define uchar unsigned char

char code tab[]={0x88,0x83,0xC6,0xA1,0xF9,0xA4,0xB0,0x99};

void timer(uchar);

sbit P0_0=P0^0;

main()

{uchar i,a=3;

SCON=0;

for(;;)

{ P0_0=1;

for(i=0;i<4;i++)

{

SBUF=tab[a];

a--;

while(!TI);

TI=0;

if(a==255) a=7;

}

P0_0=0;

timer(100);

}

}

void timer(uchar t)

{ uchar i;

for(i=0;i

UART.rar > pc2mcu.c

1.include

2.#define uchar unsigned char

3.#define SL

4.AVE 0xF1

5.uchar receive();

6.void send();

7.void delay();

8.uchar p;

9.main()

10.{ TMOD=0x20;

11. TH1=TL1=0xE5;

12. PCON=0x00; //smod=0

13. TR1=1;

14. SCON=0x70;

15. P0=0xFE;

16.while(1)

17. { p=receive();

18.switch(p)

19. { case 1: P1=0xC0;break;

20.case 2: P1=0xF9;break;

21.case 3: P1=0xA4;break;

22.case 4: P1=0xB0;break;

23.case 5: P1=0x99;break;

24.case 6: P1=0x92;break;

25.case 7: P1=0x82;break;

26.case 8: P1=0xF8;break;

27.default: P1=0x00;break;

28. }

29. delay();

30. }

31.}

32.uchar receive()

33.{

34.while(RI!=0);

35. RI=0;

36. p=SBUF;

37.return(p);

38.}

39.void delay()

40.{ uchar i,j,k;

41.for(i=500;i>0;i--)

42.for(j=500;j>0;j--)

43.for(k=500;k>0;k--);

44.}

UART.rar > pc2mcu2.c,

1.#include

2.#include "lcd_chuan.h"

3.#define uchar unsigned char

4.//void delay();

5.//void dis(uchar p);

6.uchar p;

7.uchar rct1[5];

8.uchar rct2[5];

9.uchar rct3[5];

10.uchar rct4[5];

11.uchar receive()

12.{ uchar p;

13. REN=1;

14.while(RI==0);

15. RI=0;

16. p=SBUF;

17.return(p);

18.}

19.

20.main()

21.{

22. uchar r;

23. TMOD=0x20;

24. TH1=TL1=0xfd; //1200的初值=e5,,9600=fd

25. PCON=0x00; //smod=0

26. TR1=1;

27. SCON=0x60;

28. RI=1;

29. EA=0;

30. InitLCD();

31.while(1)

32. { r=receive();

33.//displaylcd(0x80,r);

34. r=receive();

35.//displaylcd(0x81,r);

36. r=receive();

37.//displaylcd(0x82,r);

38. r=receive();

39.//displaylcd(0x83,r);

40. r=receive();

41.//displaylcd(0x84,r);

42. r=receive();

43.//displaylcd(0x90,r);

44. r=receive();

45.//displaylcd(0x91,r);

46. r=receive();

47.//displaylcd(0x92,r);

48. r=receive();

49.//displaylcd(0x93,r);

50. r=receive();

51.//displaylcd(0x94,r);

52. r=receive();

53.//displaylcd(0x88,r);

54. r=receive();

55.//displaylcd(0x89,r);

56. r=receive();

57.//displaylcd(0x8a,r);

58. r=receive();

59.//displaylcd(0x8b,r);

60. r=receive();

61.//displaylcd(0x8c,r);

62. r=receive();

63. displaylcd(0x80,r);

64. r=receive();

65. displaylcd(0x90,r);

66. r=receive();

67.//displaylcd(0x90,r);

68. r=receive();

69.//displaylcd(0x9b,r);

70. r=receive();

71.//displaylcd(0x9c,r);

72. }

73.}

74./*

75.void dis(uchar p)

76.{ P0=0xFE;

77. switch(p)

78. { case 0x01: P1=0xC0;break;

79. case 0x02: P1=0xF9;break;

80. case 0x03: P1=0xA4;break;

81. case 0x04: P1=0xB0;break;

82. case 0x05: P1=0x99;break;

83. case 0x06: P1=0x92;break;

84. case 0x07: P1=0x82;break;

85. case 0x08: P1=0xF8;break;

86. default: P1=0x00;break;

87. }

88.}

89.void delay()

90.{ uchar i;

91. for(i=500;i>0;i--);

92.}

93.*/

UART.rar > pc_correspond

1.#include

2.#define uchar unsigned char

3.#define SLAVE 0xF1

4.#define BN 10

5.uchar idata rdata[10];

6.uchar idata tdata[10]={"abcdefghij"};

7.uchar receive(uchar addrs);

8.uchar send ();

9.void main(void)

10.{ uchar i;

11.for(i=0;i<10;i++)

12. TMOD=0x20;

13. TL1=0xE6; TH1=0xE6;

14. PCON=0x00;

16. SCON=0xE0;

17. receive(SLAVE);

18. send();

19.}

20.void error(void)

21.{ SBUF=0xFF;

22.while(TI!=1);

23. TI=0;

24.}

25.uchar receive(uchar addrs)

26.{ uchar i,p;

27.while(1)

28. { while(RI!=1);

29. RI=0;

30.if(SBUF!=addrs)

31. error();

32.else

33. {

34. TB8=0;

35. SBUF=0x00;

36.while(TI!=1); TI=0;

37.while(RI!=1); RI=0;

38. { while(1)

39. { p=0;

40.for(i=0;i

41.

51系列串口通讯例程

51系列串口通讯例程

51系列串口通讯例程

UART.rar > pc_mcu.c,

1.#include

2.#define uchar unsigned char

3.#define SLAVE 0xF1

4.void send();

5.main()

6.{ TMOD=0x20; //C/T为三,自动重装载

7. TH1=TL1=0xE5; //1200的初值

8. PCON=0x00; //smod=0

9. TR1=1;

10. SCON=0x50;

11.while(1){ //串口模式为1

12. send(); }

13.}

14.void send()

15.{

16. SBUF=0x59;

17.while(TI!=1);

18. TI=0;

20.}

UART.rar > 数显1.c,

1.#include

2.#define uchar unsigned char

3.float a,a0;

4.uchar i,j;

5.void delay();

6.void delay1();

7.void display(int t);

8.uchar code p[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

9.uchar code wei[]={0xC0,0xf9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98,0xFF};

10.void main()

11.{

12.while(1)

13. { a=40.87;

14. a0=a*10;

15. display(a0);

16. }

17.}

18.void display(int temp)

19.{

20. uchar dis[4];

21. temp=a0;

22. dis[0]=temp/1000;

23. dis[1]=(temp-dis[0]*1000)/100;

24. dis[2]=(temp-dis[0]*1000-dis[1]*100)/10;

25. dis[3]=temp%10;

26.for(i=0;i<3;i++)

27. { P0=p[i];

28. j=3-i;

29.if(j==2) P1=wei[dis[j]]&0x7F;

30.else P1=wei[dis[j]];

31. delay();

32. }

33. delay();

34.}

35.void delay(void)

36.{

37. unsigned char m,s;

38.for(m=20;m>0;m--)

39.for(s=150;s>0;s--);

40.}

相关推荐
相关主题
热门推荐