#include <reg52.h>bit flagOnceTxd = 0; //单次发送完成标志,即发送完一个字节bit cmdArrived = 0; //命令到达标志,即接收到上位机下发的命令unsigned char cntRxd = 0;unsigned char pdata bufRxd[40]; //串口接收缓冲区extern bit flagBuzzOn;extern void LcdShowStr(unsigned char x, unsigned char y, const unsigned char *str);extern void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len);void ConfigUART(unsigned int baud) //串口配置函数,baud为波特率{ SCON = 0x50; //配置串口为模式1 TMOD &= 0x0F; //清零T1的控制位 TMOD |= 0x20; //配置T1为模式2 TH1 = 256 - (11059200/12/32) / baud; //计算T1重载值 TL1 = TH1; //初值等于重载值 ET1 = 0; //禁止T1中断 ES = 1; //使能串口中断 TR1 = 1; //启动T1}unsigned char UartRead(unsigned char *buf, unsigned char len) //串口数据读取函数,数据接收指针buf,读取数据长度len,返回值为实际读取到的数据长度{ unsigned char i; if (len > cntRxd) //读取长度大于接收到的数据长度时, { len = cntRxd; //读取长度设置为实际接收到的数据长度 } for (i=0; i<len; i++) //拷贝接收到的数据 { *buf = bufRxd[ i]; buf++; } cntRxd = 0; //清零接收计数器 return len; //返回实际读取长度}void UartWrite(unsigned char *buf, unsigned char len) //串口数据写入函数,即串口发送函数,待发送数据指针buf,数据长度len{ while (len--) { flagOnceTxd = 0; SBUF = *buf; buf++; while (!flagOnceTxd); }}bit CmdCompare(unsigned char *buf, const unsigned char *cmd) //命令比较函数,缓冲区数据与指定命令比较,相同返回1,不同返回0{ while (*cmd != '\0') { if (*cmd != *buf) //遇到不相同字符时即刻返回0 { return 0; } else //当前字符相等时,指针递增准备比较下一字符 { cmd++; buf++; } } return 1; //到命令字符串结束时字符都相等则返回1}void UartDriver() //串口驱动函数,检测接收到的命令并执行相应动作{ unsigned char i; unsigned char len; unsigned char buf[30]; const unsigned char code cmd0[] = "buzz on"; const unsigned char code cmd1[] = "buzz off"; const unsigned char code cmd2[] = "showstr "; const unsigned char code *cmdList[] = {cmd0, cmd1, cmd2}; if (cmdArrived) //有命令到达时,读取处理该命令 { cmdArrived = 0; for (i=0; i<sizeof(buf); i++) //清零命令接收缓冲区 { buf[ i] = 0; } len = UartRead(buf, sizeof(buf)); //将接收到的命令读取到缓冲区中 for (i=0; i<sizeof(cmdList)/sizeof(cmdList[0]); i++) //与所支持的命令列表逐一进行比较 { if (CmdCompare(buf, cmdList[ i]) == 1) //检测到相符命令时退出循环,此时的i值就是该命令在列表中的下标值 { break; } } switch (i) //根据比较结果执行相应命令 { case 0: flagBuzzOn = 1; //开启蜂鸣器 break; case 1: flagBuzzOn = 0; //关闭蜂鸣器 break; case 2: buf[len] = '\0'; //为接收到的字符串添加结束符 i = sizeof(cmd2) - 1; LcdShowStr(0, 0, buf+i); //显示字符串 i = len - i; //计算有效字符个数 if (i < 16) //有效字符少于16时,清楚液晶上的后续字符位 { LcdAreaClear(i, 0, 16-i); } break; default: //i大于命令列表最大下标时,即表示没有相符的命令,给上机发送“错误命令”的提示 UartWrite("bad command.\r\n", sizeof("bad command.\r\n")-1); return; } buf[len++] = '\r'; //有效命令被执行后,在原命令帧之后添加回车换行符后返回给上位机,表示已执行 buf[len++] = '\n'; UartWrite(buf, len); }}void UartRxMonitor(unsigned char ms) //串口接收监控函数{ static unsigned char cntbkp = 0; static unsigned char idletmr = 0; if (cntRxd > 0) //接收计数器大于零时,监控总线空闲时间 { if (cntbkp != cntRxd) //接收计数器改变,即刚接收到数据时,清零空闲计时 { cntbkp = cntRxd; idletmr = 0; } else { if (idletmr < 30) //接收计数器未改变,即总线空闲时,累积空闲时间 { idletmr += ms; if (idletmr >= 30) //空闲时间超过30ms即认为一帧命令接收完毕 { cmdArrived = 1; //设置命令到达标志 } } } } else { cntbkp = 0; }}void InterruptUART() interrupt 4 //UART中断服务函数{if (RI) //接收到字节 { RI = 0; //手动清零接收中断标志位 if (cntRxd < sizeof(bufRxd)) //接收缓冲区尚未用完时, { bufRxd[cntRxd++] = SBUF; //保存接收字节,并递增计数器 } }if (TI) //字节发送完毕 { TI = 0; //手动清零发送中断标志位 flagOnceTxd = 1; //设置单次发送完成标志 }}/*************************main.c文件程序源代码**************************/
#include <reg52.h>
sbit BUZZ = P1^6; //蜂鸣器控制引脚
bit flagBuzzOn = 0; //蜂鸣器启动标志
unsigned char T0RH = 0; //T0重载值的高字节
unsigned char T0RL = 0; //T0重载值的低字节
void ConfigTimer0(unsigned int ms);
extern void LcdInit();
extern void ConfigUART(unsigned int baud);
extern void UartRxMonitor(unsigned char ms);
extern void UartDriver();
void main ()
{
EA = 1; //开总中断
ConfigTimer0(1); //配置T0定时1ms
ConfigUART(9600); //配置波特率为9600
LcdInit(); //初始化液晶
while(1)
{
UartDriver();
}
}
void ConfigTimer0(unsigned int ms) //T0配置函数
{
unsigned long tmp;
tmp = 11059200 / 12; //定时器计数频率
tmp = (tmp * ms) / 1000; //计算所需的计数值
tmp = 65536 - tmp; //计算定时器重载值
tmp = tmp + 18; //修正中断响应延时造成的误差
T0RH = (unsigned char)(tmp >> 8); //定时器重载值拆分为高低字节
T0RL = (unsigned char)tmp;
TMOD &= 0xF0; //清零T0的控制位
TMOD |= 0x01; //配置T0为模式1
TH0 = T0RH; //加载T0重载值
TL0 = T0RL;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
}
void InterruptTimer0() interrupt 1 //T0中断服务函数
{
TH0 = T0RH; //定时器重新加载重载值
TL0 = T0RL;
if (flagBuzzOn) //执行蜂鸣器鸣叫或关闭
BUZZ = ~BUZZ;
else
BUZZ = 1;
UartRxMonitor(1); //串口接收监控
}
/***********************lcd1602.c文件程序源代码*************************/
#include <reg52.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P1^0;
sbit LCD1602_RW = P1^1;
sbit LCD1602_E = P1^5;
void LcdWaitReady() //等待液晶准备好
{
unsigned char sta;
LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do
{
LCD1602_E = 1;
sta = LCD1602_DB; //读取状态字
LCD1602_E = 0;
} while (sta & 0x80); //bit7等于1表示液晶正忙,重复检测直到其等于0为止
}
void LcdWriteCmd(unsigned char cmd) //写入命令函数
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_E = 1;
LCD1602_DB = cmd;
LCD1602_E = 0;
}
void LcdWriteDat(unsigned char dat) //写入数据函数
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdShowStr(unsigned char x, unsigned char y, const unsigned char *str) //显示字符串,屏幕起始坐标(x,y),字符串指针str
{
unsigned char addr;
//由输入的显示坐标计算显示RAM的地址
if (y == 0)
addr = 0x00 + x; //第一行字符地址从0x00起始
else
addr = 0x40 + x; //第二行字符地址从0x40起始
//由起始显示RAM地址连续写入字符串
LcdWriteCmd(addr | 0x80); //写入起始地址
while (*str != '\0') //连续写入字符串数据,直到检测到结束符
{
LcdWriteDat(*str);
str++;
}
}
void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len) //区域清除,清除从(x,y)坐标起始的len个字符位
{
unsigned char addr;
//由输入的显示坐标计算显示RAM的地址
if (y == 0)
addr = 0x00 + x; //第一行字符地址从0x00起始
else
addr = 0x40 + x; //第二行字符地址从0x40起始
//由起始显示RAM地址连续写入字符串
LcdWriteCmd(addr | 0x80); //写入起始地址
while (len--) //连续写入空格
{
LcdWriteDat(' ');
}
}
void LcdInit() //液晶初始化函数
{
LcdWriteCmd(0x38); //16*2显示,5*7点阵,8位数据接口
LcdWriteCmd(0x0C); //显示器开,光标关闭
LcdWriteCmd(0x06); //文字不动,地址自动+1
LcdWriteCmd(0x01); //清屏
}