/*************** *********************************************************************** * 串口通信控制 * 实现现象:下载程序后打开串口调试助手,将波特率设置为4800,根据串口接收到的数据执行不同命令。 注意事项:无。 ***************************************************************************************/ #include "reg52.h" //此文件中定义了单片机的一些特殊功能寄存器 #include "string.h" #include "intrins.h" typedef unsigned int u16; //对数据类型进行声明定义 typedef unsigned char u8; char jiesuo[5] = {'o','p','e','n','\0'}; char suoche[6] = {'c','l','o','s','\0'}; char receiveDatas[10]; sbit led1 = P2^0; sbit led2 = P2^1; sbit da = P1^3; sbit v = P1^5; sbit gnd = P1^2; int br=1; int i=0; int s=0; int t=0; int m=0; void Receive(char x); void result(); void delay500ms(); void delay1s(); sfr WDT_CONTR=0xe1; /******************************************************************************* * 函数名 :UsartInit() * 函数功能 :设置串口 * 输入 : 无 * 输出 : 无 *******************************************************************************/ void UsartInit() { SCON=0X50; //设置为工作方式1 TMOD=0X21; //设置计数器工作方式2 PCON=0X80; //波特率加倍 TH1=0XF4; //计数器初始值设置,注意波特率是4800的,晶振11.0592 TL1=0XF4; ES=1; //打开接收中断 EA=1; //打开总中断 TR1=1; //打开计数器 ET0 = 1; TR0 = 1; TH0 = 0x4C; TL0 = 0x00; WDT_CONTR=0x35; } /******************************************************************************* * 函 数 名 : main * 函数功能 : 主函数 * 输 入 : 无 * 输 出 : 无 *******************************************************************************/ void main() { UsartInit(); // 串口初始化 v=1; da=1; gnd=0; k1=0; k2=0; k3=0; k4=0; while(1) { delay1s(); delay500ms(); WDT_CONTR=0x35; }; } /******************************************************************************* * 函数名 : Usart() interrupt 4 * 函数功能 : 串口通信中断函数 * 输入 : 无 * 输出 : 无 *******************************************************************************/ void Usart() interrupt 4 { u8 receiveData; receiveData=SBUF;//出去接收到的数据 Receive(receiveData); RI = 0;//清除接收中断标志位 SBUF=receiveData;//将接收到的数据放入到发送寄存器 while(!TI); //等待发送数据完成 TI=0; //清除发送完成标志位 } /************************************************** 将接收到的字符存入字符串 ***************************************************/ void Receive(char x) { receiveDatas[i]=x ; i++; if(i==5){ i=0; result(); } } /******************************************* 对比字符串执行命令 *********************************/ void result() { int a= strcmp(receiveDatas,jiesuo) ; int b= strcmp(receiveDatas,suoche); int c= strcmp(receiveDatas,qixihuo); if(a==0) {led1=1; } if(b==0) { led2=1; } if(c==0) {led3=1; } } /*********************************************** 定时器中断 *****************************************/ void Timer0Interrupt(void) interrupt 1 { TH0 = 0x4C; TL0 = 0x00; s++; t++; if(s==10){ i=0; s=0; } if(t==30) { WDT_CONTR=0x35; t=0; } if(da==0) { if (br==1) { m++; if(m==30000) { k4=1; delay1s(); delay500ms(); WDT_CONTR=0x35; delay500ms(); delay1s(); k1=1; br=0; delay500ms(); k1=0; WDT_CONTR=0x35; delay1s(); delay500ms(); WDT_CONTR=0x35; delay500ms(); delay1s(); WDT_CONTR=0x35; k4=0; } } } else { br=1; m=0; } //add your code here! } /********************************************* 延时0.5秒 和延时1秒 9秒 *******************************************/ void delay1s() //误差 -0.000000000227us { unsigned char a,b,c; for(c=13;c>0;c--) for(b=247;b>0;b--) for(a=142;a>0;a--); _nop_(); //if Keil,require use intrins.h } void delay500ms() //误差 -0.000000000114us { unsigned char a,b,c; for(c=98;c>0;c--) for(b=127;b>0;b--) for(a=17;a>0;a--); _nop_(); //if Keil,require use intrins.h } |