当前位置:   article > 正文

HT32F52352学习笔记之三_ht32硬件spi

ht32硬件spi

接着学习F52352芯片。这次写的是触发外部中断控制LED亮灭的程序。这个外部中断有15个中断号,即EXTI0-15,还是很多的。初始化过程中需要配置EXTI 来源选择寄存器 0 – ESSR0或是EXTI 来源选择寄存器 1– ESSR1选择中断来源。这个是一一对应的关系,比如选着了GPIOA的引脚4,那么就同时选择了EXTI4中断,同时中断服务函数也是要写EXTI4。相关引脚也需要复用模式才能使用。


当然由于在ht32_cm0plus_misc.h中找不到NVIC的初始化函数以及关于控制中断优先级的函数,所以相关代码就没有涉及到中断优先级的设置。

主要代码:

(1)EXIT的初始化

  1. #include "exti.h"
  2. static void EXTI_CKCU_Config()
  3. {
  4. CKCU_PeripClockConfig_TypeDef CKCUClock;
  5. CKCUClock.Bit.PA = 1;
  6. CKCUClock.Bit.EXTI = 1;
  7. CKCUClock.Bit.AFIO = 1;
  8. CKCU_PeripClockConfig(CKCUClock, ENABLE);
  9. }
  10. static void EXTI_GPIO_Config()
  11. {
  12. HT_GPIO_TypeDef* GPIOx;
  13. GPIOx = HT_GPIOA;
  14. AFIO_GPxConfig(EXTI_KEY1_GPIO_ID, EXTI_KEY1_GPIO_PIN, EXTI_KEY1_AFIO_MODE);
  15. /* GPIOBx位被选作EXTIn的来源信号 */
  16. AFIO_EXTISourceConfig((AFIO_EXTI_CH_Enum)EXTI_KEY1_CHANNEL, (AFIO_ESS_Enum)EXTI_KEY1_GPIO_ID);
  17. /* 输入使能函数 */
  18. GPIO_InputConfig(GPIOx, EXTI_KEY1_GPIO_PIN, ENABLE);
  19. /* Configure the GPIO pin */
  20. GPIO_PullResistorConfig(GPIOx, EXTI_KEY1_GPIO_PIN, GPIO_PR_DISABLE);
  21. GPIO_DirectionConfig(GPIOx, EXTI_KEY1_GPIO_PIN, GPIO_DIR_IN);
  22. }
  23. static void EXTI_Config()
  24. {
  25. EXTI_InitTypeDef EXTI_InitStruct;
  26. EXTI_InitStruct.EXTI_Channel = EXTI_KEY1_CHANNEL;
  27. EXTI_InitStruct.EXTI_Debounce = EXTI_DEBOUNCE_DISABLE;
  28. EXTI_InitStruct.EXTI_DebounceCnt = 0;
  29. /* EXTI 中断配置寄存器 n – EXTICFGRn */
  30. EXTI_InitStruct.EXTI_IntType = EXTI_NEGATIVE_EDGE;
  31. EXTI_Init(&EXTI_InitStruct);
  32. EXTI_IntConfig(EXTI_KEY1_CHANNEL, ENABLE);
  33. /* 允许中断函数在检测到中断时起作用 */
  34. NVIC_EnableIRQ(EXTI4_15_IRQn);
  35. }
  36. void EXTI_Initia()
  37. {
  38. EXTI_CKCU_Config();
  39. EXTI_GPIO_Config();
  40. EXTI_Config();
  41. }

NVIC_EnableIRQ(EXTI4_15_IRQn)这个必须要有,虽然找不到在哪里定义的......另外LED的初始化剑第二篇(https://blog.csdn.net/Unlimited_Bit/article/details/86657366

(2)中断服务函数

  1. void EXTI4_15_IRQHandler(void)
  2. {
  3. if (EXTI_GetEdgeStatus(EXTI_KEY1_CHANNEL, EXTI_EDGE_NEGATIVE))
  4. {
  5. EXTI_ClearEdgeFlag(EXTI_KEY1_CHANNEL);
  6. LEDToggle(HT_GPIOC, GPIO_PIN_14);
  7. }
  8. }

(3)main函数

  1. #include "ht32.h"
  2. #include "ht32_board.h"
  3. #include "led.h"
  4. #include "exti.h"
  5. int main()
  6. {
  7. LED_Init();
  8. EXTI_Initia();
  9. while(1)
  10. {
  11. }
  12. }

对硬件方面(如引脚、模式选择等的封装)

  1. #ifndef _EXTI_H
  2. #define _EXTI_H
  3. #include "ht32f5xxxx_01.h"
  4. #define EXTI_KEY1_GPIO_ID (GPIO_PA)
  5. #define EXTI_KEY1_GPIO_PIN (GPIO_PIN_4)
  6. #define EXTI_KEY1_AFIO_MODE (AFIO_FUN_GPIO)
  7. #define EXTI_KEY1_CHANNEL (4)
  8. #define EXTI_KEY2_GPIO_ID (GPIO_PA)
  9. #define EXTI_KEY2_GPIO_PIN (GPIO_PIN_1)
  10. #define EXTI_KEY2_AFIO_MODE (AFIO_FUN_GPIO)
  11. #define EXTI_KEY2_CHANNEL (1)
  12. #define EXTI_KEY3_GPIO_ID (GPIO_PA)
  13. #define EXTI_KEY3_GPIO_PIN (GPIO_PIN_2)
  14. #define EXTI_KEY3_AFIO_MODE (AFIO_FUN_GPIO)
  15. #define EXTI_KEY3_CHANNEL (2)
  16. void EXTI_Initia(void);
  17. #endif

用到的库函数

(1)中断来源选择函数

  1. /*********************************************************************************************************//**
  2. * @brief Select the GPIO pin to be used as EXTI channel.
  3. * @param AFIO_EXTI_CH_n: Specify the EXTI channel to be configured.
  4. * This parameter can be AFIO_EXTI_CH_x.
  5. * @param AFIO_ESS_Px: Specify the GPIO port to be used for EXTI channel.
  6. * This parameter can be AFIO_ESS_Px where x can be A ~ E.
  7. * @retval None
  8. ************************************************************************************************************/
  9. void AFIO_EXTISourceConfig(AFIO_EXTI_CH_Enum AFIO_EXTI_CH_n, AFIO_ESS_Enum AFIO_ESS_Px)

(2)中断初始化函数

  1. typedef enum
  2. {
  3. EXTI_DEBOUNCE_DISABLE = 0x0,
  4. EXTI_DEBOUNCE_ENABLE = 0x1
  5. } EXTIDeb_TypeDef;
  6. typedef enum
  7. {
  8. EXTI_LOW_LEVEL = 0x0,
  9. EXTI_HIGH_LEVEL = 0x1,
  10. EXTI_NEGATIVE_EDGE = 0x2,
  11. EXTI_POSITIVE_EDGE = 0x3,
  12. EXTI_BOTH_EDGE = 0x4
  13. } EXTIInterrupt_TypeDef;
  14. typedef struct
  15. {
  16. u32 EXTI_Channel;
  17. EXTIDeb_TypeDef EXTI_Debounce;
  18. u16 EXTI_DebounceCnt;
  19. EXTIInterrupt_TypeDef EXTI_IntType;
  20. } EXTI_InitTypeDef;
  21. /*********************************************************************************************************//**
  22. * @brief Initialize the EXTI peripheral.
  23. * @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure.
  24. * @retval None
  25. ************************************************************************************************************/
  26. void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)

在此说明下EXTI_Debounce、EXTI_IntType都是配置EXTI 中断 配置寄存器 n – EXTICFGRn, n = 0 ~ 15。EXTI_Debounce决定是否是能去抖电路,EXTI_IntType选择触发类型。

(3)中断使能函数

  1. /*********************************************************************************************************//**
  2. * @brief Enable or Disable the specified EXTI channelx interrupts.
  3. * @param EXTI_Channel: specify the EXTI channel.
  4. * This parameter can be one of the following values:
  5. * @arg EXTI_CHANNEL_0
  6. * @arg EXTI_CHANNEL_1
  7. * @arg EXTI_CHANNEL_2
  8. * @arg EXTI_CHANNEL_3
  9. * @arg EXTI_CHANNEL_4
  10. * @arg EXTI_CHANNEL_5
  11. * @arg EXTI_CHANNEL_6
  12. * @arg EXTI_CHANNEL_7
  13. * @arg EXTI_CHANNEL_8
  14. * @arg EXTI_CHANNEL_9
  15. * @arg EXTI_CHANNEL_10
  16. * @arg EXTI_CHANNEL_11
  17. * @arg EXTI_CHANNEL_12
  18. * @arg EXTI_CHANNEL_13
  19. * @arg EXTI_CHANNEL_14
  20. * @arg EXTI_CHANNEL_15
  21. * @param NewState: This parameter can be ENABLE or DISABLE.
  22. * @retval None
  23. ************************************************************************************************************/
  24. void EXTI_IntConfig(u32 EXTI_Channel, ControlStatus NewState)

(4)检测中断源状态函数

  1. /*********************************************************************************************************//**
  2. * @brief Enable or Disable the specified EXTI channelx interrupts.
  3. * @param EXTI_Channel: specify the EXTI channel.
  4. * This parameter can be one of the following values:
  5. * @arg EXTI_CHANNEL_0
  6. * @arg EXTI_CHANNEL_1
  7. * @arg EXTI_CHANNEL_2
  8. * @arg EXTI_CHANNEL_3
  9. * @arg EXTI_CHANNEL_4
  10. * @arg EXTI_CHANNEL_5
  11. * @arg EXTI_CHANNEL_6
  12. * @arg EXTI_CHANNEL_7
  13. * @arg EXTI_CHANNEL_8
  14. * @arg EXTI_CHANNEL_9
  15. * @arg EXTI_CHANNEL_10
  16. * @arg EXTI_CHANNEL_11
  17. * @arg EXTI_CHANNEL_12
  18. * @arg EXTI_CHANNEL_13
  19. * @arg EXTI_CHANNEL_14
  20. * @arg EXTI_CHANNEL_15
  21. * @param NewState: This parameter can be ENABLE or DISABLE.
  22. * @retval None
  23. ************************************************************************************************************/
  24. void EXTI_IntConfig(u32 EXTI_Channel, ControlStatus NewState)

(5)中断标志位清除函数

  1. /*********************************************************************************************************//**
  2. * @brief Clear the specified EXTI channelx edge flag.
  3. * @param EXTI_Channel: specify the EXTI channel.
  4. * This parameter can be one of the following values:
  5. * @arg EXTI_CHANNEL_0
  6. * @arg EXTI_CHANNEL_1
  7. * @arg EXTI_CHANNEL_2
  8. * @arg EXTI_CHANNEL_3
  9. * @arg EXTI_CHANNEL_4
  10. * @arg EXTI_CHANNEL_5
  11. * @arg EXTI_CHANNEL_6
  12. * @arg EXTI_CHANNEL_7
  13. * @arg EXTI_CHANNEL_8
  14. * @arg EXTI_CHANNEL_9
  15. * @arg EXTI_CHANNEL_10
  16. * @arg EXTI_CHANNEL_11
  17. * @arg EXTI_CHANNEL_12
  18. * @arg EXTI_CHANNEL_13
  19. * @arg EXTI_CHANNEL_14
  20. * @arg EXTI_CHANNEL_15
  21. * @retval None
  22. ************************************************************************************************************/
  23. void EXTI_ClearEdgeFlag(u32 EXTI_Channel)

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/565937
推荐阅读
相关标签
  

闽ICP备14008679号