单片机控制洗衣机C程序设计
扫描二维码
随时随地手机看文章
#include
#define waterin P1_6
#define waterout P1_5
#define swim P1_7
bit flag;
char Count,t,waterinflag,wateroutflag,swimflag; // 用于中断程序的计数
unsigned char second;
void Timeinit(void)
{
//秒表初始化
Count=0; t=0; second=0; flag=0;
// 开定时器1,设定为工作方式2, 让定时中断时自动加载定时的初值
IE=0x8a; //允许中断,选中T0,T1中断
TMOD=0x21; //定时器0,使用TR0启动方式,选中工作方式1,定时器1,使用TR1启动方式,工作方式2
TH1=0x06; //设置定时初值 256-250=6 (250us,要实现10ms就要40次)
TL1=0x06;
TR1=1;
}
//用T1定时的秒表中断程序,工作方式2
void Miao(void) interrupt 3
{
Count++;
if(Count==40){
Count=0;
t++; //百分秒
if(t==100){
flag=1;
t=0;
if(second==10) second=0;
else second++;
}
}
}
void Initcover() { //盖子的中断程序
EA=1;
ET1=0;
EX1=1;
}
void coverinterrupt() interrupt 2 {
waterin=1;
waterout=1;
swim=1;
while (P3_3==0) ;
waterin=waterinflag;
waterout=wateroutflag;
swim=swimflag;
}
main() { //洗衣机工作过程
int i=0;
Initcover();
waterout=1;
wateroutflag=1;
swim=1;
swimflag=1;
P3_2=1;
if (P1_0==1) {
waterin=0;
waterinflag=0;
while(P3_2==1) {
waterin=1;
waterinflag=1;
for (i=0;i<10;i++) {
Timeinit();
while (second!=10) {
swim=0;
swimflag=0;
if(flag) {
flag=0;
P1=~(second*2);
waterin=waterinflag;
waterout=wateroutflag;
swim=swimflag;
}
}
P1=0xff;
waterin=waterinflag;
waterout=wateroutflag;
swim=swimflag;
Timeinit();
while (second!=3) {
swim=1;
swimflag=1;
if(flag) {
flag=0;
P1=~(second*2);
waterin=waterinflag;
waterout=wateroutflag;
swim=swimflag;
}
}
}
Timeinit();
while (second!=10 ) {
waterout=0;
wateroutflag=0;
if(flag) {
flag=0;
P1=~(second*2);
waterin=waterinflag;
waterout=wateroutflag;
swim=swimflag;
}
}
waterout=1;
wateroutflag=1;
}
}
}}