A42 - Design of washing machine based on 51 single chip microcomputer

Task

Based on the 51 single-chip microcomputer, simulate the whole process of the washing machine to complete the following:

  1. Power supply circuit, designed to meet the requirements of the power input
  2. Washing machine weight detection module to detect the weight of clothes in the drum
  3. Water level detection, used to control the amount of water added to the washing machine
  4. Washing machine lid detection for safety detection to prevent accidents
  5. Button control, switch between multiple laundry modes
  6. LED digital tube displays the remaining washing time
  7. LED Status Knowledge Current Mode
  8. Buzzer alarm, when there is an abnormal situation such as the lid of the washing machine is opened halfway
  9. Inlet and outlet water control, relay solenoid valve
  10. Washing Motor Control

design framework

insert image description here

insert image description here

simulation

insert image description here
insert image description here

source program

/*******************************************************************************

* 文件名称:基于51单片机的洗衣机设计

* 实验目的:1.

* 2.

* 程序说明:完整程序Q:2772272579;@: [email protected]

* 日期版本:本项目分享关键细节,熟悉使用单片机的可做参考代码。完整讲解+源代码工程可联系获取,可定制。

*******************************************************************************/
#include "config.h"
#include "adc0832.h" //AD转换库函数

/*******************************************
实现对洗衣机整个洗衣过程的控制,包括进水、洗涤、漂洗、排水、脱水五个过程。
标准:洗涤60秒,漂洗20秒,脱水10秒。---90秒
快速:洗涤40秒,漂洗20秒,脱水10秒。---70秒
轻柔:洗涤30秒,漂洗15秒,脱水10秒。---55秒

洗涤和漂洗:电机正转3秒,暂停2秒,反转3秒

*******************************************/
#define MODE_NORMAL 0
#define MODE_FAST   1
#define MODE_SOFT   2
uchar mode=MODE_NORMAL;
uchar start_step=0;
char Sec_Now=90;
bit is_1s=0;
bit is_gaiziOpen=0;

void keyscan(void);
void display(uchar Num);	
void CleatStop(void);
void motor_control01(void);
void motor_control02(void);

sbit LED_M0=P3^4;
sbit LED_M1=P3^5;
sbit LED_M2=P3^6;

sbit IN_WATER=P2^0;
sbit OUT_WATER=P2^1;

sbit BEEP=P2^2;

sbit LED_01=P3^0;
sbit LED_02=P3^1;
sbit LED_03=P2^5;
sbit LED_04=P3^3;

sbit MOTOR_IN1=P1^3;
sbit MOTOR_IN2=P1^4;
sbit MOTOR_EA=P2^6;

sbit M_GAIZI=P3^2;

uint adc0_Weight,adc1_WaterLevel;
void Timer0_Init(void)
{
    
    
	TMOD=0x01;		 //模式设置,00000001,可见采用的是定时器0,工作与模式1(M1=0,M0=1)。
	TR0=0;			 //定时器
	TH0=(65536-50000)/256; //设置定时器为50MS中断一次
	TL0=(65536-50000)%256;
	ET0=1;			 //开定时器0中断
	EA=1;			 //开总中断
}

uchar count_end=0;
void main()
{
    
    
	CleatStop();
	Timer0_Init();
	IT0 = 1;                        //set INT0 int type (1:Falling 0:Low level)
  EX0 = 1;                        //enable INT0 interrupt
	while(1)
	{
    
    	
		display(Sec_Now);
		if(is_gaiziOpen==0)
		{
    
    
			keyscan();
			if(start_step==1)
			{
    
    
				adc0_Weight = GetAD0832(0);
				display(Sec_Now);
				adc1_WaterLevel = GetAD0832(1);
				while(adc1_WaterLevel <= adc0_Weight)
				{
    
    
					display(Sec_Now);
					adc1_WaterLevel = GetAD0832(1);
					IN_WATER=0; //进水打开
					LED_01=1;
					LED_02=1;
					LED_03=1;
					LED_04=1;
					if(M_GAIZI==0)
					{
    
    
						TR0 = 1;
						if(is_1s)
						{
    
    
							is_1s=0;
							BEEP = !BEEP;
						}
					}	
					else
					{
    
    
						TR0 = 0;
						BEEP = 1;
						is_gaiziOpen = 0;
					}
				}
				IN_WATER=1; //进水关闭
				
				LED_01=0;   //洗涤模式
				LED_02=1;
				LED_03=1;
				LED_04=1;
				
				TR0 = 1;
				start_step=2;		
			}
			else if(start_step==2)  //洗涤模式
			{
    
    
				motor_control01();
				switch(mode){
    
    
					case MODE_NORMAL:
						if(Sec_Now<=30){
    
    start_step=3;LED_01=1;LED_02=0;LED_03=1;LED_04=1;OUT_WATER=1;continue;}
						break;
					case MODE_FAST:
						if(Sec_Now<=30){
    
    start_step=3;LED_01=1;LED_02=0;LED_03=1;LED_04=1;OUT_WATER=1;continue;}
						break;
					case MODE_SOFT:
						if(Sec_Now<=25){
    
    start_step=3;LED_01=1;LED_02=0;LED_03=1;LED_04=1;OUT_WATER=1;continue;}
						break;
					default:break;
				}	
			}
			else if(start_step==3)  //漂洗模式
			{
    
    
				motor_control01();
				switch(mode){
    
    
					case MODE_NORMAL:
						if(Sec_Now<=10){
    
    start_step=4;LED_01=1;LED_02=1;LED_03=0;LED_04=1;OUT_WATER=0;continue;}
						break;
					case MODE_FAST:
						if(Sec_Now<=10){
    
    start_step=4;LED_01=1;LED_02=1;LED_03=0;LED_04=1;OUT_WATER=0;continue;}
						break;
					case MODE_SOFT:
						if(Sec_Now<=10){
    
    start_step=4;LED_01=1;LED_02=1;LED_03=0;LED_04=1;OUT_WATER=0;continue;}
						break;
					default:break;
				}	
			}
			else if(start_step==4)  //脱水模式
			{
    
    
				OUT_WATER=0;
				motor_control02();
				switch(mode){
    
    
					case MODE_NORMAL:
						if(Sec_Now<0){
    
    start_step=5;LED_01=1;LED_02=1;LED_03=1;LED_04=0;BEEP = 0;count_end=0;is_1s = 0;OUT_WATER=1;continue;}
						break;
					case MODE_FAST:
						if(Sec_Now<0){
    
    start_step=5;LED_01=1;LED_02=1;LED_03=1;LED_04=0;BEEP = 0;count_end=0;is_1s = 0;OUT_WATER=1;continue;}
						break;
					case MODE_SOFT:
						if(Sec_Now<0){
    
    start_step=5;LED_01=1;LED_02=1;LED_03=1;LED_04=0;BEEP = 0;count_end=0;is_1s = 0;OUT_WATER=1;continue;}
						break;
					default:break;
				}	
			}
			else if(start_step==5) //结束
			{
    
    
					if(is_1s)
					{
    
    
						is_1s = 0;
						count_end++;
						if(count_end>=3)
						{
    
    
							CleatStop();
						}
					}	
			}
		}	
		else //盖子被打开
		{
    
    
			MOTOR_EA = 0;
			if(is_1s)
			{
    
    
				is_1s=0;
				BEEP = !BEEP;
			}
			if(M_GAIZI) // 高电平正常,盖子关闭
			{
    
    
				BEEP = 1;
				is_gaiziOpen=0;
				MOTOR_EA = 1;
			}
		}
	} //while
}
void CleatStop(void)
{
    
    
	//关闭定时器
	TR0 = 0;
	
	//初始化变量
	Sec_Now = 90;
	start_step=0;
	count_end=0;
	is_1s = 0;
	
	//初始化灯
	LED_01=1;
	LED_02=1;
	LED_03=1;
	LED_04=1;
	
	LED_M0=0; //标准模式
	LED_M1=1;
	LED_M2=1;
	
	IN_WATER=1;
	OUT_WATER=1;
	
	//蜂鸣器
	BEEP = 1;
	
	//电机
	MOTOR_IN1=1;
	MOTOR_IN2=1;
	MOTOR_EA = 0;
}
//External interrupt0 service routine
void exint0() interrupt 0           //(location at 0003H)
{
    
    
   //下降沿,洗衣机盖子被打开
	if(start_step) //不是停止状态才执行
		is_gaiziOpen = 1;
}
void timer0() interrupt 1	   //中断计时 50MS
{
    
    
	static uchar count=0;
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	count++;
	if(count>=20)		  //50MS *20  =1 S
	{
    
    
		count=0;
		is_1s = 1;
		
		if(is_gaiziOpen==0)
		{
    
    
			Sec_Now--;
			if(Sec_Now<0)
			{
    
    
				Sec_Now=-1;
			}		
		}
	}
}
void motor_control01(void)
{
    
    
	static uchar count_control01=0;
	if(is_1s)
	{
    
    
		is_1s = 0;
		if(count_control01<3)//0-2
		{
    
    
			MOTOR_IN1=1;
			MOTOR_IN2=0;
		}
		else if(count_control01<5)//3-4
		{
    
    
			MOTOR_IN1=1;
			MOTOR_IN2=1;
		}
		else if(count_control01<8)//5-7
		{
    
    
			MOTOR_IN1=0;
			MOTOR_IN2=1;
		}
		count_control01++;
		if(count_control01>=8)
		{
    
    
			count_control01=0;
		}
	}
}
void motor_control02(void)
{
    
    
	MOTOR_IN1=1;
	MOTOR_IN2=0;
}
unsigned char code table_CA[]={
    
    0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};	  //CA:是共阳极数码管;CC:共阴数码管段码表
sbit W0=P2^3;
sbit W1=P2^4;
void display(char Num)			 //共阴数码管显示
{
    
    
	uchar a=0,b=0;
	if(Num<0)
		Num=0;	
	a=Num/10;		  //取出当前描述的个位与十位
	b=Num%10;

	P0=0xFF;
	W1=1;
	W0=0;
	P0=~table_CA[a];
	delay_ms(5);

	W0=1;
	W1=0;
	P0=~table_CA[b];
	delay_ms(5);
	P0=0x00;
}

sbit key0=P1^5;		//按键接口定义
sbit key1=P1^6;
sbit key2=P1^7;
void keyscan(void)			   //按键检测和处理函数
{
    
    
	if(key0==0)		   //选择键
	{
    
    
		delay_ms(20);
		if(key0==0)
		{
    
    
			if(start_step==0)
			{
    
    
				mode++;
				if(mode>MODE_SOFT)
				{
    
    
					mode=MODE_NORMAL;
				}
				switch(mode){
    
    
					case MODE_NORMAL:
						LED_M0=0;LED_M1=1;LED_M2=1;Sec_Now=90;
						break;
					case MODE_FAST:
						LED_M0=1;LED_M1=0;LED_M2=1;Sec_Now=70;
						break;
					case MODE_SOFT:
						LED_M0=1;LED_M1=1;LED_M2=0;Sec_Now=55;
						break;
					default:break;
				}			
			}
		}
		while(!key0);
	}
	if(key1==0)		    //开始/加
	{
    
    
		delay_ms(20);
		if(key1==0)
		{
    
    			
			if(start_step==0)
			{
    
    
				start_step=1;	
				MOTOR_EA = 1; // 电机可以启动
			}
		}
		while(!key1);
	}
	if(key2==0)		  //停止/减
	{
    
    
		delay_ms(20);
		if(key2==0)
		{
    
    			
			if(start_step)
			{
    
    
				CleatStop();
			}
		}
		while(!key2);
	}
}

Guess you like

Origin blog.csdn.net/qq_20467929/article/details/126107859