在线时间954 小时
UID336767
注册时间2011-12-29
NXP金币725
TA的每日心情 | 开心 2018-7-23 21:04 |
---|
签到天数: 103 天 连续签到: 1 天 [LV.6]常住居民II
金牌会员
 
- 积分
- 16706
- 最后登录
- 1970-1-1
|
接下来练习一下KEY0控制LED0,每次按下一次KEY0,LED0状态取反一次。
首先查看一下电路,LED0接pio0_15,KEY0接pio1_8。
使用配置工具建立工程。
设置引脚。
设置时钟。
完成程序。
- /**
- * This is template for main module created by MCUXpresso Project Generator. Enjoy!
- **/
- #include "board.h"
- #include "pin_mux.h"
- #include "clock_config.h"
- #include "fsl_gpio.h"
- /*!
- * @brief Application entry point.
- */
- int main(void) {
- /* Init board hardware. */
- BOARD_InitBootPins();
- BOARD_InitBootClocks();
- BOARD_InitDebugConsole();
- #ifdef SDK_PRIMARY_CORE
- BOARD_StartSecondaryCore();
- #endif
- /* Add your code here */
- gpio_pin_config_t led0Config = {
- kGPIO_DigitalOutput,
- 0,
- };
-
- gpio_pin_config_t key0Config = {
- kGPIO_DigitalInput,
- 0,
- };
-
- CLOCK_EnableClock(kCLOCK_Gpio0);
- CLOCK_EnableClock(kCLOCK_Gpio1);
-
- GPIO_PinInit(BOARD_INITPINS_LED0_GPIO, BOARD_INITPINS_LED0_PORT, BOARD_INITPINS_LED0_GPIO_PIN, &led0Config);
- GPIO_PinInit(BOARD_INITPINS_KEY0_GPIO, BOARD_INITPINS_KEY0_PORT, BOARD_INITPINS_KEY0_GPIO_PIN, &key0Config);
- for(;;) { /* Infinite loop to avoid leaving the main function */
- // __asm("NOP"); /* something to use as a breakpoint stop while looping */
- if ((GPIO_ReadPinsInput(BOARD_INITPINS_KEY0_GPIO, BOARD_INITPINS_KEY0_PORT) & (1U << BOARD_INITPINS_KEY0_GPIO_PIN)) != (1U << BOARD_INITPINS_KEY0_GPIO_PIN))
- {
- GPIO_TogglePinsOutput(BOARD_INITPINS_LED0_GPIO, BOARD_INITPINS_LED0_PORT, 1U << BOARD_INITPINS_LED0_GPIO_PIN);
- do
- {
- ;
- } while ((GPIO_ReadPinsInput(BOARD_INITPINS_KEY0_GPIO, BOARD_INITPINS_KEY0_PORT) & (1U << BOARD_INITPINS_KEY0_GPIO_PIN)) != (1U << BOARD_INITPINS_KEY0_GPIO_PIN));
- }
- }
- }
复制代码 编译下载,每次按下一次KEY0,LED0状态取反一次。
完整工程:
KEY0_MCUXpressoIDE_Project_cm4.rar
(4.72 MB, 下载次数: 13)
|
|