基于TI-RTOS的CC2650DK开发(3)---按钮的使用

来源:互联网 发布:淘宝店招怎么设置全屏 编辑:程序博客网 时间:2024/06/03 14:51
关于按钮,TI公司有现成例子,只需将例子pinInterrupt导入CCS即可,以下代码是我在pinInterrupt基础上稍做修改,去掉异常判断,使得看上去更简单、轻松些。
#include <xdc/std.h>
#include <xdc/runtime/System.h>

#include <ti/sysbios/BIOS.h>
#include <ti/drivers/Power.h>
#include <ti/drivers/power/PowerCC26XX.h>

#include <ti/drivers/PIN.h>
#include <ti/drivers/pin/PINCC26XX.h>

#include "Board.h"
/* Pin driver handles */
static PIN_Handle buttonPinHandle;
static PIN_Handle ledPinHandle;
/* Global memory storage for a PIN_Config table */
static PIN_State buttonPinState;
static PIN_State ledPinState;

PIN_Config ledPinTable[] = {
    Board_LED0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
    Board_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW  | PIN_PUSHPULL | PIN_DRVSTR_MAX,
    PIN_TERMINATE
};
/* 按钮中断被配置为下降沿触发,其实就是从高电平变为低电平的那一瞬间触发 */
PIN_Config buttonPinTable[] = {
    Board_BUTTON0  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
    Board_BUTTON1  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
    PIN_TERMINATE
};
 /*  ======== buttonCallbackFxn ========
 *  引脚中断回函数,准备用于buttonPinTable,如果把LED3和LED4都加入buttonPinTable,
 *  那么LED3和LED4都可以触发此中断回调函数
 */

void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId) {
    uint32_t currVal = 0;
    //按键消抖
    CPUdelay(8000*50);
    if (!PIN_getInputValue(pinId)) {
        /*切换LED亮灭状态*/
        switch (pinId) {
            case Board_BUTTON0:
                currVal =  PIN_getOutputValue(Board_LED0);
                PIN_setOutputValue(ledPinHandle, Board_LED0, !currVal);
                break;
            case Board_BUTTON1:
                currVal =  PIN_getOutputValue(Board_LED1);
                PIN_setOutputValue(ledPinHandle, Board_LED1, !currVal);
                break;
            default:
                /* Do nothing */
                break;
        }
    }
}

int main(void)
{
    Board_initGeneral();

    ledPinHandle = PIN_open(&ledPinState, ledPinTable);
    buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);
    /* 在按钮配置集中注册回调函数*/
    PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn);
    /* Start kernel. */
    BIOS_start();
    return (0);
}
运行程序,UP键控制LED4,DOWN键控制LED1,按下按键灯亮,再按,灯灭,如此反复。此程序主要演示了外部中断的使用方法。用起来还是很方便的,大至分三步走:
  1. 在配置表中配置中断
  2. 添加中断回调函数
  3. 在引脚配置集中注册回调函数
下面分析下关键代码:
PIN_Config buttonPinTable[] = {
    Board_BUTTON0  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
    Board_BUTTON1  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
    PIN_TERMINATE
};
这里将按钮配置为输入状态,初始状态为高电平,按钮按下则导致引脚变为低电平,由于配置了下降沿中断,所以此时会触发中断,从而执行回调函数。

void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId) 
此句代码声明了回调函数,此函数原型可在PIN.h中找到,代码如下:
typedef void (*PIN_IntCb)(PIN_Handle handle, PIN_Id pinId);
所以在声明时必须参照此原型。

PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn);
这句代码演示了如何将回调函数注册进配置表

另外,CC2650中,所有GPIO都可以配置外部中断。
0 0