zephyr学习笔记---CC3200---Button

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/abatei/article/details/70182511
zephyr上有button的示例,在【\samples\basic\button】目录下,我做了一些修改,两个Button都使用了,另外也加入了LED,方便调试。功能很简单,就是在按下按钮时向打印一段文字(可使用串口接收),同时切换LED灯的亮灭。

代码如下:

#include <zephyr.h>
#include <board.h>
#include <device.h>
#include <gpio.h>
#include <misc/util.h>
#include <misc/printk.h>

/* change to use another GPIO pin interrupt config */
#define EDGE    (GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW)
/* change this to enable pull-up/pull-down */
#define PULL_UP 0
/* Sleep time */
#define SLEEP_TIME  500

struct device *devLed0, *devLed2;
unsigned char led0State = 0, led2State = 0;
//Button2中断服务函数
void sw2_pressed(struct device *gpiob, struct gpio_callback *cb,
            uint32_t pins)
{
    printk("You have pressed Button2\n");
    led2State = led2State ? 0 : 1;
    gpio_pin_write(devLed2, LED2_GPIO_PIN, led2State);  
}
//Button3中断服务函数
void sw3_pressed(struct device *gpiob, struct gpio_callback *cb,
            uint32_t pins)
{
    printk("You have pressed Button3\n");
    led0State = led0State ? 0 : 1;
    gpio_pin_write(devLed0, LED0_GPIO_PIN, led0State);
}

static struct gpio_callback gpio_cb2, gpio_cb3;

void main(void)
{
    struct device *gpiob2, *gpiob3;
    printk("Press the user defined button on the board\n");
    
    //Button2初始化
    gpiob2 = device_get_binding(SW2_GPIO_NAME);
    if (!gpiob2) {
        printk("error\n");
        return;
    }
    gpio_pin_configure(gpiob2, SW2_GPIO_PIN,
               GPIO_DIR_IN | GPIO_INT |  PULL_UP | EDGE);
    gpio_init_callback(&gpio_cb2, sw2_pressed, BIT(SW2_GPIO_PIN));
    gpio_add_callback(gpiob2, &gpio_cb2);
    gpio_pin_enable_callback(gpiob2, SW2_GPIO_PIN);

    //Button3初始化
    gpiob3 = device_get_binding(SW3_GPIO_NAME);
    if (!gpiob3) {
        printk("error\n");
        return;
    }
    gpio_pin_configure(gpiob3, SW3_GPIO_PIN,
               GPIO_DIR_IN | GPIO_INT |  PULL_UP | EDGE);
    gpio_init_callback(&gpio_cb3, sw3_pressed, BIT(SW3_GPIO_PIN));
    gpio_add_callback(gpiob3, &gpio_cb3);
    gpio_pin_enable_callback(gpiob3, SW3_GPIO_PIN);

    //LED 初始化
    devLed0 = device_get_binding(LED0_GPIO_PORT);
    devLed2 = device_get_binding(LED2_GPIO_PORT);
    gpio_pin_configure(devLed0, LED0_GPIO_PIN, GPIO_DIR_OUT);
    gpio_pin_configure(devLed2, LED2_GPIO_PIN, GPIO_DIR_OUT);

    while (1) {
        uint32_t val = 0;

        gpio_pin_read(gpiob2, SW2_GPIO_PIN, &val);
        gpio_pin_read(gpiob3, SW3_GPIO_PIN, &val);
        k_sleep(SLEEP_TIME);
    }
}

串口调试助手的串口设置请参照下图,运行效果如下图所示:


感觉使用起来还是非常麻烦的,不如TI-RTOS及Contiki方便。当然,需要时可以自己再包装一层。

展开阅读全文

没有更多推荐了,返回首页