按键读取程序怎么理解

[复制链接]
4801|17
 楼主| xifengsong 发表于 2011-11-7 16:36 | 显示全部楼层 |阅读模式
在TI的例程里经常看到关于按键值得读取的程序都大同小异,但是我就看不懂那一段程序到底是什么意思?为什么会那么写。哪位高手给我解读下好么?程序如下:
    ucData = GPIOPinRead(BUTTONS_GPIO_BASE, ALL_BUTTONS);         
    //
    // Determine the buttons that are in a different state than the debounced
    // state.
    //
      
    ucDelta = ucData ^g_ucButtonStates;
    //
    // Increment the clocks by one.
    //
    g_ucDebounceClockA ^= g_ucDebounceClockB;                          g_ucDebounceClockB = ~g_ucDebounceClockB;                  
    // Reset the clocks corresponding to switches that have not changed state.
    //
    g_ucDebounceClockA &= ucDelta;
    g_ucDebounceClockB &= ucDelta;
    //
    // Get the new debounced switch state.
    //
    g_ucButtonStates &= g_ucDebounceClockA | g_ucDebounceClockB;
    g_ucButtonStates |= (~(g_ucDebounceClockA | g_ucDebounceClockB)) & ucData;
    //
    // Determine the switches that just changed debounced state.
    //
    ucDelta ^= (g_ucDebounceClockA | g_ucDebounceClockB);
    //
    // Remember the delta state in our global variable.
    //
    g_ucButtonDelta = ucDelta;
    //
    // Now consider auto-repeats.
    //
    ucRepeat = 0;
    //
    // Loop through the buttons.
    //
    for(ulLoop = 0; ulLoop < NUM_BUTTONS; ulLoop++)
    {
        //
        // We only ever send auto-repeats for buttons that are in the pressed
        // state so check for this first.
        //
        if((g_ucButtonStates & g_psButtonInfo[ulLoop].ucBtn) == 0)
        {
            //
            // First, check for any buttons that have just been pressed.  We
            // need to initialize the auto-repeat counter for these.
            //
            if(ucDelta & g_psButtonInfo[ulLoop].ucBtn)
            {
                //
                // The button has just been pressed so set up for the initial
                // delay prior to repeating.
                //
                g_psButtonInfo[ulLoop].ucCount =
                g_psButtonInfo[ulLoop].ucInitialCount;
            }
            //
            // Now determine if the button needs to send an auto-repeat.  This
            // will be necessary if auto-repeat is enabled (ucRepeatCount not
            // set to 0) and the button counter has reached 0.
            //
            if((g_psButtonInfo[ulLoop].ucCount == 0) &&
               (g_psButtonInfo[ulLoop].ucRepeatCount != 0))
            {
                //
                // Set the auto-repeat flag for this button.
                //
                ucRepeat |= g_psButtonInfo[ulLoop].ucBtn;
                //
                // Reset the button counter for the next auto-repeat interval.
                //
                g_psButtonInfo[ulLoop].ucCount =
                    g_psButtonInfo[ulLoop].ucRepeatCount;
            }
            //
            // Decrement the button counter.
            //
            g_psButtonInfo[ulLoop].ucCount--;
        }
    }

    //
    // Update our global with the auto-repeat state.
    //
    g_ucButtonRepeat = ucRepeat;
    //
    // Pass the returned button information back to the caller.
    //   
    *pucDelta = ucDelta;
    *pucRepeat = ucRepeat;   
    return(g_ucButtonStates);
 楼主| xifengsong 发表于 2011-11-8 16:51 | 显示全部楼层
怎么没人来啊,大家没用过TI的例程吗?
TI_MCU 发表于 2011-11-8 19:07 | 显示全部楼层
你不明白的部分在哪里?这段代码有消抖和自动重复的功能
amini 发表于 2011-11-8 21:49 | 显示全部楼层
留印慢慢看。现在看着有点晕。
21ID 发表于 2011-11-9 08:48 | 显示全部楼层
这样的发出代码来一般没人会看
 楼主| xifengsong 发表于 2011-11-10 08:53 | 显示全部楼层
要怎么发才有人看啊?我不明白g_ucDebounceClockA     g_ucDebounceClockB 这两个变量的作用。它消抖和自动重复的原理是什么啊?
TI_MCU 发表于 2011-11-10 09:35 | 显示全部楼层
6# xifengsong

其实每行上都有注释啦~~先试着理解下,关键的一两句不懂的再写出来问,问的时候记得交代下前后都做过什么。代码太长了,大家都没有这么多时间仔细看的
jzhjt29 发表于 2011-11-28 13:28 | 显示全部楼层
哪里不懂?   不好解释啊
tl5324260 发表于 2011-11-28 13:31 | 显示全部楼层
怎么看啊
tianm 发表于 2011-11-29 10:13 | 显示全部楼层
别急着看代码 先把消抖和自动重复的原理搞明白
chaofansoft 发表于 2011-11-29 10:27 | 显示全部楼层
:victory:
ximenk 发表于 2012-5-6 15:57 | 显示全部楼层
太长la
gaochy1126 发表于 2012-5-6 16:05 | 显示全部楼层
楼主表明一下定义的变量,很乱,按键的读写都是一样的,参考单片机的程序读一下吧
cooran 发表于 2012-5-7 14:16 | 显示全部楼层
那一段啊?楼主缩小下范围啊,全部?
shaomin128 发表于 2012-6-15 09:42 | 显示全部楼层
figo20042005 发表于 2012-6-15 13:33 | 显示全部楼层
看了下,大概是消除抖动,然后判断按键弹起,按下的吧。stellaris没用过
dyq052 发表于 2012-9-16 09:16 | 显示全部楼层
路过,。。。。。。。。
shenmu2012 发表于 2012-9-17 09:46 | 显示全部楼层
楼主这程序太繁琐了吧?看的有些不大明白的啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

77

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部