AVR_M16 按键+数码管显示遇到的问题,我是解决不了,请大神
先上原理图,这个硬件要想做好显示和按键是有问题的,我是这样做的,当按键按下的时候停止显示,显示程序是2MS执行一次。下面说说问题:
在PROTUES仿真的时候,想要的现象是对的,但是下载到板子上,按键却不能用,但是在板子上单独调试按键这一款的时候按键是好用的,最终把问题锁定在这里为什么呢?百思不得其解
unsigned char key_scan(void)
{
static unsigned char statuse = 0;
static unsigned char key_cnt = 0;
static unsigned char scan_f = 0;
unsigned char temp;
unsigned char key_value = 0;
unsigned char now_key = 0;
if(key_cnt >3)
{
key_cnt = 0;
}
temp = PINA;
temp |= 0x70;
PORTA = key_sel & temp;
now_key = PINA;
now_key >>=7;
switch(statuse)
{
case 0: if(now_key == 0) //有键按下
{
statuse = 1;
key_value = 0xf0;
}
else
{
if(++scan_f >20 )
{
scan_f = 0;
key_cnt ++;
}
statuse = 0;
key_value = 0xff;
}
break;
case 1: if(now_key == 0) //延时去抖
{
statuse = 2;
key_value = 0xf0;
}
else
{
statuse = 0;
key_value = 0xff;
}
break;
case 2: if(now_key == 1) //按键松开
{
key_value = key_cnt;
statuse = 0;
}
else
{
key_value= 0xf0;
statuse = 2;
}
break;
default:
key_value = 0xff;
break;
}
return key_value;
}
unsigned char Key(void)
{
unsigned char key_value = 0;
key_value = key_scan();
//PORTD = Distance;
//PORTD = key_value;
switch(key_value)
{
case 0: //距离+
if(Distance > 9)
{
Distance = 1;
}
else
{
Distance ++;
}
break;
case 1: //距离-
if(Distance < 2)
{
Distance = 10;
}
else
{
Distance --;
}
break;
case 2: //I+
if(Current > 9)
{
Current = 1;
}
else
{
Current ++;
}
break ;
case 3: //I-
if(Current < 2)
{
Current = 10;
}
else
{
Current --;
}
break ;
default: break;
}
return key_value;
} 求解啊 看这个C的结构就是 跟马老师书上的写完差不多。 问题自己解决了,是对IO位操作的时候有问题 ~楼主可否将改完的程序上传? unsigned char key_scan(void)
{
static unsigned char statuse = 0; //按键状态
static unsigned char key_cnt = 0;//扫描计数
static unsigned char scan_f = 0;//扫描频率
unsigned char key_value = 0; //返回键值
unsigned char now_key = 0; //当前键值
if(key_cnt >4) //正常应该是3,应为111,所以重复一次
{
key_cnt = 0;
}
switch(key_cnt)
{
case 0: PORTA &= ~(1 << PA4) ; PORTA &= ~(1 << PA5) ;; PORTA |= (1 << PA6); break;//100
case 1: PORTA |= (1 << PD4); PORTA &= ~(1 << PA5) ;PORTA |= (1 << PA6); break; //101
case 2: PORTA &= ~(1 << PA4); PORTA |= (1 << PA5);PORTA |= (1 << PA6); break; //110
case 3: PORTA |= (1 << PA4); PORTA |= (1 << PA5);PORTA |= (1 << PA6); break; //111
case 4: PORTA |= (1 << PA4); PORTA |= (1 << PA5);PORTA |= (1 << PA6); break; //111,重复一次,否则111时候的按键检测不到
default:break;
}
now_key =PINA & (1 << PA7);
now_key >>=7;
switch(statuse)
{
case 0: if(now_key == 0) //有键按下
{
statuse = 1;
key_value = 0xf0; //这个值是为了区分按键正常时候的状态,根据自己要求来。
disp_en = 0; ///////disp_en = 按键不扫描,数码管不显示 =1按键扫描,数码管正常显示
}
else
{
disp_en = 1;
if(++scan_f >20 ) ////扫描一次查询20次
{
scan_f = 0;
key_cnt ++;
}
statuse = 0;
key_value = 0xff;
}
break;
case 1: if(now_key == 0) //延时去抖
{
statuse = 2;
key_value = 0xf0;
}
else
{
statuse = 0;
key_value = 0xff;
}
disp_en = 0;
break;
case 2: if(now_key == 1) //按键松开
{
key_value = key_cnt;
statuse = 0;
}
else
{
key_value= 0xf0;
statuse = 2;
}
disp_en = 0;
break;
default:
key_value = 0xff;
break;
}
return key_value;
}
页:
[1]