当前位置:首页 > 单片机 > 单片机
[导读] 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》头文件,自己做一个


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 特殊功能寄存器)


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

空气断路器的作用是什么

关键字: 断路器 方式

无人机是怎样起飞升空的?

关键字: 无人机 方式

近日据外媒报道,发表在《美国医学会杂志》上的一项新研究表明,与从不吸烟的人相比,每天一包烟抽20年的重度吸烟者在戒烟后10至15年内患心血管疾病的风险仍然更高。 据了解,戒烟对健康的一些影响马上开始显

关键字: 方式 抽烟 戒烟

题意:     给定红绿两种方块,要求以1,2,3..n的方式,一层一层垒上去,且任意一层只能是单色,问在该种方式下垒到最高高度的方式有多少种?解题:     因为是1,2,3..所以只要刚刚好方块够

关键字: 队列 滚动数组

开关电源的输入正常有滤波器来减小电源反馈到输入的纹波,输入滤波器普通有电容和电感组成&prod形滤波器,图1. 和图2. 分辨为典范的ac/dc电源输入电路和dc/dc电源输入电路。

关键字: 开关电源 方式 电流 电源技术解析

/******************************** 基于队列的Mega8UART通信驱动程序 文件名:uart.c 编译:WinAVR-20070122 硬件:CA-M8X 时钟:外部4MH...

关键字: uart 通信模块 队列

一、方式0: 二、方式1 三、方式2 四、方式3

关键字: 单片机 工作 方式
关闭
关闭