1 測試環境
開發板:N32L40XCL-STB V1.0
開發環境:RT-Thread studio 2.2.6
RT-Thread版本:4.0.2
2 硬件資源介紹
開發板上共有3個按鍵和3個LED如下圖所示。
GPIO輸入輸出測試:KEY1對應引腳設置為GPIO輸入,控制LED1、LED2亮滅
GPIO中斷模式測試:KEY2對應引腳設置為下降沿觸發,觸發后打印KEY2!等若干字符
LED3指示系統正常工作
3 GPIO測試代碼
關于工程創建等可參考其他人的文章,在applications文件夾中創建gpio_test.c文件并加入如下的測試代碼
#include
#include
#include
/* defined the LED1 pin: PA8 /
#define LED1_PIN GET_PIN(A, 8)
/ defined the LED2 pin: PB4 /
#define LED2_PIN GET_PIN(B, 4)
/ defined the KEY1 pin: PA4 /
#define KEY1_PIN GET_PIN(A, 4)
/ defined the KEY2 pin: PA5 */
#define KEY2_PIN GET_PIN(A, 5)
static uint8_t curr_st = 0;
static uint8_t next_st = 0;
static uint8_t led_st = 0;
static void key_led_thread_entry(void parameter)
{
while (1) {
/ led status switch /
if (rt_pin_read(KEY1_PIN) == PIN_LOW) {
rt_thread_mdelay(20);
if (rt_pin_read(KEY1_PIN) == PIN_LOW) {
next_st = 1;
} else {
next_st = 0;
}
} else {
next_st = 0;
}
/ switch on/off the led */
if ((curr_st == 0) && (next_st != 0)) {
led_st = !led_st;
} else {
led_st = led_st;
}
curr_st = next_st;
if (led_st) {
rt_pin_write(LED1_PIN, PIN_HIGH);
rt_pin_write(LED2_PIN, PIN_LOW);
} else {
rt_pin_write(LED1_PIN, PIN_LOW);
rt_pin_write(LED2_PIN, PIN_HIGH);
}
rt_thread_mdelay(20);
}
}
static void key2_callback(void *args)
{
char str = args;
rt_kprintf(str);
}
int gpio_test(void)
{
/ gpio input and output test /
rt_pin_mode(KEY1_PIN, PIN_MODE_INPUT_PULLUP);
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
rt_pin_write(LED1_PIN, PIN_LOW);
rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
rt_pin_write(LED2_PIN, PIN_HIGH);
rt_thread_t tid;
tid = rt_thread_create("key_led", key_led_thread_entry, RT_NULL, 512, 10, 5);
if (tid != RT_NULL) {
rt_thread_startup(tid);
} else {
rt_kprintf("startup the thread failed!n");
}
/ gpio irq test */
rt_pin_mode(KEY2_PIN, PIN_MODE_INPUT_PULLUP);
rt_pin_attach_irq(KEY2_PIN, PIN_IRQ_MODE_FALLING, key2_callback, (void *)"KEY2!n");
rt_pin_irq_enable(KEY2_PIN, PIN_IRQ_ENABLE);
}
INIT_APP_EXPORT(gpio_test);
-
led燈
+關注
關注
22文章
1592瀏覽量
107837 -
觸發器
+關注
關注
14文章
1996瀏覽量
61052 -
STB
+關注
關注
0文章
23瀏覽量
16571 -
GPIO
+關注
關注
16文章
1196瀏覽量
51918 -
RT-Thread
+關注
關注
31文章
1272瀏覽量
39922
發布評論請先 登錄
相關推薦
評論