这里是我找到的家用电扇控制设计,希望大家有用
vb写的上位机源码:
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg52.h>
- #include<stdio.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar a[16]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
- uchar LED[4]={0xff,0xf3,0xeb,0xdb};
- uchar LED_2[4]={0xff,0xf5,0xed,0xdd};
- uchar motor[4]={0x80,0x60,0x30,0x00};
- uchar text[3][3]={1,2,3,4,5,6,7,8,9};
- char b=0x00;
- char c=0x00;
- char j=0x00;
- #define T_value (unsigned char)0x80
- unsigned char t=0X00;
- unsigned char T_Count;
- unsigned char T_1=0x50;
- unsigned char T_y=0x00;
- unsigned char T_z=0x00;
- unsigned char Tx;
- unsigned char x;
- sbit P3_2=P3^2;
- sbit E=P3^7;
- sbit RW=P3^6;
- sbit RS=P3^5;
- sfr T2MOD=0xc9;
- void delay_1(uint z)
- {
- uint n,y;
- for(n=z;n>0;n--)
- for(y=110;y>0;y--);
- }
- void lcd_com(uchar com)
- {RS=0;
- RW=0;
- P2=com;
- delay_1(5);
- E=1;
- delay_1(5);
- E=0;
- }
- void lcd_data(uchar dat)
- {RS=1;
- RW=0;
- P2=dat;
- delay_1(5);
- E=1;
- delay_1(5);
- E=0;
- }
- void disp_str(uchar n,uchar y,uchar *p)
- {if(n==0)
- lcd_com(0x80+y);
- else
- lcd_com(0xc0+y);
- while(*p)
- lcd_data(*p++);
- }
- void int_lcd(void)
- {
- lcd_com(0x01);
- lcd_com(0x3c);
- lcd_com(0x0c);
- }
- void delay(uint i)
- {uint j;
- for(j=0;j<i;j++);
- }
- uchar checkkey()
- {uchar i,j;
- i=0x0f;
- P1=i;
- j=P1;
- j=j&0x0f;
- if(j==0x0f&&t==0x00)return(0);
- else return(0xff);
- }
- uchar keyscan()
- {
- uchar scancode;
- uchar scanvalue;
- uchar n;
- uchar m=0;
- uchar k;
- uchar i,j;
- {
- scancode=0xf7;
- m=0x00;
- for(i=1;i<=4;i++)
- {k=0x80;
- P1=scancode;
- n=P1;
- for(j=0;j<4;j++)
- {
- if((n&k)==0)
- {scanvalue=m+j;
- while(checkkey()!=0);
- return(scanvalue);
- }
- else
- k=k>>1;
- }
- m=m+4;
- scancode=~scancode;
- scancode=scancode>>1;
- scancode=~scancode;
- }
- }
- }
- void init_tim0()
- {
- TMOD = 0x01;
- TH0 = 0xfe;
- TL0 = 0x00;
- TR0 = 1;
- ET0 = 1;
- EA = 1;
- }
- void init_tim1()
- {TMOD=0x11;
- TH1=0x4c;
- TL1=0x00;
- TR1=1;
- ET1=1;
- EA=1;
- }
- void init_tim2()
- { T2MOD = 0x01;
- T2CON = 0x34;
- TH2 = 0xFF;
- TL2 = 0xDC;
- RCAP2H = 0xFF;
- RCAP2L = 0xDC;
- SCON = 0x50;
- ES=1;
- EA=1;
- }
- void key()
- {
- unsigned char str[4][10]={"","slow","middle","strong"};
- P0=0xff;
- Tx=motor[b];
- while(1)
- {
- if(T_Count == 0)
- {
- P3_2 = ~P3_2;
- Tx = T_value - Tx;
- T_Count = Tx;
- }
- if(checkkey()==0)continue;
- else
- {delay(0x10);
- if(checkkey()==0)continue;
- else
- {x=keyscan();
- if(a[x]==0x00)
- {j=0x01;
- b++;
- if(b==0x04)b=0x01;
- }
- if(a[x]==0x01&&j==0x01)
- {
- c++;
- if(c==0x03)c=0x00;
- }
- if(a[x]==0x02)
- {b=0;
- c=0;
- Tx=0x80;
- P3_2=0;
- j=0x00;
- T_y=0x00;
- T_z=0x00;
- int_lcd();
- P0=0xff;
- SBUF=0;
- }
- if(c==0x00&&a[x]!=0x02)
- {TR1=0;
- P0=LED[b];
- Tx=motor[b];
- P3_2=0X00;
- int_lcd();
- disp_str(0,1,"normal");
- disp_str(1,1,str[b]);
- t=0x00;
- SBUF=text[c][b-1];
- }
- if(c==0x01&&a[x]!=0x02)
- {P0=LED_2[b];
- T_y=0x00;
- init_tim1();
- int_lcd();
- disp_str(0,1,"nature");
- disp_str(1,1,str[b]);
- t=0x00;
- SBUF=text[c][b-1];
- }
- if(c==0x02&&a[x]!=0x02)
- {P0=0xfe;
- b=0x01;
- T_z=0x00;
- init_tim1();
- int_lcd();
- disp_str(0,1,"sleep");
- disp_str(1,1,str[b]);
- t=0x00;
- SBUF=text[c][b-1];
- }
- delay(0x10);
- }
- }
- }
- }
- void main()
- {
- P3_2 = 0;
- init_tim0();
- T_Count = 0X80;
- init_tim2();
- int_lcd();
- key();
- }void int_tim0() interrupt 1 using 1
- { TH0 = 0xfe;
- TL0 = 0x28;
- T_Count--;
- }
- void int_time1() interrupt 3 using 2
- {TH1=0x4c;
- TL1=0x00;
- T_1--;
- if(T_z==0x00||T_y==0x00)
- {Tx=0X80;
- P3_2=0x00;
- }
- if(T_1==0X00)
- {T_y++;T_z++;
- T_1=0x50;
- if(c==0x01)
- {if(T_y==0x02)
- {
- Tx=motor[b];
- P3_2=0x00;
- }
- if(T_y==0x03)
- {Tx=0x80;
- T_y=0x00;
- P3_2=0x00;
- }
- }
- if(c==0x02)
- {if(T_z==0x02)
- {
- Tx=motor[1];
- P3_2=0x00;
- }
- if(T_z==0x04)
- {Tx=0x80;
- T_z=0x00;
- P3_2=0x00;
- }
- }
- }
- }
- void uart() interrupt 4 using 3
- {if(RI==1)
- {RI=0;
- t=SBUF;
- if(t==0x31)
- {b++;
- if(b==0x04)b=1;
- }
- if(t==0x32)
- {c++;
- if(c==0x03)c=0x00;
- }
- if(t==0x33)
- {b=0;
- c=0;
- Tx=0x80;
- P3_2=0;
- j=0x00;
- T_y=0x00;
- T_z=0x00;
- int_lcd();
- P0=0xff;
- SBUF=0;
- }
- }
- else TI=0;
- }
复制代码
所有资料51hei提供下载:
电风扇控制器.7z
(33.14 KB, 下载次数: 50)
|