搜索
bottom↓
回复: 4

帮我看一下这个基于状态机的按键扫描程序怎么不起作用呢?(问题解决)

[复制链接]

出0入0汤圆

发表于 2009-3-28 11:19:48 | 显示全部楼层 |阅读模式
extern volatile int8_t keytime;//定时10ms 自加,在定时器中处理
#define Key1 (PIND&_BV(0))  //按键1
#define Key2 (PINE&_BV(6))  //按键2
#define Key3 (PINE&_BV(7))  //按键3

#define KEY_A 0x01
#define KEY_B 0x02
#define KEY_C 0x04
#define NULL 0
#define STATE_A 0
#define STATE_B 1
#define STATE_C 2
void keyportinit(void)
{
        PORTD|=_BV(PD0);                //置上接电阻
        PORTE|=_BV(PD6)|_BV(PD7);//置上拉电阻
        DDRD&=~_BV(PD0);                //输入
        DDRE&=~_BV(PD6)|~_BV(PD7);//输入
        asm volatile ("nop");
        asm volatile ("nop");
}
uint8 getkeyvalue(void)//取得键值
{
        static uint8 keyvalue;
        if(Key1==0)
        keyvalue=KEY_A;
          if(Key2==0)
            keyvalue=KEY_B;
        if(Key3==0)
        keyvalue=KEY_C;
        else
        keyvalue=NULL;
        return keyvalue;
}
uint8 keyscan(void)
{
        static uint8_t event = 0;
        volatile uint8_t key_press;
        if(getkeyvalue()!=0)
        {
                key_press=getkeyvalue();
                keytime=0;
        }
        else
        {
                key_press=0;
        }
        if(key_press!=0)
        {
                if((keytime > 1) && (keytime < 8)) //判断按键时间
                {
                        switch(key_press)
                        {
                                case KEY_A:
                                event=STATE_A;
                                break;
                                case KEY_B:
                                event=STATE_B;
                                break;
                                case KEY_C:
                                   event=STATE_C;
                                break;
                                default:
                                event=NULL;
                                break;
                        }
                }
               
        }

        return event;
}

以上代码片断
以下是正确代码
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>

#define MCU_XTAL 7372800
#define TIMER1_START  0x0C        // Start value Timer1, Prescaler 256, CTC mode, OCR1A = Top
#define TIMER1_STOP   0x00        // Stop Timer1 value
// OCR1A Top value, 100ms interrupt
#define TICK_100MS    (uint16_t)(10000/(1000000UL/(MCU_XTAL/256)))
// auto power off time, default 60s
#define AUTO_OFF_TIME 600

volatile int8_t keytime;//定时10ms 自加,在定时器中处理
#define Key1 (PIND&_BV(0))  //按键1
#define Key2 (PINE&_BV(6))  //按键2
#define Key3 (PINE&_BV(7))  //按键3

#define KEY_A 0x01
#define KEY_B 0x02
#define KEY_C 0x04
#define NULL 0
#define STATE_A 0
#define STATE_B 1
#define STATE_C 2
void keyportinit(void)
{
        PORTD|=_BV(PD0);                //置上接电阻
        PORTE|=_BV(PD6)|_BV(PD7);//置上拉电阻
        DDRD&=~_BV(PD0);                //输入
        DDRE&=~_BV(PD6)|~_BV(PD7);//输入
        asm volatile ("nop");
        asm volatile ("nop");
}
uint8_t getkeyvalue(void)//取得键值
{
        uint8_t keyvalue=0;
        if(Key1==0)
        keyvalue=KEY_A;
        if(Key2==0)
        keyvalue=KEY_B;
        if(Key3==0)
        keyvalue=KEY_C;
        return keyvalue;
}
uint8_t keyscan(void)
{
        volatile uint8_t event = 0;
        volatile uint8_t key_press;
        if(getkeyvalue()!=0)
        {
                key_press=getkeyvalue();
               
        }
        else
        {
                key_press=0;
                                keytime=0;
        }
        if(key_press!=0)
        {
                if((keytime > 1)&&(keytime<8)) //判断按键时间
                {
                        switch(key_press)
                        {
                                case KEY_A:
                                event=STATE_A;
                                break;
                                case KEY_B:
                                event=STATE_B;
                                break;
                                case KEY_C:
                                   event=STATE_C;
                                break;
                                default:
                                event=NULL;
                                break;
                        }
                }
                 
        }

        return event;
}

void Timer1_Init(void)
{
        OCR1A = TICK_100MS;                // define TIMER1 overflow value in CTC mode
    TIMSK = _BV (OCIE1A);        // enable Timer1 output compare A interrupt
    TCCR1B = _BV (WGM12) | _BV (CS12);// start Timer1
}
ISR(TIMER1_COMPA_vect)
{
        keytime++;
}
int main(void)
{
        Timer1_Init();
        keyportinit();
        sei();
        while(1)
        {
        volatile uint8_t a;
        a=keyscan();
        switch(a)
        {
          ...
        }

        return 0;
}

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

 楼主| 发表于 2009-3-29 20:52:37 | 显示全部楼层
没有人回应呢?

出0入0汤圆

发表于 2009-3-29 20:56:27 | 显示全部楼层
markk

出0入0汤圆

发表于 2009-3-31 10:21:48 | 显示全部楼层
 而马老师的按键状态机如下:
  if (10ms计时标志)
    {
        清除10ms计时标志
        switch (状态机)
        {  
    case 状态0:
       读键端口
          if (有按键按下)
                  转状态1
               break;
    case 状态1: 
       读键端口
          if (前后两次相同)
       {
                  置按键标志
                  转状态2,等等按键释放
               }
               else
         转状态0
                break;
   case 状态2:
       读键端口
       if (按键已经释放)
         转状态0  
               break;
        }
     }  

出0入0汤圆

 楼主| 发表于 2009-4-2 13:13:27 | 显示全部楼层
我这个可以判断一个按键时间长短,实现单键多功能。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-25 20:00

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表