主要程序如下:
其中P3.2用按键代替液滴低落 P3.3用按键表示液位变化
存在问题:(一般是先用矩阵键盘设定上下限,再按P3.2口的按键模仿液滴滴落)
由于仿真液滴滴落由P3.2口的按键代替,滴速上下限min,max要用键盘设定,需要人工操作, 二者存在先后顺序。
在矩阵键盘设置完滴速下限min时,已经检测了speed为0,电机就开始转,以至于无法设定max,影响后面的操作。请各位帮帮忙。
#define LCD_DB P0 //端口定义
#define KEY P1
sbit LCD_E=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_RS=P2^2;
sbit beep=P2^3;
sbit A1=P2^4;
sbit B1=P2^5;
sbit C1=P2^6;
sbit D1=P2^7;
函数声明略;
unsigned int value,max=0,min=0,b,c,d,b1,d1,c1,bai,shi,ge,speed;//变量
unsigned char key;
unsigned int speed_count=0;
unsigned int number0,number1;
unsigned char time_out=0;
void main()
{
LCD_init();
init();
LCD1602_ShowStr(1, 5, "speed:", 6);
LCD1602_ShowStr(0, 0, "min:000", 7);
LCD1602_ShowStr(0, 8, "max:000", 7);
while(1)
{
key_place();
if(time_out== 1 )
{
time_out=0;
speed=speed_count*12;
speed_count=0;
ge=speed%10;
shi=speed/10%10;
bai=speed/100%10;
LCD_ShowNum(1, 11,bai);
LCD_ShowNum(1, 12,shi);
LCD_ShowNum(1, 13,ge);
if(speed>max) //滴速大于设定值上限
{
A1=0;B1=1;C1=1;D1=1;delay(20000);
A1=0;B1=0;C1=1;D1=1;delay(20000);
A1=1;B1=0;C1=1;D1=1;delay(20000);
A1=1;B1=0;C1=0;D1=1;delay(20000);
A1=1;B1=1;C1=0;D1=1;delay(20000);
A1=1;B1=1;C1=0;D1=0;delay(20000);
A1=1;B1=1;C1=1;D1=0;delay(20000);
A1=0;B1=1;C1=1;D1=0;delay(20000);
delay(10000);
}
if(speed<min) //滴速小于设定值下限
{
A1=1;B1=1;C1=1;D1=0;delay(20000);
A1=1;B1=1;C1=0;D1=0;delay(20000);
A1=1;B1=1;C1=0;D1=1;delay(20000);
A1=1;B1=0;C1=0;D1=1;delay(20000);
A1=1;B1=0;C1=1;D1=1;delay(20000);
A1=0;B1=0;C1=1;D1=1;delay(20000);
A1=0;B1=1;C1=1;D1=1;delay(20000);
A1=0;B1=1;C1=1;D1=0;delay(20000);
delay(10000);
}
}
unsigned char scan_key(void) //按键扫描
{
u8 temp,H=0,L=0;
KEY=0x0f;
delay(1000);
temp=KEY;
if(temp!=0x0f)
{
switch(temp)
{
case 0x0e:H=1;break;
case 0x0d:H=2;break;
case 0x0b:H=3;break;
case 0x07:H=4;break
default:break;
}
KEY=0xf0;
temp=KEY;
if (temp!=0xf0)
{
switch(temp)
{
case 0xe0:L=1;break;
case 0xd0:L=2;break;
case 0xb0:L=3;break;
case 0x70:L=4;break;
default:break;
}
while(KEY !=0xF0);
}
}
if(H!=0&&L!=0)
return( (H-1)*4+L );
else
return(0);
}
unsigned char read_key(void) //按键返回数字或字母
{
unsigned char temp,keynumber;
temp=scan_key();
if(temp!=0
{
switch(temp)
{
case 1: keynumber='A';break;
case 2: keynumber='3';break;
case 3: keynumber='2';break;
case 4: keynumber='1';break;
case 5: keynumber='B';break;
case 6: keynumber='6';break;
case 7: keynumber='5';break;
case 8: keynumber='4';break;
case 9: keynumber='C';break;
case 10: keynumber='9';break;
case 11: keynumber='8';break;
case 12: keynumber='7';break;
case 13: keynumber='D';break;
case 14: keynumber='E';break;
case 15: keynumber='0';break;
case 16: keynumber='F';break;
}
}
else
{
keynumber=0;
}
return(keynumber);
}
void key_place(void) //按键设定滴速范围上下限
{
unsigned int j;
unsigned char key;
key=read_key();
if(key!=0)
{
if(key=='1' ||key=='2' ||key=='3' ||key=='4' ||key=='5' ||key=='6' ||key=='7' ||key=='8' ||key=='9'||key=='0' )
{
LCD_ShowNum(1,j,key-'0');
j++;
value=value*10+key-'0';
}
if(key=='A') //A也就是min,下限
{
LCD1602_ShowStr(1, 0, " ", 3);
min=value ;
j=0;
value=0;
}
if(key=='B') //B也就是max,上限
{
LCD1602_ShowStr(1, 0, " ", 3);
max=value;
j=0;
value=0;
}
d=min%10;
c=min/10%10;
b=min/100%10;
LCD_ShowNum(0, 4,b);
LCD_ShowNum(0, 5,c);
LCD_ShowNum(0, 6,d);
d1=max%10;
c1=max/10%10;
b1=max/100%10;
LCD_ShowNum(0, 12,b1);
LCD_ShowNum(0, 13,c1);
LCD_ShowNum(0, 14,d1);
}
void LCD_init(void)
{
LCD_write_command(0x38);
LCD_write_command(0x0c);
LCD_write_command(0x06);
LCD_write_command(0x01);
}
void LCD_write_command(unsigned char com)
{
LCD_E=0;
LCD_RS=0;
LCD_RW=0;
LCD_DB=com;
delay(1000);
LCD_E=1;
delay(5000);
LCD_E=0;
}
void LCD_write_data(unsigned char dat)
{
LCD_E=0;
LCD_RS=1;
LCD_RW=0;
LCD_DB=dat
delay(1000);
LCD_E=1;
delay(5000);
LCD_E=0;
}
void LCD1602_ShowStr(unsigned char x, unsigned char y, unsigned char *str, unsigned char len)
{
LCD1602_SetCursor(x, y);
while (len--)
{
LCD_write_data(*str++);
}
}
void LCD_ShowNum(unsigned char x, unsigned char y,unsigned char num)
{
LCD1602_SetCursor(x, y)
LCD_ShowChar(x,y,num+'0');
}
void LCD_ShowChar(unsigned char x, unsigned char y,unsigned char dat)
{
LCD1602_SetCursor(x, y);
LCD_write_data(dat);
}
void LCD1602_SetCursor(unsigned char x, unsigned char y)
{
unsigned char addr;
if (x == 0)
addr = 0x00 + y;
else
addr = 0x40 + y;
LCD_write_command(addr|0x80);
}
void init()//中断初始化
{
TMOD=0x11; //?????0??1?????????1?? 0001 0001
TH0=(65536-50000)/256;//?????0??1??????
TL0=(65536-50000)%256;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1; //?????ж?
ET0=1; //???????0?ж?
ET1=1; //???????1?ж?
EX0=1; //?????ж?0
EX1=1; //?????ж?1
TR0=1; //????????0
TR1=1; //????????1
IT0=1; //???ж?0??????????
IT1=1; //???ж?1??????????
}
void int0() interrupt 0 //外部中断0检测 液体低落
{
EX0=0;
number1=0;
speed_count++;
EX0=1;
delayms(1);
}
void int1() interrupt 2//中断1检测液位变化
{
EX1=0;
while(1)
{
beep=0;
delay(1000);
beep=1;
delay(1000);
if(read_key()=='D') break;
}
EX1=1;
}
void T0_time() interrupt 1 //定时器0每5秒number0置零,time_out=1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
number0++;
if(number0==100)
{
number0=0;
time_out=1;
}
}
void T1_time() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
number1++;
if(number1==1200)
{
while(1)
{
beep=0;
delay(1000);
beep=1;
delay(1000);
if(read_key()=='D')break;
}
}
}
|