在OKdo E1开发板上配置了LED和KEY,通过它们可以学习和掌握I/O口的使用方法,其原理图见图1所示。
图1 LED和KEY原理图
由此我们可以获知它们与MCU的连接关系如下: LEDR ---PIO1_4 LEDB ---PIO1_6 LEDG ---PIO1_7 WAKEUP --- PIO1_18 USER --- PIO1_9 ISP --- PIO0_5
为此LED和KEY的初始定义如下: LED的引脚定义: #defineLEDR_PORT 1u #defineLEDR_PIN 4u #defineLEDR_FUNC (IOCON_FUNC0 | IOCON_MODE_PULLUP |IOCON_DIGITAL_EN)
#defineLEDG_PORT 1u #defineLEDG_PIN 7u #defineLEDG_FUNC (IOCON_FUNC0 | IOCON_MODE_PULLUP | IOCON_DIGITAL_EN)
#defineLEDB_PORT 1u #defineLEDB_PIN 6u #defineLEDB_FUNC (IOCON_FUNC0 | IOCON_MODE_PULLUP |IOCON_DIGITAL_EN)
#defineLEDR_NUM 1u #defineLEDG_NUM 2u #defineLEDB_NUM 3u
KEY的引脚定义: #defineKEY_S2_PORT 0u #defineKEY_S2_PIN 5u #defineKEY_S2_FUNC (IOCON_FUNC0 |IOCON_MODE_PULLUP | IOCON_DIGITAL_EN)
#defineKEY_S3_PORT 1u #defineKEY_S3_PIN 18u #defineKEY_S3_FUNC (IOCON_FUNC0 | IOCON_MODE_PULLUP| IOCON_DIGITAL_EN)
#defineKEY_S1_PORT 1u #defineKEY_S1_PIN 9u #defineKEY_S1_FUNC (IOCON_FUNC0 |IOCON_MODE_PULLUP | IOCON_DIGITAL_EN)
#defineKEY2_NUM 0x02 #defineKEY3_NUM 0x01 #define KEY1_NUM 0x04
LED的初始化函数: - void led_init(void)
- {
- gpio_pin_config_t gpioPinConfig;
- gpioPinConfig.pinDirection = kGPIO_DigitalOutput;
- gpioPinConfig.outputLogic = 1u; /* output high as default. */
- GPIO_PinInit (GPIO, LEDR_PORT, LEDR_PIN, &gpioPinConfig);
- GPIO_PinInit (GPIO, LEDG_PORT, LEDG_PIN, &gpioPinConfig);
- GPIO_PinInit (GPIO, LEDB_PORT, LEDB_PIN, &gpioPinConfig);
- }
复制代码
KEY的初始化函数: - void key_init(void)
- {
- static uint32_t i = 0;
- gpio_pin_config_t gpioPinConfig;
- gpioPinConfig.pinDirection = kGPIO_DigitalInput;
- gpioPinConfig.outputLogic = 1u;
- CLOCK_EnableClock(kCLOCK_Iocon);
- IOCON->PIO[KEY_S2_PORT][KEY_S2_PIN] = KEY_S2_FUNC;
- IOCON->PIO[KEY_S3_PORT][KEY_S3_PIN] = KEY_S3_FUNC;
- IOCON->PIO[KEY_S1_PORT][KEY_S1_PIN] = KEY_S1_FUNC;
- CLOCK_DisableClock(kCLOCK_Iocon);
- GPIO_PinInit (GPIO, KEY_S2_PORT, KEY_S2_PIN, &gpioPinConfig);
- GPIO_PinInit (GPIO, KEY_S3_PORT, KEY_S3_PIN, &gpioPinConfig);
- GPIO_PinInit (GPIO, KEY_S1_PORT, KEY_S1_PIN, &gpioPinConfig);
- }
复制代码
KEY控制LED的主程序: - int main(void)
- {
- volatile uint8_t s_KeyValue = 0;
- POWER_SetBodVbatLevel(kPOWER_BodVbatLevel1650mv, kPOWER_BodHystLevel50mv, false);
- CLOCK_EnableClock(kCLOCK_Gpio0);
- CLOCK_EnableClock(kCLOCK_Gpio1);
- BOARD_BootClockFROHF96M();
- led_init();
- key_init();
- while (1)
- {
- s_KeyValue = key_read();
- if( (s_KeyValue&KEY2_NUM) == KEY2_NUM) // ISP
- {
- led_set(LEDR_NUM, 0);
- }
- else
- {
- led_set(LEDR_NUM, 1);
- }
- if( (s_KeyValue&KEY3_NUM) == KEY3_NUM) // WAKE
- {
- led_set(LEDG_NUM, 0);
- }
- else
- {
- led_set(LEDG_NUM, 1);
- }
- if( (s_KeyValue&KEY1_NUM) == KEY1_NUM) // USER
- {
- led_set(LEDB_NUM, 0);
- }
- else
- {
- led_set(LEDB_NUM, 1);
- }
- }
- }
复制代码 经编译下载,其运行效果如图2~图4所示。
目标代码:
|