#include #include"lcd.h" #include"ds1302.h" #include"i2c.h" #include #include sbit LED0=P0^2; sbit LED1=P0^1; sbit LED2=P0^0; sbit LED3=P0^4; sbit LED4=P0^3; sbit key1 = P1^2; sbit key2 = P1^3; sbit IN = P3^7; sbit OUT = P3^6; struct UserInfo{ int num; int model; int start; int Light; }; struct UserInfo userInfo; void LcdDisplay(); void LcdDisplay1(); void Delay10ms(void); void Uart_Init() { TMOD = 0x20 | 0x01; SCON = 0x50; TH1 = 0xFD; TL1 = TH1; PCON = 0x00; EA = 1; ES = 1; TR1 = 1; } void us_delay(uchar t) { while(t--); } void Send_Uart(uchar value) { ES=0; TI=0; SBUF=value; while(TI==0); TI=0; ES=1; } void Bluetooth_Set(uchar *puf) { while(*puf!='\0') { Send_Uart(*puf); us_delay(5); puf++; } } void Detection() { int i = 0; if(key1==0) //��ⰴ��K1�Ƿ��� { Delay10ms(); //�������� if(key1==0){ if(userInfo.model == 0) userInfo.model = 1; else userInfo.model = 0; } while((i<50)&&(key1==0)){ Delay10ms(); i++; } i=0; } if(key2==0) //��ⰴ��K1�Ƿ��� { Delay10ms(); //�������� if(key2==0){ if(userInfo.start == 0) userInfo.start = 1; else userInfo.start = 0; } while((i<50)&&(key2==0)){ Delay10ms(); i++; } i=0; } if(IN==0) //��ⰴ��K1�Ƿ��� { Delay10ms(); //�������� if(IN==0){ userInfo.num++; if(userInfo.num > 99) userInfo.num = 99; } while((i<50)&&(IN==0)){ Delay10ms(); i++; } i=0; } if(OUT==0) //��ⰴ��K1�Ƿ��� { Delay10ms(); //�������� if(OUT==0){ userInfo.num--; if(userInfo.num < 0) userInfo.num = 0; } while((i<50)&&(OUT==0)){ Delay10ms(); i++; } i=0; } } void Dispose() { if(userInfo.start && userInfo.model){ LED0 = 0, LED1 = 0, LED2 = 0 ,LED3 = 0; }else{ if(userInfo.Light < 50 && userInfo.model == 0){ if(userInfo.num < 10 && userInfo.num > 0) LED0 = 0, LED1 = 0, LED2 = 0 ,LED3 = 1; else if(userInfo.num >= 10 && userInfo.num < 20) LED0 = 0, LED1 = 0, LED2 = 1 ,LED3 = 1; else if(userInfo.num >= 20 && userInfo.num < 30) LED0 = 0, LED1 = 1, LED2 = 1 ,LED3 = 1; else if(userInfo.num >= 30) LED0 = 1, LED1 = 1, LED2 = 1 ,LED3 = 1; else LED0 = 0, LED1 = 0, LED2 = 0 ,LED3 = 0; } } LED4 = userInfo.model; } void main() { int i = 0; uchar str[30]; LcdInit(); Ds1302Init(); Uart_Init(); while(1) { Ds1302ReadTime(); LcdDisplay(); Detection(); sprintf(str, "%d\r\n", userInfo.model); Bluetooth_Set(str); Dispose(); } } void LcdDisplay() { // TODO 显示时间 LcdWriteCom(0x80); LcdWriteData('m'); } void Delay10ms(void) //��� 0us { unsigned char a,b,c; for(c=1;c>0;c--) for(b=38;b>0;b--) for(a=130;a>0;a--); }