要求:
1、有3档速度调节
2、要显示所设置的速度值
3、可改变方向
电路原理图如下:
程序如下:
- #include"reg51.h"
- #include"intrins.h"
- #include"absacc.h"
- #define busy 0x80
- #define uchar unsigned char
- #define unit unsigned int
- sbit RS=P2^3;
- sbit RW=P2^4;
- sbit E=P2^5;
- sbit KEY1=P2^0;
- sbit KEY2=P2^1;
- sbit KEY3=P2^2;
- uchar code tab[8]={0x02,0x06,0x04,0x0C,0x08,0x09,0x01,0x03};
- uchar temp;
- void delay(uchar k)
- {
- unit i,j;
- for(i=0;i<k;i++)
- {
- for(j=0;j<60;j++)
- {;}
- }
- }
- void test_1602busy()
- {
- P0=0xFF;
- E=1;
- RS=0;
- RW=1;
- _nop_();
- _nop_();
- while(P0&busy)
- {
- E=0;
- _nop_();
- E=1;
- _nop_();
- }
- E=0;
- }
- void write_1602Command(uchar co)
- {
- test_1602busy();
- RS=0;
- RW=0;
- E=0;
- _nop_();
- P0=co;
- _nop_();
- E=1;
- _nop_();
- E=0;
- }
- void write_1602Data(uchar Data)
- {
- test_1602busy();
- P0=Data;
- RS=1;
- RW=0;
- E=1;
- _nop_();
- E=0;
- }
- void init_1602(void)
- {
- write_1602Command(0x38);
- delay(5);
- write_1602Command(0x01);
- delay(5);
- write_1602Command(0x06);
- delay(5);
- write_1602Command(0x0F);
- delay(5);
- write_1602Command(0x0C);
- }
- void DisplayOneChar(uchar X, uchar Y, uchar DData)
- {
- Y&=1;
- X&=15;
- if(Y)X|=0x40;
- X|=0x80;
- write_1602Command(X);
- write_1602Data(DData);
- }
- void display_1602(uchar *DData,X,Y)
- {
- uchar ListLength=0;
- Y&=0x01;
- X&=0x0F;
- while(X<16)
- {
- DisplayOneChar(X,Y,DData[ListLength]) ;
- ListLength++;
- X++;
- }
- }
- void main()
- {
- uchar i=0;
- uchar delay_v=100;
- uchar flag=0;
- P1=0xFF;
- P2=0xFF;
- init_1602();
- display_1602("STA: SPD: ",0,0);
- display_1602("RUN: ",0,1);
- while(1)
- {
- if(KEY2==1)DisplayOneChar(4,0,'Z');
- else DisplayOneChar(4,0,'F');
- if(KEY3==0)
- {
- i++;
- i=i%3;
- while(KEY3==0)
- {;}
- }
- switch(i)
- {
- case 0:delay_v=100;DisplayOneChar(13,0,'1');break;
- case 1:delay_v=75;DisplayOneChar(13,0,'2');break;
- case 2:delay_v=100;DisplayOneChar(13,0,'3');break;
- }
- if(KEY1==0)
- {
- display_1602(" RUN:ON ",0,1);
- if(flag==0)
- {
- if(KEY2==1)
- { temp=0;
- P1=tab[temp];
- flag=1;
- delay(delay_v);
- }
- if(KEY2==0)
- { temp=6;
- P1=tab[temp];
- flag=1;
- delay(delay_v);
- }
- }
- if(KEY2==1)
- { temp++;
- if(temp==8)
- {temp=0;}
- P1=tab[temp];
- delay(delay_v);
- }
- if(KEY2==0)
- { temp--;
- if(temp==0xFF)
- {temp=7;}
- P1=tab[temp];
- delay(delay_v);
- }
- }
- else display_1602(" RUN:off ",0,1);
- }
- }
复制代码
|