#include "drive_1602.h" #include "string.h" #include "stdio.h" #define uint unsigned int #define uchar unsigned char /* 采用5V USB电路供电 可以实现语音停止和开启风扇 实现语音调速风扇 调速等级高低各3级 */ sbit SW1=P3^7; sbit SW2=P3^6; sbit SW3=P3^5; sbit fun=P3^3;//风扇io uchar temp,humi; int JCount=0; void delay_xms(int xms) { char ix=0; for(;xms>0;xms--) for(ix=110;ix>0;ix--); } uchar SET=0; bit keyflag=0; bit openflag=0;//开启标志位 uchar dw_val=3;//挡位值 int pwm_cnt=0; #define pwm_max 1000 int set_pwm=pwm_max; void KeyRead(void) { if(!SW1) { if(!SW1&&keyflag==0) { keyflag=1; openflag^=1; while(!SW1); } } else if(!SW2) { if(!SW2&&keyflag==0) { keyflag=1; if(++dw_val>3)dw_val=1; while(!SW2); } } else if(!SW3) { if(!SW3&&keyflag==0) { keyflag=1; if(dw_val>1)dw_val--; } } else { keyflag=0; } } void Timer0Init(void) //10毫秒@11.0592MHz { TMOD &= 0xF0; //设置定时器模式 TMOD |= 0x01; //设置定时器模式 TL0 = 0x00; //设置定时初值 TH0 = 0xDC; //设置定时初值 TF0 = 0; //清除TF0标志 ET0=1; TR0 = 1; //定时器0开始计时 EA=1; } uchar get_key=0; void TimeISR()interrupt 1 { TL0 = 0x66; //设置定时初值 TH0 = 0xFC; //设置定时初值 1ms if(++get_key>10) { get_key=0; KeyRead(); } pwm_cnt++; if(pwm_cnt>=pwm_max) { pwm_cnt=0; } if(pwm_cnt>set_pwm) { fun=1; } else { fun=0; } JCount++; } void UartInit(void) //9600bps@11.0592MHz { PCON &= 0x7F; //波特率不倍速 SCON = 0x50; //8位数据,可变波特率 TMOD &= 0x0F; //清除定时器1模式位 TMOD |= 0x20; //设定定时器1为8位自动重装方式 TL1 = 0xFD; //设定定时初值 TH1 = 0xFD; //设定定时器重装值 ET1 = 0; //禁止定时器1中断 TR1 = 1; //启动定时器1 EA=1; ES=0; } void sendData(char *p,unsigned char n) { if( p == 0) return ; ES = 0; if(n > 0) { while(n --) { SBUF = *p++ ; while(!TI) ; TI = 0 ; } } // ES = 1; } // 往串口发送字符串 void sendString(char *p) { if(p == 0) return ; sendData(p,strlen(p)); } uchar rxdat=0; // 接收中断函数 void usart() interrupt 4 { if(RI == 1) { rxdat=SBUF; switch(rxdat) { case 0x5a: openflag^=1; break; case 0x65: if(++dw_val>3)dw_val=1; break; case 0x78: if(dw_val>1)dw_val--; break; } // setUsartRxData(SBUF); } RI = 0; TI = 0; } void main() { lcd1602_init(); UartInit(); delay_xms(200); Timer0Init(); while(1) { if(JCount>500)//500ms采集一次 { JCount=0; if(openflag) { L1602_printf(0,0,"FUN:ON"); switch(dw_val) { case 1: set_pwm=300; break; case 2: set_pwm=700; break; case 3: set_pwm=pwm_max; break; } }else { L1602_printf(0,0,"FUN:OFF"); } L1602_printf(0,1,"Gears:%d",dw_val); } } }