1 測(cè)試環(huán)境
開發(fā)板:N32L40XCL-STB V1.0
開發(fā)環(huán)境:RT-Thread studio 2.2.6
RT-Thread版本:4.0.2
2 硬件資源介紹
開發(fā)板上共有3個(gè)按鍵和3個(gè)LED如下圖所示。
GPIO輸入輸出測(cè)試:KEY1對(duì)應(yīng)引腳設(shè)置為GPIO輸入,控制LED1、LED2亮滅
GPIO中斷模式測(cè)試:KEY2對(duì)應(yīng)引腳設(shè)置為下降沿觸發(fā),觸發(fā)后打印KEY2!等若干字符
LED3指示系統(tǒng)正常工作
3 GPIO測(cè)試代碼
關(guān)于工程創(chuàng)建等可參考其他人的文章,在applications文件夾中創(chuàng)建gpio_test.c文件并加入如下的測(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燈
+關(guān)注
關(guān)注
22文章
1592瀏覽量
107724 -
觸發(fā)器
+關(guān)注
關(guān)注
14文章
1995瀏覽量
61007 -
STB
+關(guān)注
關(guān)注
0文章
23瀏覽量
16561 -
GPIO
+關(guān)注
關(guān)注
16文章
1188瀏覽量
51821 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1259瀏覽量
39823
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論