Keil C51没有 《stdbool.h》头文件,自己做一个
stdbool.h
#ifndef __STDBOOL_H__
#define __STDBOOL_H__
typedef enum{
false = 0,
true = 1,
}bool;
#endif
最简单的数组模拟循环队列
Queue.h
#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
Queue.c
#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;
}
}
main.c
#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;
}
}
//----------------------------------------------
阅读 51 手册,发送缓冲与接收缓冲是独立的两个SBUF(虽然都对应 名字一样的SUBF 特殊功能寄存器)
很遗憾,但就是没实现中断接收与中断发送的实验。