当前位置:文档之家› 基于51单片机的全自动洗衣机-基本代码

基于51单片机的全自动洗衣机-基本代码

#include //包含头文件

sbit RS = P2^4; //定义端口
sbit RW = P2^5;
sbit EN = P2^6;

sbit key_con=P1^0; //按键端口定义
sbit key1=P1^1;
sbit key2=P1^2;
sbit key3=P1^3;
sbit P14=P1^4;
sbit P15=P1^5;
sbit P16=P1^6;
sbit P17=P1^7;

#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
/******************************************************************/
/* LCD微秒延时函数 */
/******************************************************************/
void delay_us(unsigned int n) //延时
{
if (n == 0)
{
return ;
}
while (--n);
}
/******************************************************************/
/* LCD毫秒函数声明 */
/******************************************************************/
void delay_ms(unsigned char i)
{
unsigned char a, b;
for (a = 1; a < i; a++)
{
for (b = 1; b; b++)
{ ; }
}
}
/******************************************************************/
/* LCD写入命令函数 */
/******************************************************************/
void LCD_write_com(unsigned char com)
{
RS_CLR;
RW_CLR;
EN_SET;
P0 = com;
delay_us(5);
EN_CLR;
}
/******************************************************************/
/* LCD写入数据函数 */
/******************************************************************/
void LCD_write_Data(unsigned char Data)
{
RS_SET;
RW_CLR;
EN_SET;
P0 = Data;
delay_us(5);
EN_CLR;
}
/******************************************************************/
/* LCD清屏函数 */
/******************************************************************/
void LCD_clear(void)
{
LCD_write_com(0x01);
delay_ms(5);}
/******************************************************************/
/* LCD写入字符串函数 */
/******************************************************************/
void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_write_com(0x80 + x);
}
else
{
LCD_write_com(0xC0 + x);
}
while (*s)
{
LCD_write_Data( *s);
s ++;
}
}
/******************************************************************/
/* LCD写入字节函数 */
/******************************************************************/
void LCD_write_char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_write_com(0x80 + x);
}
else
{
LCD_write_com(0xC0 + x);
}
LCD_write_Data( Data);
}
/******

************************************************************/
/* LCD初始化函数 */
/******************************************************************/
void LCD_init(void)
{
LCD_write_com(0x38); /*显示模式设置*/
delay_ms(5);
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
LCD_write_com(0x08); /*显示关闭*/
LCD_write_com(0x01); /*显示清屏*/
LCD_write_com(0x06); /*显示光标移动设置*/
delay_ms(5);
LCD_write_com(0x0C); /*显示开及光标设置*/
}

/******************************************************************/
/* 延时函数 */
/******************************************************************/



void delay500ms(void) //延时500ms
{
unsigned char h,i,j,k;
for(h=5;h>0;h--)
for(i=4;i>0;i--)
for(j=116;j>0;j--)
for(k=107;k>0;k--);
}

void delay1s(void) //延时1s
{
unsigned char h,i,j,k;
for(h=5;h>0;h--)
for(i=4;i>0;i--)
for(j=116;j>0;j--)
for(k=214;k>0;k--);
}

void delay10ms(void) //延时 10ms
{
unsigned char i,j,k;
for(i=5;i>0;i--)
for(j=4;j>0;j--)
for(k=248;k>0;k--);
}






/******************************************************************/
/* 洗涤漂洗甩干程序 */
/******************************************************************/

void wash_delay() //洗涤程序
{
int i;
LCD_clear();
for(i=100;i>0;i--)
{
LCD_write_str(0,0,"washing now");
delay10ms();
LCD_clear();
}return;
}

void rinsh_delay() //漂洗程序
{
int i;
LCD_clear();
for(i=40;i>0;i--)
{
LCD_write_str(0,0,"rinshing now");
delay10ms();
LCD_clear();
delay10ms();
}return;
}

void shin_delay() //甩干程序
{
int i;
LCD_clear();
for(i=40;i>0;i--)
{
LCD_write_str(0,0,"shin now");
delay10ms();
LCD_clear();
delay10ms();
}return;
}

/******************************************************************/
/* 注水程序延时 */
/******************************************************************/

void high() //高水量注水
{
int i;
LCD_clear();
LCD_write_str(0,0,"high water");
LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');
for(i=9;i>0;i--)
{

LCD_write_char(1,1,'9');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"high water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'8');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"high water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_wri

te_char(1,1,'7');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"high water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'6');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"high water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'5');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"high water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'4');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"high water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'3');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"high water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'2');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"high water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'1');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"high water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'0');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"high water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();return;
}
}

void middle() //中等水量注水
{
int i;
LCD_clear();
LCD_write_str(0,0,"middle water");
LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');
for(i=5;i>0;i--)
{
LCD_write_char(1,1,'5');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"middle water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'4');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"middle water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'3');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"middle water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'2');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"middle water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'1');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"middle water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'0

');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"middle water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();return;
}
}

void low() //低水量注水
{
int i;
LCD_clear();
LCD_write_str(0,0,"low water");
LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');
for(i=3;i>0;i--)
{
LCD_write_char(1,1,'3');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"low water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'2');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"low water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'1');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"low water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();
LCD_write_char(1,1,'0');LCD_write_char(2,1,'0');LCD_write_char(3,1,'s');LCD_write_str(0,0,"low water");
delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();delay1s();return;
}
}









/******************************************************************/
/* 功能区 */
/******************************************************************/



void fun1()//洗涤功能
{
while(1)
{
LCD_clear();
LCD_write_str(0,0,"water yield");
LCD_write_str(0,1,"1.H 2.M 3.L");delay1s();
if(!key1)
{
high();
wash_delay();return;
}
else if(!key2)
{
middle();
wash_delay();return;
}
else if(!key3)
{
low();
wash_delay();return;
}
}
}

void fun2()//漂洗功能
{
while(1)
{
LCD_clear();
LCD_write_str(0,0,"water yield");
LCD_write_str(0,1,"1.H 2.M 3.L");delay500ms();
if(!key1)
{
high();
rinsh_delay();return;
}
else if(!key2)
{
middle();
rinsh_delay();return;
}
else if(!key3)
{
low();
rinsh_delay();return;
}
}
}

void fun3()//脱水功能
{
shin_delay();
}

/******************************************************************/
/* 菜单函数 */
/******************************************************************/

void menu_hade()
{
LCD_init();
LCD_clear();
while(1)
{
LCD_write_str(0,0,"1.wash 2.rinsh");//1.wash 2.rinsh 3.shin 1洗涤 2漂洗 3 脱水
LCD_write_str(0,1,"3.shin");
if(!key1)
{
fun1();
}
else if(!key2)
{
fun2();
}
else if(!key3)
{
fun3();
}
}
}

/******************************************************************/
/* 主函数 */
/********************************

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

void main()
{
EA=1; //中断控制
EX0=1;
IT0=0;
menu_hade();
}

/******************************************************************/
/* 暂停与继续 */
/******************************************************************/

void pause(void) interrupt 0 using 1//暂停与继续应用中断,由两个键实现,暂停可在洗衣机执行任何时刻使用,暂停程序,防止突发情况
{
LCD_clear();
LCD_write_str(0,0,"pause now");
while(1)
{
if(!key_con)
{
delay10ms();
LCD_clear();
return;
}
}
return;
}

相关主题
文本预览
相关文档 最新文档