基于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);
}
#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,按下按键灯亮,再按,灯灭,如此反复。此程序主要演示了外部中断的使用方法。用起来还是很方便的,大至分三步走:
- 在配置表中配置中断
- 添加中断回调函数
- 在引脚配置集中注册回调函数
下面分析下关键代码:
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
};
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
- 基于TI-RTOS的CC2650DK开发(3)---按钮的使用
- 基于TI-RTOS的CC2650DK开发(2)---点亮LED
- 基于TI-RTOS的CC2650DK开发(5)---线程概览
- 基于TI-RTOS的CC2650DK开发(6)---硬件中断
- 基于TI-RTOS的CC2650DK开发(7)---软件中断
- 基于TI-RTOS的CC2650DK开发(8)---任务
- 基于TI-RTOS的CC2650DK开发(9)---任务示例
- 基于TI-RTOS的CC2650DK开发(10)---空闲循环
- 基于TI-RTOS的CC2650DK开发(11)---信号量
- 基于TI-RTOS的CC2650DK开发(12)---事件模块
- 基于TI-RTOS的CC2650DK开发(13)---门
- 基于TI-RTOS的CC2650DK开发(14)---邮箱
- 基于TI-RTOS的CC2650DK开发(17)---支持模块
- 基于TI-RTOS的CC2650DK开发(21)---检测
- 基于TI-RTOS的CC2650DK开发(22)--- 时间基准
- 基于TI-RTOS的CC2650DK开发(23)--- 尺寸基准
- 基于TI-RTOS的CC2650DK开发(25)---IOM接口
- 基于TI-RTOS的CC2650DK开发(27)--- 邮箱示例
- 【设计模式 - 10】之外观模式(Facade)
- Install Ubuntu with multiple HDD merging
- Java程序员面试题集(1-50)
- jQuery选择元素
- OpenCV4Android学习之图像特征匹配算法
- 基于TI-RTOS的CC2650DK开发(3)---按钮的使用
- 十五周—字符串逆序输出
- C++ Primer学习笔记之第六章--函数
- Linux下安装Hadoop(2.7.1)详解及WordCount运行
- DatabaseTest
- 来来来, 出个题考一下你的计算机功底------程序中有某一个变量, 为什么在同一时刻有不同的值?
- [AHK]Gmailkeys-for-outlook
- 中国农历
- linux系统启动过程详解