我们还是先把所有中断相关寄存器列出来吧。与中断有关的寄存器一共7个,结构都差不多,下面一一讲解:
/** @} */
/** @} end of GPIO_PinConfigSettings group */
/*!
* @brief GPIO回调函数类型
*
* @param index GPIO index。它和GPIO_setCallback()函数中传递的index
* 是同一个值。通过使用index来标识导致中断的GPIO,使得你可
* 以在多个GPIO中断中使用相同的回调函数。
*/
typedef void (*GPIO_CallbackFxn)(uint_least8_t index);
/*
* 回调函数数组指针
* NOTE: 此处引脚配置顺序必须与CC3220SF_LAUNCHXL.h中的所定义的保持一致
* NOTE: 不用于中断的引脚可在此处省略,以减少内存使用(前提是不使用中断的
* 引脚排放在数组未尾)
*/
GPIO_CallbackFxn gpioCallbackFunctions[] = {
NULL, /* CC3220SF_LAUNCHXL_GPIO_SW2 */
NULL /* CC3220SF_LAUNCHXL_GPIO_SW3 */
};
/* 具体设备 GPIO_config 结构体 */
const GPIOCC32XX_Config GPIOCC32XX_config = {
.pinConfigs = (GPIO_PinConfig *)gpioPinConfigs,
.callbacks = (GPIO_CallbackFxn *)gpioCallbackFunctions,
.numberOfPinConfigs = sizeof(gpioPinConfigs)/sizeof(GPIO_PinConfig),
.numberOfCallbacks = sizeof(gpioCallbackFunctions)/sizeof(GPIO_CallbackFxn),
.intPriority = (~0)
};
/*!
* @def CC3220SF_LAUNCHXL_GPIOName
* @brief Enum of GPIO names on the CC3220SF_LAUNCHXL dev board
*/
typedef enum CC3220SF_LAUNCHXL_GPIOName {
CC3220SF_LAUNCHXL_GPIO_SW2 = 0,
CC3220SF_LAUNCHXL_GPIO_SW3,
CC3220SF_LAUNCHXL_GPIO_LED_D7,
CC3220SF_LAUNCHXL_GPIOCOUNT
} CC3220SF_LAUNCHXL_GPIOName;
/*
* 赋予一个端口的8个引脚的用户定义引脚索引。
* 用于端口中断函数,来定位一个引脚的回调函数。
*/
typedef struct PortCallbackInfo {
/*
* 端口的8个相应的用户定义pinId索引
*/
uint8_t pinIndex[NUM_PINS_PER_PORT];
} PortCallbackInfo;
/*
* portCallbackInfos表,每端口一个。
*/
static PortCallbackInfo gpioCallbackInfo[NUM_PORTS];
void GPIO_setCallback(uint_least8_t index, GPIO_CallbackFxn callback)
{
uint32_t pinNum;
uint32_t portIndex;
PinConfig *config = (PinConfig *) &GPIOCC32XX_config.pinConfigs[index];
DebugP_assert(initCalled && index < GPIOCC32XX_config.numberOfCallbacks);
/*
* 此段代码将回调函数索引号加入gpioCallbackInfo数组。
*/
pinNum = getPinNumber(config->pin);
portIndex = config->port & PORT_MASK;
if (callback == NULL) {
gpioCallbackInfo[portIndex].pinIndex[pinNum] =
CALLBACK_INDEX_NOT_CONFIGURED;
}
else {
gpioCallbackInfo[portIndex].pinIndex[pinNum] = index;
}
/*
* 此段代码将回调函数加入 callBackFunctions 数组.
*/
if (GPIOCC32XX_config.callbacks[index] != callback) {
GPIOCC32XX_config.callbacks[index] = callback;
}
}
GPIO_PinConfig gpioPinConfigs[] = {
GPIOCC32XX_GPIO_22 | GPIO_CFG_INPUT | GPIO_CFG_IN_INT_RISING, //SW2
GPIOCC32XX_GPIO_13 | GPIO_CFG_INPUT | GPIO_CFG_IN_INT_RISING, //SW3
GPIOCC32XX_GPIO_09 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
GPIOCC32XX_GPIO_10 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
GPIOCC32XX_GPIO_11 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
};
typedef enum CC3220SF_LAUNCHXL_GPIOName{
CC3220SF_LAUNCHXL_GPIO_SW2 = 0,
CC3220SF_LAUNCHXL_GPIO_SW3,
CC3220SF_LAUNCHXL_GPIO_LED_09,
CC3220SF_LAUNCHXL_GPIO_LED_10,
CC3220SF_LAUNCHXL_GPIO_LED_11,
CC3220SF_LAUNCHXL_GPIOCOUNT
} CC3220SF_LAUNCHXL_GPIOName;
#ifndef __BOARD_H
#define __BOARD_H
#ifdef __cplusplus
extern "C" {
#endif
#include "CC3220SF_LAUNCHXL.h"
#define BUTTON0 CC3220SF_LAUNCHXL_GPIO_SW2
#define BUTTON1 CC3220SF_LAUNCHXL_GPIO_SW3
#define LED_ON CC3220SF_LAUNCHXL_GPIO_LED_ON
#define LED_OFF CC3220SF_LAUNCHXL_GPIO_LED_OFF
#define RED_LED CC3220SF_LAUNCHXL_GPIO_LED_09
#define YELLOW_LED CC3220SF_LAUNCHXL_GPIO_LED_10
#define GREEN_LED CC3220SF_LAUNCHXL_GPIO_LED_11
#ifdef __cplusplus
}
#endif
#endif /* __BOARD_H */
#include
#include
#include "Board.h"
#include
//Button0中断服务函数
void Button0_isr(uint_least8_t index)
{
GPIO_toggle(RED_LED);
}
//Button1中断服务函数
void Button1_isr(uint_least8_t index)
{
GPIO_toggle(GREEN_LED);
}
int main(void)
{
GPIO_init();
//关联BUTTON0的中断回调函数
GPIO_setCallback(BUTTON0, Button0_isr);
GPIO_enableInt(BUTTON0);
//关联BUTTON1的中断回调函数
GPIO_setCallback(BUTTON1, Button1_isr);
GPIO_enableInt(BUTTON1);
BIOS_start();
return (0);
}
GPIO_PinConfig gpioPinConfigs[] =
{
GPIOCC32XX_GPIO_22 | GPIO_CFG_INPUT | GPIO_CFG_IN_INT_RISING, //SW2
GPIOCC32XX_GPIO_13 | GPIO_CFG_INPUT | GPIO_CFG_IN_INT_RISING, //SW3
GPIOCC32XX_GPIO_08 | GPIO_CFG_INPUT | GPIO_CFG_IN_INT_RISING, //外接SW
GPIOCC32XX_GPIO_09 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
GPIOCC32XX_GPIO_10 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
GPIOCC32XX_GPIO_11 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
};
GPIO_CallbackFxn gpioCallbackFunctions[] =
{
NULL, /* CC3220SF_LAUNCHXL_GPIO_SW2 */
NULL, /* CC3220SF_LAUNCHXL_GPIO_SW3 */
NULL /* CC3220SF_LAUNCHXL_GPIO_EXTERNAL_SW */
};
typedef enum CC3220SF_LAUNCHXL_GPIOName {
CC3220SF_LAUNCHXL_GPIO_SW2 = 0,
CC3220SF_LAUNCHXL_GPIO_SW3,
CC3220SF_LAUNCHXL_GPIO_EXTERNAL_SW,
CC3220SF_LAUNCHXL_GPIO_LED_09,
CC3220SF_LAUNCHXL_GPIO_LED_10,
CC3220SF_LAUNCHXL_GPIO_LED_11,
CC3220SF_LAUNCHXL_GPIOCOUNT
} CC3220SF_LAUNCHXL_GPIOName;
#ifndef __BOARD_H
#define __BOARD_H
#ifdef __cplusplus
extern "C" {
#endif
#include "CC3220SF_LAUNCHXL.h"
#define BUTTON0 CC3220SF_LAUNCHXL_GPIO_SW2
#define BUTTON1 CC3220SF_LAUNCHXL_GPIO_SW3
#define BUTTON2 CC3220SF_LAUNCHXL_GPIO_EXTERNAL_SW
#define LED_ON CC3220SF_LAUNCHXL_GPIO_LED_ON
#define LED_OFF CC3220SF_LAUNCHXL_GPIO_LED_OFF
#define RED_LED CC3220SF_LAUNCHXL_GPIO_LED_09
#define YELLOW_LED CC3220SF_LAUNCHXL_GPIO_LED_10
#define GREEN_LED CC3220SF_LAUNCHXL_GPIO_LED_11
#ifdef __cplusplus
}
#endif
#endif /* __BOARD_H */
#include
#include
#include
#include
#include "Board.h"
#include
#define DELAY_TIME (0x18fff)
void delay(int temp)
{
int i = 0;
for (i = 0; i < temp; i++);
}
//Button0中断服务函数
void Button0_isr(uint_least8_t index)
{
delay(DELAY_TIME); //8毫秒延时
if(GPIO_read(index))
{
GPIO_toggle(RED_LED);
}
}
//Button1中断服务函数
void Button1_isr(uint_least8_t index)
{
delay(DELAY_TIME); //8毫秒延时
if(GPIO_read(index))
{
GPIO_toggle(GREEN_LED);
}
}
void Button2_isr(uint_least8_t index)
{
delay(DELAY_TIME); //8毫秒延时
if(GPIO_read(index))
{
GPIO_toggle(YELLOW_LED);
}
}
int main(void)
{
GPIO_init();
//关联BUTTON0的中断回调函数
GPIO_setCallback(BUTTON0, Button0_isr);
GPIO_enableInt(BUTTON0);
//关联BUTTON1的中断回调函数
GPIO_setCallback(BUTTON1, Button1_isr);
GPIO_enableInt(BUTTON1);
//关联外接BUTTON2的中断回调函数
GPIO_setCallback(BUTTON2, Button2_isr);
GPIO_enableInt(BUTTON2);
BIOS_start();
return (0);
}
现在,终于可以使用按钮控制黄灯了。这回添加了消抖代码,虽然代码不是很正规,但用起来舒服了很多。这个消抖延时时间经过试验,基本可以达到百分之百的命中。