#include <iom8515v.h> #include <macros.h> #define uchar unsigned char #define uint unsigned int #define ulong unsigned long
void port_init(void) { PORTA = 0x00; DDRA = 0xFF; PORTB = 0x04; DDRB = 0x00; PORTC = 0xC8; DDRC = 0x18; PORTD = 0x00; DDRD = 0x00; PORTE = 0x00; DDRE = 0x00; OSCCAL= 0XA7; SFIOR&=~BIT(2); //SFIOR&=~BIT(PUD); }
void mDelay(uint Delay) { uint K; for(;Delay>0;Delay--) { for(K=0;K<900;K++) {;} } }
//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; EMCUCR = 0x00; GICR = 0x00; TIMSK = 0x00; SEI(); //re-enable interrupts //all peripherals are now initialized }
// void main(void) { uint i=0; init_devices(); //insert your functional code here... CLI(); while(1) { if((PINB&0x04)==0) {mDelay(10); //消除按键抖动 if ((PINB&0x40)==0) { PORTA=i++; if(i>0xff) i=0;
} }
} }
目的是PB2的按键使PORTA的数据递增,问题是按
键下去没反映,while(1)中换成 while(1) { if((PINB&0x04)==0) {//mDelay(10); //消除按键抖
动 //if ((PINB&0x40)==0) { PORTA=i++; if(i>0xff) i=0;
} }
} 就可以响应按键,难道中间的消除按键抖动不能
用? 刚学习AVR还请做过的人指教,谢谢!
|