要求实现按住按键不放到一定时间时响应频率增加,红色字体部分为判断语句,现在的问题就是当按住不放的时候不能实现响应速度增加效果,请大师们帮忙提点,谢谢
//ICC-AVR application builder : 2010-4-1 12:16:35
// Target : M16
// Crystal: 4.0000Mhz
#include <iom16v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0xFF; //m103 output only
DDRC = 0xFF;
PORTD = 0x00;
DDRD = 0x00;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
#define uchar unsigned char
#define uint unsigned int
#define right 0X80
#define left 0X40
uchar codetable[]={0X14,0X9F,0X38,0X1A,0X93,0X52,0X50,0X1F,0X10,0X12};
uchar count=0,x=20,y=0,z=0;
void delay1()//显示延迟程序
{
unsigned int c,d;
for(c=1000;c>0;c--);
{for(d=10000;d>0;d--);}
}
void display()//显示程序
{
PORTA=left|right;//关显示
PORTC=codetable[a/10];
PORTA=left;
delay1();
PORTA=left|right;//关显示
PORTC=codetable[a%10];
PORTA=right;
delay1();
}
void delay()//去抖程序
{
for(x;x>0;x--)
{
display();
}
}
void anniu()//计数
{
if((PINB&0X01)==0X00) //判断按键
{
count++;
z=count;
if((10<(z-y))&&(10<x)) //判断按下时间调整响应时间,调节由x传递范围为10~20
{
x--;
delay(x);
}
else
{
x=20;
delay();
}
}
display();
}
void main(void)
{
init_devices(); //初始化
while(1)
{y=count;
if(count<100)//当计数到99时返回0
anniu();
else
count=0;
}
} |