您现在的位置: 主页 > MCU > 单片机技术应用 > C51 队列 方式 中断接收 查询发送 -
本文所属标签:
为本文创立个标签吧:

C51 队列 方式 中断接收 查询发送 -

来源: 网络用户发布,如有版权联系网管删除 2018-09-06 

[导读]
Keil C51没有 《stdbool.h》头文件,自己做一个stdbool.h#ifndef__STDBOOL_H__#define__STDBOOL_H__typedefenum{false=0,true=1,}bool;#endif最简单的数组模拟循环队列Queue.h#ifndef__QUEUE__#define_

Keil C51没有 《stdbool.h》头文件,自己做一个

本文引用地址: http://www.21ic.com/app/mcu/201808/765805.htm


stdbool.h


  1. #ifndef__STDBOOL_H__

  2. #define__STDBOOL_H__

  3. typedefenum{

  4. false=0,

  5. true=1,

  6. }bool;

  7. #endif




最简单的数组模拟循环队列


Queue.h


  1. #ifndef__QUEUE__

  2. #define__QUEUE__

  3. #include

  4. #include"stdbool.h"

  5. #defineu8unsignedchar

  6. #defineMaxsize10

  7. typedefstruct{

  8. u8element[Maxsize];

  9. u8front;

  10. u8rear;

  11. }SeqCycleQueue;

  12. voidInit_Cycle_Queue(SeqCycleQueue*Q);

  13. boolEntry_Queue(SeqCycleQueue*Q,u8x);

  14. boolDelete_Queue(SeqCycleQueue*Q,u8*x);

  15. boolGet_front_value(SeqCycleQueue*Q,u8*x);

  16. boolIs_Queue_Full(SeqCycleQueue*Q);

  17. boolIs_Queue_Empty(SeqCycleQueue*Q);

  18. #endif





Queue.c


  1. #include"Queue.h"

  2. SeqCycleQueueQ;

  3. voidInit_Cycle_Queue(SeqCycleQueue*Q)

  4. {

  5. Q->front=0;

  6. Q->rear=0;

  7. }

  8. boolEntry_Queue(SeqCycleQueue*Q,u8x)

  9. {

  10. if((Q->rear+1)%Maxsize==Q->front)

  11. {

  12. returnfalse;

  13. }

  14. Q->element[Q->rear]=x;

  15. Q->rear=(Q->rear+1)%Maxsize;

  16. returntrue;

  17. }

  18. boolDelete_Queue(SeqCycleQueue*Q,u8*x)

  19. {

  20. if(Q->front==Q->rear)

  21. returnfalse;

  22. *x=Q->element[Q->front];

  23. Q->front=(Q->front+1)%Maxsize;

  24. returntrue;

  25. }

  26. boolGet_front_value(SeqCycleQueue*Q,u8*x)

  27. {

  28. if(Q->front==Q->rear)

  29. {

  30. returnfalse;

  31. }

  32. else

  33. {

  34. *x=Q->element[Q->front];

  35. returntrue;

  36. }

  37. }

  38. boolIs_Queue_Full(SeqCycleQueue*Q)

  39. {

  40. if((Q->rear+1)%Maxsize==Q->front)

  41. {

  42. returntrue;

  43. }

  44. else

  45. {

  46. returnfalse;

  47. }

  48. }

  49. boolIs_Queue_Empty(SeqCycleQueue*Q)

  50. {

  51. if(Q->front==Q->rear)

  52. {

  53. returntrue;

  54. }

  55. else

  56. {

  57. returnfalse;

  58. }

  59. }




main.c


  1. #include"Queue.h"

  2. volatileunsignedcharrx_data;

  3. externSeqCycleQueueQ;

  4. voidSend_Char(u8ch)

  5. {

  6. SBUF=ch;

  7. while(TI==0);

  8. TI=0;

  9. }

  10. //----------------------------------------------

  11. voidmain(void)

  12. {

  13. volatileunsignedchartmp=0;

  14. TMOD=0x20;//T1方式2

  15. TH1=0xFD;//Baud:9600bps@11.0592MHz

  16. TL1=0xFD;

  17. TR1=1;//启动定时器1

  18. SCON=0x50;//串口方式1,8-n-1,允许接收

  19. REN=1;//使能串口接收

  20. EA=1;//打开总中断

  21. ES=1;//打开串口中断开关

  22. Init_Cycle_Queue(&Q);

  23. while(1)

  24. {

  25. if(!Is_Queue_Empty(&Q))

  26. {

  27. Delete_Queue(&Q,&tmp);

  28. Send_Char(tmp);

  29. }

  30. }

  31. }

  32. //----------------------------------------------

  33. voidserial(void)interrupt4

  34. {

  35. if(RI)

  36. {

  37. rx_data=SBUF;

  38. //P1=rx_data;

  39. Entry_Queue(&Q,rx_data);

  40. RI=0;

  41. }

  42. }

  43. //----------------------------------------------




阅读 51 手册,发送缓冲与接收缓冲是独立的两个SBUF(虽然都对应 名字一样的SUBF 特殊功能寄存器)




              查看评论 回复



嵌入式交流网主页 > MCU > 单片机技术应用 > C51 队列 方式 中断接收 查询发送 -
 

"C51 队列 方式 中断接收 查询发送 -"的相关文章

网站地图

围观()