冰楓論壇

標題: AT89C2051製作智慧洗衣機 [打印本頁]

作者: 洪嵐峰    時間: 2023-4-4 12:33
標題: AT89C2051製作智慧洗衣機
所需材料:

AT89C2051單片機
LCD顯示屏
電源模塊
16MHz晶體振盪器
三態開關
水位傳感器
水閥
電磁閥
洗衣機馬達
電阻、電容、二極管、三極管等電子元件
連結腳位:

P1.0至P1.3:連接LCD顯示屏
P3.0至P3.3:連接水位傳感器
P3.4、P3.5:連接三態開關
P3.6、P3.7:連接電磁閥
P2.0、P2.1、P2.2、P2.3:連接洗衣機馬達
以下是AT89C2051智慧洗衣機的程式碼
請根據實際需要進行修改:

scss

#include<reg51.h>
#include<intrins.h>

#define uchar unsigned char
#define uint unsigned int

sbit RS = P1^0;
sbit RW = P1^1;
sbit E = P1^2;
sbit MOTOR1 = P2^0;
sbit MOTOR2 = P2^1;
sbit MOTOR3 = P2^2;
sbit MOTOR4 = P2^3;
sbit VALVE = P3^6;
sbit PUMP = P3^7;
sbit SENSOR_LOW = P3^0;
sbit SENSOR_MID = P3^1;
sbit SENSOR_HIGH = P3^2;
sbit SWITCH_POWER = P3^4;
sbit SWITCH_START = P3^5;

void delay(uint i)
{
    while(i--);
}

void write_com(uchar com)
{
    RS = 0;
    RW = 0;
    P0 = com;
    E = 1;
    delay(100);
    E = 0;
}

void write_data(uchar dat)
{
    RS = 1;
    RW = 0;
    P0 = dat;
    E = 1;
    delay(100);
    E = 0;
}

void init_lcd()
{
    write_com(0x38);
    write_com(0x0c);
    write_com(0x06);
    write_com(0x01);
}

uchar read_sensor()
{
    if(SENSOR_HIGH==1)
        return 3;
    else if(SENSOR_MID==1)
        return 2;
    else if(SENSOR_LOW==1)
        return 1;
    else
        return 0;
}

void wash()
{
    write_data('W');
    write_data('A');
    write_data('S');
    write_data('H');
    MOTOR1 = 1;
    MOTOR2 = 0;
    MOTOR3 = 0;
    MOTOR4 = 0;

void spin()
{
write_data('S');
write_data('P');
write_data('I');
write_data('N');
MOTOR1 = 0;
MOTOR2 = 1;
MOTOR3 = 0;
MOTOR4 = 0;
}

void dry()
{
write_data('D');
write_data('R');
write_data('Y');
VALVE = 0;
PUMP = 1;
delay(10000);
VALVE = 1;
PUMP = 0;
MOTOR1 = 0;
MOTOR2 = 0;
MOTOR3 = 1;
MOTOR4 = 0;
}

void main()
{
init_lcd();
while(1)
{
if(SWITCH_POWER==1)
{
write_com(0x01);
write_data('P');
write_data('O');
write_data('W');
write_data('E');
write_data('R');
while(SWITCH_POWER==1);
}
if(SWITCH_START==1)
{
uchar water_level = read_sensor();
if(water_level==0)
{
write_com(0x01);
write_data('N');
write_data('O');
write_data(' ');
write_data('W');
write_data('A');
write_data('T');
write_data('E');
write_data('R');
delay(1000);
}
else
{
write_com(0x01);
write_data('W');
write_data('A');
write_data('T');
write_data('E');
write_data('R');
delay(1000);
wash();
delay(10000);
spin();
delay(5000);
dry();
}
while(SWITCH_START==1);
}
}
}




歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓