赞
踩
接着学习F52352芯片。这次写的是触发外部中断控制LED亮灭的程序。这个外部中断有15个中断号,即EXTI0-15,还是很多的。初始化过程中需要配置EXTI 来源选择寄存器 0 – ESSR0或是EXTI 来源选择寄存器 1– ESSR1选择中断来源。这个是一一对应的关系,比如选着了GPIOA的引脚4,那么就同时选择了EXTI4中断,同时中断服务函数也是要写EXTI4。相关引脚也需要复用模式才能使用。
当然由于在ht32_cm0plus_misc.h中找不到NVIC的初始化函数以及关于控制中断优先级的函数,所以相关代码就没有涉及到中断优先级的设置。
主要代码:
(1)EXIT的初始化
- #include "exti.h"
-
- static void EXTI_CKCU_Config()
- {
- CKCU_PeripClockConfig_TypeDef CKCUClock;
-
- CKCUClock.Bit.PA = 1;
- CKCUClock.Bit.EXTI = 1;
- CKCUClock.Bit.AFIO = 1;
-
- CKCU_PeripClockConfig(CKCUClock, ENABLE);
- }
-
- static void EXTI_GPIO_Config()
- {
- HT_GPIO_TypeDef* GPIOx;
-
- GPIOx = HT_GPIOA;
-
- AFIO_GPxConfig(EXTI_KEY1_GPIO_ID, EXTI_KEY1_GPIO_PIN, EXTI_KEY1_AFIO_MODE);
-
- /* GPIOBx位被选作EXTIn的来源信号 */
- AFIO_EXTISourceConfig((AFIO_EXTI_CH_Enum)EXTI_KEY1_CHANNEL, (AFIO_ESS_Enum)EXTI_KEY1_GPIO_ID);
-
- /* 输入使能函数 */
- GPIO_InputConfig(GPIOx, EXTI_KEY1_GPIO_PIN, ENABLE);
-
- /* Configure the GPIO pin */
- GPIO_PullResistorConfig(GPIOx, EXTI_KEY1_GPIO_PIN, GPIO_PR_DISABLE);
- GPIO_DirectionConfig(GPIOx, EXTI_KEY1_GPIO_PIN, GPIO_DIR_IN);
- }
-
- static void EXTI_Config()
- {
- EXTI_InitTypeDef EXTI_InitStruct;
-
- EXTI_InitStruct.EXTI_Channel = EXTI_KEY1_CHANNEL;
- EXTI_InitStruct.EXTI_Debounce = EXTI_DEBOUNCE_DISABLE;
- EXTI_InitStruct.EXTI_DebounceCnt = 0;
-
- /* EXTI 中断配置寄存器 n – EXTICFGRn */
- EXTI_InitStruct.EXTI_IntType = EXTI_NEGATIVE_EDGE;
-
- EXTI_Init(&EXTI_InitStruct);
-
- EXTI_IntConfig(EXTI_KEY1_CHANNEL, ENABLE);
-
- /* 允许中断函数在检测到中断时起作用 */
- NVIC_EnableIRQ(EXTI4_15_IRQn);
- }
-
- void EXTI_Initia()
- {
- EXTI_CKCU_Config();
- EXTI_GPIO_Config();
- EXTI_Config();
- }
NVIC_EnableIRQ(EXTI4_15_IRQn)这个必须要有,虽然找不到在哪里定义的......另外LED的初始化剑第二篇(https://blog.csdn.net/Unlimited_Bit/article/details/86657366)
(2)中断服务函数
- void EXTI4_15_IRQHandler(void)
- {
- if (EXTI_GetEdgeStatus(EXTI_KEY1_CHANNEL, EXTI_EDGE_NEGATIVE))
- {
- EXTI_ClearEdgeFlag(EXTI_KEY1_CHANNEL);
- LEDToggle(HT_GPIOC, GPIO_PIN_14);
- }
-
- }
(3)main函数
- #include "ht32.h"
- #include "ht32_board.h"
- #include "led.h"
- #include "exti.h"
-
- int main()
- {
- LED_Init();
- EXTI_Initia();
-
- while(1)
- {
-
- }
- }
对硬件方面(如引脚、模式选择等的封装)
- #ifndef _EXTI_H
- #define _EXTI_H
-
- #include "ht32f5xxxx_01.h"
-
- #define EXTI_KEY1_GPIO_ID (GPIO_PA)
- #define EXTI_KEY1_GPIO_PIN (GPIO_PIN_4)
- #define EXTI_KEY1_AFIO_MODE (AFIO_FUN_GPIO)
- #define EXTI_KEY1_CHANNEL (4)
-
- #define EXTI_KEY2_GPIO_ID (GPIO_PA)
- #define EXTI_KEY2_GPIO_PIN (GPIO_PIN_1)
- #define EXTI_KEY2_AFIO_MODE (AFIO_FUN_GPIO)
- #define EXTI_KEY2_CHANNEL (1)
-
- #define EXTI_KEY3_GPIO_ID (GPIO_PA)
- #define EXTI_KEY3_GPIO_PIN (GPIO_PIN_2)
- #define EXTI_KEY3_AFIO_MODE (AFIO_FUN_GPIO)
- #define EXTI_KEY3_CHANNEL (2)
-
- void EXTI_Initia(void);
-
- #endif
用到的库函数
(1)中断来源选择函数
- /*********************************************************************************************************//**
- * @brief Select the GPIO pin to be used as EXTI channel.
- * @param AFIO_EXTI_CH_n: Specify the EXTI channel to be configured.
- * This parameter can be AFIO_EXTI_CH_x.
- * @param AFIO_ESS_Px: Specify the GPIO port to be used for EXTI channel.
- * This parameter can be AFIO_ESS_Px where x can be A ~ E.
- * @retval None
- ************************************************************************************************************/
- void AFIO_EXTISourceConfig(AFIO_EXTI_CH_Enum AFIO_EXTI_CH_n, AFIO_ESS_Enum AFIO_ESS_Px)
(2)中断初始化函数
- typedef enum
- {
- EXTI_DEBOUNCE_DISABLE = 0x0,
- EXTI_DEBOUNCE_ENABLE = 0x1
- } EXTIDeb_TypeDef;
-
- typedef enum
- {
- EXTI_LOW_LEVEL = 0x0,
- EXTI_HIGH_LEVEL = 0x1,
- EXTI_NEGATIVE_EDGE = 0x2,
- EXTI_POSITIVE_EDGE = 0x3,
- EXTI_BOTH_EDGE = 0x4
- } EXTIInterrupt_TypeDef;
-
-
- typedef struct
- {
- u32 EXTI_Channel;
- EXTIDeb_TypeDef EXTI_Debounce;
- u16 EXTI_DebounceCnt;
- EXTIInterrupt_TypeDef EXTI_IntType;
- } EXTI_InitTypeDef;
-
- /*********************************************************************************************************//**
- * @brief Initialize the EXTI peripheral.
- * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure.
- * @retval None
- ************************************************************************************************************/
- void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)
在此说明下EXTI_Debounce、EXTI_IntType都是配置EXTI 中断 配置寄存器 n – EXTICFGRn, n = 0 ~ 15。EXTI_Debounce决定是否是能去抖电路,EXTI_IntType选择触发类型。
(3)中断使能函数
- /*********************************************************************************************************//**
- * @brief Enable or Disable the specified EXTI channelx interrupts.
- * @param EXTI_Channel: specify the EXTI channel.
- * This parameter can be one of the following values:
- * @arg EXTI_CHANNEL_0
- * @arg EXTI_CHANNEL_1
- * @arg EXTI_CHANNEL_2
- * @arg EXTI_CHANNEL_3
- * @arg EXTI_CHANNEL_4
- * @arg EXTI_CHANNEL_5
- * @arg EXTI_CHANNEL_6
- * @arg EXTI_CHANNEL_7
- * @arg EXTI_CHANNEL_8
- * @arg EXTI_CHANNEL_9
- * @arg EXTI_CHANNEL_10
- * @arg EXTI_CHANNEL_11
- * @arg EXTI_CHANNEL_12
- * @arg EXTI_CHANNEL_13
- * @arg EXTI_CHANNEL_14
- * @arg EXTI_CHANNEL_15
- * @param NewState: This parameter can be ENABLE or DISABLE.
- * @retval None
- ************************************************************************************************************/
- void EXTI_IntConfig(u32 EXTI_Channel, ControlStatus NewState)
(4)检测中断源状态函数
- /*********************************************************************************************************//**
- * @brief Enable or Disable the specified EXTI channelx interrupts.
- * @param EXTI_Channel: specify the EXTI channel.
- * This parameter can be one of the following values:
- * @arg EXTI_CHANNEL_0
- * @arg EXTI_CHANNEL_1
- * @arg EXTI_CHANNEL_2
- * @arg EXTI_CHANNEL_3
- * @arg EXTI_CHANNEL_4
- * @arg EXTI_CHANNEL_5
- * @arg EXTI_CHANNEL_6
- * @arg EXTI_CHANNEL_7
- * @arg EXTI_CHANNEL_8
- * @arg EXTI_CHANNEL_9
- * @arg EXTI_CHANNEL_10
- * @arg EXTI_CHANNEL_11
- * @arg EXTI_CHANNEL_12
- * @arg EXTI_CHANNEL_13
- * @arg EXTI_CHANNEL_14
- * @arg EXTI_CHANNEL_15
- * @param NewState: This parameter can be ENABLE or DISABLE.
- * @retval None
- ************************************************************************************************************/
- void EXTI_IntConfig(u32 EXTI_Channel, ControlStatus NewState)
(5)中断标志位清除函数
- /*********************************************************************************************************//**
- * @brief Clear the specified EXTI channelx edge flag.
- * @param EXTI_Channel: specify the EXTI channel.
- * This parameter can be one of the following values:
- * @arg EXTI_CHANNEL_0
- * @arg EXTI_CHANNEL_1
- * @arg EXTI_CHANNEL_2
- * @arg EXTI_CHANNEL_3
- * @arg EXTI_CHANNEL_4
- * @arg EXTI_CHANNEL_5
- * @arg EXTI_CHANNEL_6
- * @arg EXTI_CHANNEL_7
- * @arg EXTI_CHANNEL_8
- * @arg EXTI_CHANNEL_9
- * @arg EXTI_CHANNEL_10
- * @arg EXTI_CHANNEL_11
- * @arg EXTI_CHANNEL_12
- * @arg EXTI_CHANNEL_13
- * @arg EXTI_CHANNEL_14
- * @arg EXTI_CHANNEL_15
- * @retval None
- ************************************************************************************************************/
- void EXTI_ClearEdgeFlag(u32 EXTI_Channel)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。