打印
[AVR单片机]

atmega8515简单按键程序为什么要改成这样才有响应?

[复制链接]
1406|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
mm7989680|  楼主 | 2007-6-10 21:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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还请做过的人指教,谢谢!

相关帖子

沙发
dudongdao| | 2007-6-10 22:46 | 只看该作者

可能是你的MDELY涵数有为题啊

使用特权

评论回复
板凳
tuwen| | 2007-6-11 15:33 | 只看该作者

if ((PINB&0x40)==0) 这行写错了

是04,不是40。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

112

主题

371

帖子

4

粉丝