智能洗衣机程序.docx
《智能洗衣机程序.docx》由会员分享,可在线阅读,更多相关《智能洗衣机程序.docx(29页珍藏版)》请在冰豆网上搜索。
智能洗衣机程序
#include
#include
#include
#include
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharCurrenttemp=0;
uintsec;
ucharwaterflag;
uchardoorflag;
ucharwaterwashflag,displaymode;
sbitLED1=P2^2;//指示灯1
sbitLED2=P2^3;//指示灯2
sbitLED3=P2^4;//指示灯3
sbitLED4=P3^7;//指示灯4
sbitrelay1=P2^0;//模拟洗衣机进水继电器1
sbitrelay2=P2^1;//模拟洗衣机出水继电器2
sbitbee=P1^2;//蜂鸣器
ucharsettime;//设置显示时间
ucharwash_time,wash_time1;//设置清洗时间
ucharTs_time;//设置脱水时间
ucharPw_times;//设置漂洗次数
ucharTw_times;//设置脱水次数
ucharstep0;
ucharstep1;//洗衣机第1步进水,打开进水继电器
ucharstep2;//洗衣机第2步洗衣,关闭进水继电器,电机2S正反转
ucharstep3;//洗衣机第3步漂洗水,进水继电器打开,电机正转
ucharstep4;//洗衣机第4步重复漂洗次数,进水继电器打开,电机正转
ucharstep5;//洗衣机第5步脱水水,打开出水继电器,电机正转。
ucharB_TS_Time;
ucharB_Tw_times;
ucharjinshui_times=10;
voidbeep();
voidDelayMS(uintms);
uchardisplay1[]={"Tw=000secFw=0cs"};//显示时间(单位秒):
Tw清洗时间,Tr脱水时间2个时间值显示效果
uchardisplay2[]={"Tr=000secFr=0cs"};//Fw漂洗次数,Fr脱水次数
#defineMAXCHAR50
ucharaa[MAXCHAR];
ucharnum;
voidkeyhandle();
//注意,无论接收到信号还是发送完信号,都会进中断服务程序的
/*初始化程序(必须使用,否则无法收发),次程序将会使用定时器1*/
voidSerialInti()//初始化程序(必须使用,否则无法收发)
{
TMOD=0x22;//定时器1操作模式2:
8位自动重载定时器
TH1=0xfd;//装入初值,波特率9600
TL1=0xfd;
EA=1;//开总中断
ES=1;//开串行口中断
}
/*voidUart1Send(ucharc)
{
SBUF=c;
while(!
TI);//等待发送完成信号(TI=1)出现
TI=0;
}*/
//串行口连续发送char型数组,遇到终止号/0将停止
voidUart1Sends(uchar*str)
{
while(*str!
='\0')
{
SBUF=*str;
while(!
TI);//等待发送完成信号(TI=1)出现
TI=0;
str++;
}
}
voidbeep()
{
bee=0;
DelayMS(100);
bee=1;
DelayMS(100);
}
voidmain()
{
//定时器0初始化
bee=1;
//TMOD=0X01;
//TH0=(65535-5000)/256;
//TL0=(65535-5000)%256;
//TR0=1;
//IE=0x8a;
//液晶初始化
LCD_init();
DelayMS(100);
//变量初始化
wash_time=40;//漂洗时间40S;
Ts_time=20;//脱水时间20S;
Pw_times=1;//设置洗次数1
Tw_times=1;//设置脱水次数1
step1=0;
step2=0;
step3=0;
step4=0;
step5=0;
step0=0;
relay1=0;
relay2=0;
bee=1;
LED1=0;
SerialInti();
Uart1Sends("AT\r\n");
while
(1)
{
keyhandle();
timer0_serv();
//液晶显示参数程序
if(step1==1)
{
LCD_disp_char(4,1,wash_time1/10+'0');
//DelayMS(100);
LCD_disp_char(5,1,wash_time1%10+'0');
}
else
{
LCD_disp_char(4,1,wash_time/10+'0');
//DelayMS(100);
LCD_disp_char(5,1,wash_time%10+'0');
}
//DelayMS(100);
if(step2==1)
{
LCD_disp_char(4,2,B_TS_Time/10+'0');
LCD_disp_char(5,2,B_TS_Time%10+'0');
}
else
{
LCD_disp_char(4,2,Ts_time/10+'0');
LCD_disp_char(5,2,Ts_time%10+'0');
}
//DelayMS(100);
LCD_disp_char(13,1,Pw_times%10+'0');
DelayMS(8);
LCD_disp_char(13,2,Tw_times%10+'0');
if(step0==1)//进水
{
sec++;
if(sec>=100)
{
sec=0;
LED1=!
LED1;
relay1=0;
relay2=1;
jinshui_times--;
if(jinshui_times==0)
{
jinshui_times=10;
sec=0;
step1=1;
step0=0;
IN1=0;
IN2=1;
wash_time1=wash_time;
}
}
}
if(step1==1)//洗衣
{
sec++;
if(sec>=100)
{
sec=0;
LED2=!
LED2;
LED1=1;
LED3=1;
LED4=1;
IN1=!
IN1;
IN2=!
IN2;
wash_time1--;
if(wash_time1==0)
{
sec=0;
Pw_times--;
if(Pw_times==0)
{
step2=1;
step1=0;
IN1=1;
IN2=0;
B_TS_Time=Ts_time;
}
else
{
wash_time1=wash_time;
}
}
}
}
if(step2==1)//脱水
{
sec++;
LED1=1;
LED2=1;
LED4=1;
if(sec>=100)
{
sec=0;
LED3=!
LED3;
relay1=1;
relay2=0;
B_TS_Time--;
IN1=1;
IN2=0;
if(B_TS_Time==0)
{
B_TS_Time=99;
Tw_times--;
if(Tw_times==0)
{
sec=0;
step2=0;
step3=0;
relay1=1;
relay2=1;
IN1=0;
IN2=0;
beep();
beep();
beep();
}
else
{
B_TS_Time=Ts_time;
}
LED1=1;
LED2=1;
LED3=1;
LED4=1;
bee=1;
}
}
}
if(aa[0]=='1')
{
startkeypress_flag=1;
aa[0]=0xff;
}
elseif(aa[0]=='2')
{
stopkeypress_flag=1;
aa[0]=0xff;
}
elseif(aa[0]=='3')
{
zerokeypress_flag=1;
aa[0]=0xff;
}
elseif(aa[0]=='4')
{
guangbiaokeypress_flag=1;
aa[0]=0xff;
}
}
}
//按键检测,检测到有按键按下
voidkeyhandle()
{
if(startkeypress_flag==1)//设置按键按下,切换设置参数
{
startkeypress_flag=0;
beep();
LED4=0;
displaymode++;
if(displaymode>=5)
{
displaymode=0;
LED4=1;
}
}
if(stopkeypress_flag==1)//参数加+
{
stopkeypress_flag=0;
beep();
if(displaymode==1)
{
wash_time=wash_time+5;//设定值++;
if(wash_time>=100)
{
wash_time=0;
}
}
elseif(displaymode==2)
{
LED4=0;
if(Pw_times<10)
Pw_times=Pw_times+1;//设定值++;
if(Pw_times>=9)
{
Pw_times=0;
}
}
elseif(displaymode==3)
{
LED4=0;
if(Ts_time<100)
Ts_time=Ts_time+5;//设定值++;
if(Ts_time>=100)
{
Ts_time=0;
}
}
elseif(displaymode==4)
{
LED4=0;
if(Tw_times<100)
Tw_times=Tw_times+1;//设定值++;
if(Tw_times>=9)
{
Tw_times=0;
}
}
}
if(zerokeypress_flag==1)
{
zerokeypress_flag=0;
beep();
if(displaymode==1)
{
LED4=0;
wash_time=wash_time-5;//设定值-5;
if(wash_time<=0)
{
wash_time=99;
}
}
elseif(displaymode==2)
{
LED4=0;
Pw_times=Pw_times-1;//设定最小温度值++;
if(Pw_times<=0)
{
Pw_times=1;
}
}
elseif(displaymode==3)
{
LED4=0;
Ts_time=Ts_time-5;//设定值-5;
if(Ts_time<=0)
{
Ts_time=99;
}
}
elseif(displaymode==4)
{
LED4=0;
Tw_times=Tw_times-1;//设定最小温度值++;
if(Tw_times<=0)
{
Tw_times=1;
}
}
}
if(guangbiaokeypress_flag==1)
{
guangbiaokeypress_flag=0;
beep();
IN1=0;
IN2=0;//洗衣机电机转动
step0=1;
relay1=1;
relay2=1;
}
}
/*串行通讯中断,收发完成将进入该中断*/
voidSerial_interrupt()interrupt4
{
if(RI==1)
{
//LED4=!
LED4;
//LED3=!
LED3;
RI=0;//软件清除接收中断
aa[0]=SBUF;//命令存到命令数组
num++;
if(num>50)
{
num=0;
}
LED1=!
LED1;
startkeypress_flag=1;
guangbiaokeypress_flag=1;
}
}
voidDSY_Refresh()interrupt1
{
EA=0;
TH0=(65535-10000)/256;
TL0=(65535-10000)%256;
if(step0==1)//进水
{
sec++;
if(sec>=1000)
{
sec=0;
LED1=!
LED1;
jinshui_times--;
if(jinshui_times==0)
{
jinshui_times=10;
sec=0;
step1=1;
step0=0;
IN1=0;
IN2=1;
wash_time1=wash_time;
}
}
}
if(step1==1)//洗衣
{
sec++;
if(sec>=1000)
{
sec=0;
relay1=1;
relay2=0;
LED2=!
LED2;
LED1=1;
LED3=1;
LED4=1;
IN1=!
IN1;
IN2=!
IN2;
wash_time1--;
if(wash_time1==0)
{
sec=0;
Pw_times--;
if(Pw_times==0)
{
step2=1;
step1=0;
IN1=0;
IN2=1;
B_TS_Time=Ts_time;
}
else
{
wash_time1=wash_time;
}
}
}
}
if(step2==1)//脱水
{
sec++;
LED1=1;
LED2=1;
LED3=!
LED3;
LED4=1;
if(sec>=1000)
{
sec=0;
relay1=0;
relay2=1;
B_TS_Time--;
IN1=0;
IN2=1;
if(B_TS_Time==0)
{
B_TS_Time=99;
Tw_times--;
if(Tw_times==0)
{
sec=0;
step2=0;
step3=0;
relay1=0;
relay2=0;
IN1=0;
IN2=0;
}
else
{
B_TS_Time=Ts_time;
}
LED1=1;
LED2=1;
LED3=1;
LED4=1;
}
}
}
//if(step4==1)//洗衣
//{
//sec++;
//sec++;
//if(sec>=1000)
//{
//sec=0;
//Ts_time--;
//IN1=!
IN1;
//IN2=!
IN2;
//if(Ts_time==0)
//{
//Ts_time=99;
//sec=0;
//step2=0;
//step3=1;
//}
//}
//}
EA=1;
}
voidDelayMS(uintms)//延时子程序
{
uchari;
while(ms--)
{
for(i=0;i<120;i++);
}
}
#include
#defineLCD_DBP0
sbitLCD_RS=P1^3;//P2^0是p2.0的意思;LCD_RS
sbitLCD_RW=P1^4;//P2^1是p2.1的意思
sbitLCD_E=P1^5;//P2^2是p2.2的意思
/******定义函数****************/
#defineucharunsignedchar
#defineuintunsignedint
voidLCD_init(void);//初始化函数
voidLCD_write_command(ucharcommand);//写指令函数
voidLCD_write_data(uchardat);//写数据函数
voidLCD_disp_char(ucharx,uchary,uchardat);//在某个屏幕位置上显示一个字符,X(0-15),y(1-2)
voidLCD_disp_str(ucharx,uchary,uchar*str);//LCD1602显示字符串函数
voiddelay_n10us(uintn);//延时函数
/*--------------------------------------
;模块名称:
LCD_init();
;-------------------------------------*/
voidLCD_init(void)
{
delay_n10us(10);
LCD_write_command(0x38);//设置8位格式,2行,5x7
delay_n10us(10);
LCD_write_command(0x0c);//整体显示,关光标,不闪烁
delay_n10us(10);
LCD_write_command(0x06);//设定输入方式,增量不移位
delay_n10us(10);
LCD_write_command(0x01);//清除屏幕显示
delay_n10us(100);//延时清屏,延时函数,延时约n个10us
}
/*--------------------------------------
;模块名称:
LCD_write_command();
;功能:
LCD1602写指令函数
;-------------------------------------*/
voidLCD_write_command(uchardat)
{
delay_n10us(10);
LCD_RS=0;//指令
LCD_RW=0;//写入
LCD_E=1;//允许
LCD_DB=dat;
delay_n10us(10);//实践证明,我的LCD1602上,用for循环1次就能完成普通写指令。
LCD_E=0;
delay_n10us(10);//实践证明,我的LCD1602上,用for循环1次就能完成普通写指令。
}
/*--------------------------------------
;模块名称:
LCD_write_data();
;功能:
LCD1602写数据函数
;-------------------------------------*/
voidLCD_write_data(uchardat)
{
delay_n10us(10);
LCD_RS=1;//数据
LCD_RW=0;//写入
LCD_E=1;//允许
LCD_DB=dat;
delay_n10us(10);
LCD_E=0;
delay_n10us(10);
}
/*--------------------------------------
;模块名称:
LCD_disp_char();
;功能:
LCD1602显示一个字符函数,在某个屏幕位置上显示一个字符,X(0-15),y(1-2)。
;-------------------------------------*/
voidLCD_disp_char(ucharx,uchary,uchardat)
{
ucharaddress;
if(y==1)
address=0x80+x;
else
address=0xc0+x;
LCD_write_command(address);
LCD_write_data(dat);
}
/*-