rtthread gpio应用

1、获取pin编号

#define PIN_NUM  GET_PIN(F,10)

GET_PIN 是在drv_gpio.h里面定义的

#define GET_PIN(PORTx,PIN) (rt_base_t)((16 * ( ((rt_base_t)__STM32_PORT(PORTx) - (rt_base_t)GPIOA)/(0x0400UL) )) + PIN)

2、设置gpio模式   rt_pin_mode(KEY_PIN, PIN_MODE_INPUT_PULLUP);

1 输入:上拉、下拉、模拟、浮动

2 输出:上拉、下拉、推挽、开漏

3 中断:上升沿、下降沿、双沿、高电平、低电平触发

#define PIN_LOW 0x00
#define PIN_HIGH 0x01

#define PIN_MODE_OUTPUT 0x00
#define PIN_MODE_INPUT 0x01
#define PIN_MODE_INPUT_PULLUP 0x02
#define PIN_MODE_INPUT_PULLDOWN 0x03
#define PIN_MODE_OUTPUT_OD 0x04

#define PIN_IRQ_MODE_RISING 0x00
#define PIN_IRQ_MODE_FALLING 0x01
#define PIN_IRQ_MODE_RISING_FALLING 0x02
#define PIN_IRQ_MODE_HIGH_LEVEL 0x03
#define PIN_IRQ_MODE_LOW_LEVEL 0x04

#define PIN_IRQ_DISABLE 0x00
#define PIN_IRQ_ENABLE 0x01

3、读取或者设置gpio输出电平

rt_pin_write(PIN_NUM, PIN_HIGH);//输出高电平

4、设置gpio中断模式

rt_pin_mode(KEY_PIN, PIN_MODE_INPUT_PULLUP);
rt_pin_attach_irq(KEY_PIN,PIN_IRQ_MODE_FALLING,key_callback,(void*)"lcd backlight on");
rt_pin_irq_enable(KEY_PIN,PIN_IRQ_ENABLE);

void key_callback(void *args)//中断回调函数
{
char *a = args;

}

猜你喜欢

转载自www.cnblogs.com/lazybeee/p/11161660.html