- Program Size: Code=1356 RO-data=224 RW-data=16 ZI-data=1160
- ".\Objects\Demo.axf" - 0 Error(s), 0 Warning(s).
- Build Time Elapsed: 00:00:01
11、编写gpio.h内容如下:
- #ifndef _GPIO_H
- #define _GPIO_H
- #include "ac780x_gpio.h"
- #define LED1_PORT (GPIOA)
- #define LED1_PIN (GPIO_PIN2)
- #define LED2_PORT (GPIOA)
- #define LED2_PIN (GPIO_PIN3)
- /*LED1动作定义.*/
- #define LED1_ON do{GPIO_SetPinLevel(LED1_PORT, LED1_PIN, GPIO_LEVEL_HIGH);}while(0)
- #define LED1_OFF do{GPIO_SetPinLevel(LED1_PORT, LED1_PIN, GPIO_LEVEL_LOW);}while(0)
- #define LED1_TOGGLE do{if(GPIO_GetPinLevel(LED1_PORT, LED1_PIN)){LED1_OFF;}else{LED1_ON;}}while(0)
- /*LED2动作定义.*/
- #define LED2_ON do{GPIO_SetPinLevel(LED2_PORT, LED2_PIN, GPIO_LEVEL_HIGH);}while(0)
- #define LED2_OFF do{GPIO_SetPinLevel(LED2_PORT, LED2_PIN, GPIO_LEVEL_LOW);}while(0)
- #define LED2_TOGGLE do{if(GPIO_GetPinLevel(LED2_PORT, LED2_PIN)){LED2_OFF;}else{LED2_ON;}}while(0)
- #define KEY_PORT (GPIOA)
- #define KEY_PIN (GPIO_PIN11)
- #define NMI_PORT (GPIOB)
- #define NMI_PIN (GPIO_PIN2)
- void GPIO_LedInit(void);
- void GPIO_KeyInit(void);
- #endif /* _GPIO_H */
12、编写gpio.c内容如下:
- /* =========================================== Includes =========================================== */
- #include "gpio.h"
- #include "stdbool.h"
- /* ============================================ Define ============================================ */
- #define GET_KEY1_STS() (bool)(GPIO_GetPinLevel(KEY_PORT, KEY_PIN))
- #define GET_NMI_STS() (bool)(GPIO_GetPinLevel(NMI_PORT, NMI_PIN))
- void EXTI_Key_Callback(void *device, uint32_t wpara, uint32_t lpara)
- {
- if (GET_KEY1_STS() == false)
- {
- LED1_TOGGLE;
- LED2_TOGGLE;
- }
- }
- void GPIO_LedInit(void)
- {
- /*初始化引脚功能,如果引脚上电后默认为GPIO,可省略掉初始化步骤.
- 有部分引脚上电默认为非GPIO,则必须选择其功能为GPIO才能作为GPIO使用.*/
- GPIO_SetFunc(LED1_PORT, LED1_PIN, GPIO_FUN0);/*功能复用选择.*/
- GPIO_SetFunc(LED2_PORT, LED2_PIN, GPIO_FUN0);
- /*设置LED引脚为GPIO输出.*/
- GPIO_SetDir(LED1_PORT, LED1_PIN, GPIO_OUT);
- GPIO_SetDir(LED2_PORT, LED2_PIN, GPIO_OUT);
- LED1_ON;
- LED2_OFF;
- }
- void GPIO_KeyInit(void)
- {
- /*初始化引脚功能,如果引脚上电后默认为GPIO,可省略掉初始化步骤.
- 有部分引脚上电默认为非GPIO,则必须选择其功能为GPIO才能作为GPIO使用.*/
- GPIO_SetFunc(KEY_PORT, KEY_PIN, GPIO_FUN0); /*功能复用选择.*/
- /*设置按键引脚为输入,检测按键动作.*/
- GPIO_SetDir(KEY_PORT, KEY_PIN, GPIO_IN); /*设置为输入模式.*/
- /*使能KEY1的外部中断,设置为下降沿产生中断,当按下按键时产生中断.*/
- GPIO_EnableExtInterrupt(KEY_PORT, KEY_PIN, EXTI_TRIGGER_FALLING);
- /*设置外部中断,中断回调函数.*/
- GPIO_SetCallback(KEY_PIN, EXTI_Key_Callback);
- }
- /* ============================================= EOF ============================================== */
12、修改main.c如下:
- #include "gpio.h"
- int main(void)
- {
- InitDelay();
- InitDebug();
- GPIO_LedInit();
- GPIO_KeyInit();
- printf("Hi! This is GPIO external IRQ demo !\r\n");
- while (1)
- {
- }
- }
编译后无警告无错误
- Program Size: Code=3960 RO-data=224 RW-data=48 ZI-data=1160
- ".\Objects\Demo.axf" - 0 Error(s), 0 Warning(s).
- Build Time Elapsed: 00:00:01
12、选择下载器为stlink:
13、下载后就可以实验按键控制LED的亮与灭了。
【总结】这次从头创建工程,熟悉AC7802x的工程结构。同时学习了gpio中断的例程。