目录
一、主要功能
用直流电机转动模拟洗衣机。要求
cpp
有弱洗、普通洗、强洗三种模式,可通过按键选择。可以设置洗衣时长,通关按键选择15、30、45、60、90分钟。时间到蜂鸣器报警提示。LCD 显示相关信息。
仿真图:
编辑
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
本系统由51单片机最小系统、LCD1602显示模块、按键模块、L298N驱动直流电机模块、蜂鸣器模块组成。
三、程序编程
cpp
#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.5s
time++;
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)//1s
sec++;
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站演示视频:
全部资料(源程序、仿真文件、安装包、演示视频):
链接:https://pan.baidu.com/s/13_dZAdQ4VTWunsk25mU-Rg
提取码:k0us
--来自百度网盘超级会员V4的分享