目录
一、主要功能
二、硬件资源
三、程序编程
四、实现现象
一、主要功能
用直流电机转动模拟洗衣机。要求
有弱洗、普通洗、强洗三种模式,可通过按键选择。可以设置洗衣时长,通关按键选择15、30、45、60、90分钟。时间到蜂鸣器报警提示。LCD 显示相关信息。
仿真图:
编辑
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
本系统由51单片机最小系统、LCD1602显示模块、按键模块、L298N驱动直流电机模块、蜂鸣器模块组成。
三、程序编程
#include <REGX52.H>
#include<intrins.h>
#include<stdio.h>
#include "Delay.h"
#include "lcd1602.h"
#define uchar unsigned char
#define uint unsigned int
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;uchar count = 0,flag = 0; //定时中断次数
uchar time=0,mode=0,sec=0;//系统变量
uchar fen=0,miao=0,set=0;//时间
uchar miao1=0;//倒计时
uchar disp1\[\]="00:00";
uchar disp2\[\]="00s";sbit Motor1_IN1 = P1^1;
sbit Motor1_IN2 = P1^2;
sbit key1 = P3^0; //按键引脚
sbit key2 = P3^1;
sbit key3 = P3^2;
sbit key4 = P3^3;
sbit key5 = P3^4;
sbit key6 = P3^5;
sbit key7 = P3^6;
sbit key8 = P3^7;
sbit beep = P1^0; //蜂鸣器引脚void keypd();void main() //主函数
{ init_1602(); //显示屏初始化TMOD|=0X01;TH0=0X3C;TL0=0XB0; ET0=1;//打开定时器0中断允许EA=1;//关闭总中断TR0=1;//打开定时器Motor1_IN1 = 0;Motor1_IN2 = 0;beep = 0; //关掉蜂鸣器while(1){keypd(); //按键判断if(mode==0) //停止模式{if(!key4) // 15{set=15;while(!key4);}if(!key5) // 30{set=30;while(!key5);}if(!key6) // 45{set=45;while(!key6);}if(!key7) // 60{set=60;while(!key7);}if(!key8) // 90{set=90;while(!key8);}}switch(mode) //模式控制{case 0: //停止Motor1_IN1 = 0;Motor1_IN2 = 0;break;case 1: //正转Motor1_IN1 = 0;Motor1_IN2 = 1;break;case 2: //停止Motor1_IN1 = 0;Motor1_IN2 = 0;break;case 3: //反转Motor1_IN1 = 1;Motor1_IN2 = 0;break;case 4: //停止Motor1_IN1 = 0;Motor1_IN2 = 0;}}
}void keypd()
{if(key1 == 0){Delay(20);if(key1 == 0){if(mode==0){write_string(2,0,"RX ");flag = 1;mode =1;fen=set;miao=0;miao1=25;}}}if(key2 == 0){Delay(20);if(key2 == 0){if(mode==0){write_string(2,0,"PTX");flag = 2;mode =1;fen=set;miao=0;miao1=15;}}}if(key3 == 0){Delay(20);if(key3 == 0){if(mode==0){write_string(2,0,"QX ");flag = 3;mode =1;fen=set;miao=0;miao1=5;}}}
}
void Timer0() interrupt 1
{if(time<10)//0.5stime++;else{time=0;//显示if(mode==0) //停止运行{write_string(1,0,"set: ");disp1\[0\]=set/10+0x30;disp1\[1\]=set%10+0x30;disp1\[3\]='0';disp1\[4\]='0';write_string(2,5,disp1);}else//运行倒计时{write_string(1,0,"run:");disp1\[0\]=fen/10+0x30;disp1\[1\]=fen%10+0x30;disp1\[3\]=miao/10+0x30;disp1\[4\]=miao%10+0x30;write_string(2,5,disp1);disp2\[0\]=miao1/10+0x30;disp2\[1\]=miao1%10+0x30;write_string(1,6,disp2);}}if(mode>0) //开始运行{if(sec<19)//1ssec++;else{sec=0;if(miao>0)//秒计时miao--;else{if(fen>0){miao=59;//分计时fen--;}else{beep=1; //结束 蜂鸣器响mode=0;}}if(mode==1)//正转{if(miao1>0)miao1--;else{miao1=5;mode=2;}}else if(mode==2)//暂停{if(miao1>0)miao1--;else{if(flag==1){miao1=25;}if(flag==2){miao1=15;}if(flag==3){miao1=5;}mode=3;}}else if(mode==3)//反转{if(miao1>0)miao1--;else{miao1=5;mode=4;}}else if(mode==4)//暂停{if(miao1>0)miao1--;else{if(flag==1){miao1=25;}if(flag==2){miao1=15;}if(flag==3){miao1=5;}mode=1;}} else{miao1=0;} }} TH0=0X3C;TL0=0XB0;
}
四、实现现象
具体动态效果看B站演示视频:
基于51单片机的洗衣机仿真_哔哩哔哩_bilibili
全部资料(源程序、仿真文件、安装包、演示视频):
链接:https://pan.baidu.com/s/13_dZAdQ4VTWunsk25mU-Rg
提取码:k0us
–来自百度网盘超级会员V4的分享