- #include "Queue.h"
-
- volatile unsigned char rx_data;
- extern SeqCycleQueue Q;
-
- void Send_Char(u8 ch)
- {
- SBUF = ch;
- while(TI == 0);
- TI = 0;
- }
-
-
- //----------------------------------------------
- void main (void)
- {
- volatile unsigned char tmp = 0;
- TMOD = 0x20; //T1方式2
- TH1 = 0xFD; //Baud:9600bps@11.0592MHz
- TL1 = 0xFD;
- TR1 = 1; //啟動定時器1
-
- SCON = 0x50; //串口方式1, 8-n-1, 允許接收
- REN = 1; //使能串口接收
- EA = 1; //打開總中斷
- ES = 1; //打開串口中斷開關
-
-
- Init_Cycle_Queue(&Q);
- while(1)
- {
- if(!Is_Queue_Empty(&Q))
- {
- Delete_Queue(&Q, &tmp);
- Send_Char(tmp);
- }
- }
- }
- //----------------------------------------------
- void serial(void) interrupt 4
- {
- if(RI)
- {
- rx_data = SBUF;
- //P1 = rx_data;
- Entry_Queue(&Q, rx_data);
- RI = 0;
- }
- }
復制代碼
- #include "Queue.h"
- SeqCycleQueue Q;
-
- void Init_Cycle_Queue(SeqCycleQueue *Q)
- {
- Q->front = 0;
- Q->rear = 0;
- }
-
- bool Entry_Queue(SeqCycleQueue *Q,u8 x)
- {
- if((Q->rear+1) % Maxsize == Q->front)
- {
- return false;
- }
- Q->element[Q->rear] = x;
- Q->rear = (Q->rear+1) % Maxsize;
- return true;
- }
- bool Delete_Queue(SeqCycleQueue *Q,u8 *x)
- {
- if(Q->front == Q->rear)
- return false;
- *x = Q->element[Q->front];
- Q->front = (Q->front+1) % Maxsize;
- return true;
- }
-
- bool Get_front_value(SeqCycleQueue *Q,u8 *x)
- {
- if(Q->front == Q->rear)
- {
- return false;
- }
- else
- {
- *x = Q->element[Q->front];
- return true;
- }
- }
-
- bool Is_Queue_Full(SeqCycleQueue *Q)
- {
- if((Q->rear+1) % Maxsize == Q->front)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- bool Is_Queue_Empty(SeqCycleQueue *Q)
- {
- if(Q->front == Q->rear)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
復制代碼
- #ifndef __QUEUE__
- #define __QUEUE__
- #include <REGX52.H>
- #include "stdbool.h"
-
-
- #define u8 unsigned char
- #define Maxsize 10
-
- typedef struct{
- u8 element[Maxsize];
- u8 front;
- u8 rear;
- }SeqCycleQueue;
-
-
- void Init_Cycle_Queue(SeqCycleQueue *Q);
- bool Entry_Queue(SeqCycleQueue *Q,u8 x);
- bool Delete_Queue(SeqCycleQueue *Q,u8 *x);
- bool Get_front_value(SeqCycleQueue *Q,u8 *x);
- bool Is_Queue_Full(SeqCycleQueue *Q);
- bool Is_Queue_Empty(SeqCycleQueue *Q);
-
-
- #endif
復制代碼- #ifndef __STDBOOL_H__
- #define __STDBOOL_H__
-
- typedef enum{
- false = 0,
- true = 1,
-
- }bool;
-
-
- #endif
復制代碼 |