[其它] 合泰32笔记2-GPIO使用(2022/2/20)

[复制链接]
965|16
 楼主| wangtaohui 发表于 2023-2-25 12:18 | 显示全部楼层 |阅读模式
GPIO, IO, gp, pi
HT-GPIO使用
1.依赖库的位置


如上图:合泰的库文件的头文件在文件夹外部,且是5个上级目录的子文件内,

所以,移植时注意将合泰的源文件夹(以外设命名的文件夹)放在三级含有库的三级子文件夹内。

 楼主| wangtaohui 发表于 2023-2-25 12:20 | 显示全部楼层
下图为例:



否则则需 在项目文件夹复制入两个文件夹 library 和utilities, 并修改头文件路径 。
 楼主| wangtaohui 发表于 2023-2-25 12:32 | 显示全部楼层
自己写的外设.c.h文件需包含的基本头文件为如下三个:
  1. #include "ht32.h"
  2. #include "ht32_board.h"
  3. #include "ht32_board_config.h"
 楼主| wangtaohui 发表于 2023-2-25 12:36 | 显示全部楼层
GPIO使用
1.时钟配置:
合泰将所有板级的时钟配置寄存器(3个32位的寄存器定义为一个联合体union),且在联合体 CKCU_PeripClockConfig_TypeDef 内定义了一个32位的结构体,使我们可以在3个与时钟配置相关的32位寄存器中“可位寻址”。
 楼主| wangtaohui 发表于 2023-2-25 12:43 | 显示全部楼层
使用时,如下:
[code]void CKCU_Configuration(void)
{
  CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
//定义一个 *CKCU_PeripClockConfig_TypeDef* 此类型的联合体,建议内部的结构体全部赋值2为0(关闭所有时钟)
  HTCFG_OUTPUT_LED0_CLK(CKCUClock) = 1;
  HTCFG_OUTPUT_LED1_CLK(CKCUClock) = 1;
  HTCFG_OUTPUT_LED2_CLK(CKCUClock) = 1;
  HTCFG_INPUT_WAKE_CLK(CKCUClock)  = 1;
  HTCFG_INPUT_KEY1_CLK(CKCUClock)  = 1;
  HTCFG_INPUT_KEY2_CLK(CKCUClock)  = 1;
//
 楼主| wangtaohui 发表于 2023-2-25 12:46 | 显示全部楼层
GPIO输出/输入配置
几乎每一个IO都可以被复用为多达15种功能(GPIO也是功能之一),所以建议在使用前均使用复用功能配置函数配置好IO对应的内部功能。
 楼主| wangtaohui 发表于 2023-2-25 12:49 | 显示全部楼层
  1. /*********************************************************************************************************//**
  2. * [url=home.php?mod=space&uid=247401]@brief[/url] Configure alternated mode of GPIO with specified pins.
  3. * @param GPIO_Px: ⭐GPIO_PA ~ GPIO_PD.填写IO端口port,宏定义好的
  4. * @param AFIO_PIN_n: This parameter can be any combination of AFIO_PIN_x.⭐填写对应对应引脚,即我们要用的引脚
  5. * @param AFIO_MODE_n: This parameter can be one of the following values:对应的内部功能有如下15个选择,
  6. *        [url=home.php?mod=space&uid=2817080]@ARG[/url] AFIO_MODE_DEFAULT : The default I/O function
  7. *        @arg AFIO_MODE_1       : Alternated mode 1
  8. *        @arg AFIO_MODE_2       : Alternated mode 2
  9. *        @arg AFIO_MODE_3       : Alternated mode 3
  10. *        @arg AFIO_MODE_4       : Alternated mode 4
  11. *        @arg AFIO_MODE_5       : Alternated mode 5
  12. *        @arg AFIO_MODE_6       : Alternated mode 6
  13. *        @arg AFIO_MODE_7       : Alternated mode 7
  14. *        @arg AFIO_MODE_8       : Alternated mode 8
  15. *        @arg AFIO_MODE_9       : Alternated mode 9
  16. *        @arg AFIO_MODE_10      : Alternated mode 10
  17. *        @arg AFIO_MODE_11      : Alternated mode 11
  18. *        @arg AFIO_MODE_12      : Alternated mode 12
  19. *        @arg AFIO_MODE_13      : Alternated mode 13
  20. *        @arg AFIO_MODE_14      : Alternated mode 14
  21. *        @arg AFIO_MODE_15      : Alternated mode 15
  22. * @retval None

  23. ⭐但通常不会直接填写下面的模式几而是填写<ht32f5xxxx_gpio.h>头文件中的宏定义:如下:
  24. ************************************************************************************************************/
  25. void AFIO_GPxConfig(u32 GPIO_Px, u32 AFIO_PIN_n, AFIO_MODE_Enum AFIO_MODE_n)
 楼主| wangtaohui 发表于 2023-2-25 12:57 | 显示全部楼层
  1. /* Definitions of AFIO_FUN                                         常填写的是以下宏定义为函数功能,易读                                      */
  2. #define AFIO_FUN_DEFAULT    AFIO_MODE_DEFAULT  /*!< Default AFIO mode                                       */
  3. #define AFIO_FUN_GPIO       AFIO_MODE_1        /*!< AFIO mode GPIO                                          */
  4. #define AFIO_FUN_DAC        AFIO_MODE_2        /*!< AFIO mode DAC                                           */
  5. #define AFIO_FUN_ADC0       AFIO_MODE_2        /*!< AFIO mode ADC0                                          */
  6. #define AFIO_FUN_ADC1       AFIO_MODE_3        /*!< AFIO mode ADC1                                          */
  7. #define AFIO_FUN_CMP        AFIO_MODE_3        /*!< AFIO mode CMP                                           */
  8. #define AFIO_FUN_MCTM_GPTM  AFIO_MODE_4        /*!< AFIO mode MCTM/GPTM                                     */
  9. #if (LIBCFG_AFIO_PWM_MODE4)
  10. #define AFIO_FUN_PWM        AFIO_MODE_4        /*!< AFIO mode PWM                                           */
  11. #else
  12. #define AFIO_FUN_PWM        AFIO_MODE_13       /*!< AFIO mode PWM                                           */
  13. #endif
  14. #define AFIO_FUN_SPI        AFIO_MODE_5        /*!< AFIO mode SPI                                           */
  15. #define AFIO_FUN_USART_UART AFIO_MODE_6        /*!< AFIO mode USART/UART                                    */
  16. #define AFIO_FUN_I2C        AFIO_MODE_7        /*!< AFIO mode I2C                                           */
  17. #define AFIO_FUN_SCI        AFIO_MODE_8        /*!< AFIO mode SCI                                           */
  18. #define AFIO_FUN_CMP_OPA    AFIO_MODE_8        /*!< AFIO mode CMP/OPA                                       */
  19. #define AFIO_FUN_EBI        AFIO_MODE_9        /*!< AFIO mode EBI                                           */
  20. #define AFIO_FUN_I2S        AFIO_MODE_10       /*!< AFIO mode I2S                                           */
  21. #if (LIBCFG_AFIO_SCTM_MODE9)
  22. #define AFIO_FUN_SCTM       AFIO_MODE_9        /*!< AFIO mode SCTM                                          */
  23. #else
  24. #define AFIO_FUN_SCTM       AFIO_MODE_13       /*!< AFIO mode SCTM                                          */
  25. #endif
  26. #define AFIO_FUN_TEKY       AFIO_MODE_12       /*!< AFIO mode TKEY                                          */
  27. #define AFIO_FUN_LCD        AFIO_MODE_14       /*!< AFIO mode LCD                                           */
  28. #define AFIO_FUN_SLED       AFIO_MODE_14       /*!< AFIO mode SLED                                          */
  29. #define AFIO_FUN_LEDC       AFIO_MODE_14       /*!< AFIO mode LEDC                                          */
  30. #define AFIO_FUN_SYSTEM     AFIO_MODE_15       /*!< AFIO mode System                                        */

  31. /* Definitions of AFIO_FUN alias                                                                            */
  32. #define AFIO_FUN_MCTM0      AFIO_FUN_MCTM_GPTM

  33. #define AFIO_FUN_GPTM0      AFIO_FUN_MCTM_GPTM
  34. #define AFIO_FUN_GPTM1      AFIO_FUN_MCTM_GPTM
  35. #define AFIO_FUN_GPTM2      AFIO_FUN_MCTM_GPTM
  36. #define AFIO_FUN_GPTM3      AFIO_FUN_MCTM_GPTM

  37. #define AFIO_FUN_PWM0       AFIO_FUN_PWM
  38. #define AFIO_FUN_PWM1       AFIO_FUN_PWM
  39. #define AFIO_FUN_PWM2       AFIO_FUN_PWM
  40. #define AFIO_FUN_PWM3       AFIO_FUN_PWM

  41. #define AFIO_FUN_SCTM0      AFIO_FUN_SCTM
  42. #define AFIO_FUN_SCTM1      AFIO_FUN_SCTM
  43. #define AFIO_FUN_SCTM2      AFIO_FUN_SCTM
  44. #define AFIO_FUN_SCTM3      AFIO_FUN_SCTM

  45. #define AFIO_FUN_ADC        AFIO_FUN_ADC0
 楼主| wangtaohui 发表于 2023-2-25 13:02 | 显示全部楼层
输出/入选择配置:
  1. /*********************************************************************************************************//**
  2. * @brief Configure the direction of specified GPIO pins.
  3. * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.⭐使用的引脚的port
  4. * @param GPIO_PIN_nBITMAP: The port pins.⭐使用的引脚的pin
  5. *   This parameter can be any combination of GPIO_PIN_x.
  6. * @param GPIO_DIR_INorOUT:
  7. *   This parameter can be one of below:
  8. *     ⭐@arg GPIO_DIR_IN  : The pins are input mode
  9. *     ⭐@arg GPIO_DIR_OUT : The pins are output mode
  10. 两个模式的参数选择,对应宏就好
  11. * @retval None
  12. ************************************************************************************************************/
  13. void GPIO_DirectionConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, GPIO_DIR_Enum GPIO_DIR_INorOUT)
 楼主| wangtaohui 发表于 2023-2-25 13:02 | 显示全部楼层
内部的上下拉电阻配置:
  1. /*********************************************************************************************************//**
  2. * @brief Configure the pull resistor of specified GPIO pins.
  3. * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.端口
  4. * @param GPIO_PIN_nBITMAP: The port pins.引脚
  5. *   This parameter can be any combination of GPIO_PIN_x.
  6. * @param GPIO_PR_x: Selection of Pull resistor.
  7. *   This parameter can be one of below:
  8. *     @arg GPIO_PR_UP      : The pins with internal pull-up resistor⭐上拉电阻宏
  9. *     @arg GPIO_PR_DOWN    : The pins with internal pull-down resistor⭐下拉电阻宏
  10. *     @arg GPIO_PR_DISABLE : The pins without pull resistor
  11. * @retval None⭐开漏状态宏
  12. ************************************************************************************************************/
  13. void GPIO_PullResistorConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, GPIO_PR_Enum GPIO_PR_x)
 楼主| wangtaohui 发表于 2023-2-25 13:02 | 显示全部楼层
输入使能设置

设置为输入时,需要多加这一步使能
  1. /*********************************************************************************************************//**
  2. * @brief Enable or Disable the input control of specified GPIO pins.端口
  3. * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
  4. * @param GPIO_PIN_nBITMAP: The port pins.引脚
  5. *   This parameter can be any combination of GPIO_PIN_x.
  6. * @param Cmd: This parameter can be ENABLE or DISABLE.
  7. * @retval None使能与否
  8. ************************************************************************************************************/
  9. void GPIO_InputConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, ControlStatus Cmd)
 楼主| wangtaohui 发表于 2023-2-25 13:05 | 显示全部楼层
读取输入和设置输出电平
使用以下函数,参数都较为简单
  1. //读取函数:
  2. FlagStatus GPIO_ReadInBit(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_n);
  3. //读取设置为输入io的电平
  4. FlagStatus GPIO_ReadOutBit(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_n);
  5. //读取设置为输出io的电平
  6. u16 GPIO_ReadInData(HT_GPIO_TypeDef* HT_GPIOx);
  7. //读取设置为输入io的整个io组寄存器的电平
  8. u16 GPIO_ReadOutData(HT_GPIO_TypeDef* HT_GPIOx);
  9. //读取设置为输出io的整个组寄存器的电平
 楼主| wangtaohui 发表于 2023-2-25 13:05 | 显示全部楼层
  1. //设置函数:
  2. void GPIO_SetOutBits(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP);
  3. //置一函数
  4. void GPIO_ClearOutBits(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP);
  5. //清零函数
  6. void GPIO_WriteOutBits(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, FlagStatus Status);
  7. //设置某一位为0/1,可用定义的枚举类型代替RESET/SET
  8. /*
  9. 两个通用的枚举类型
  10. typedef enum {RESET = 0, SET = !RESET} FlagStatus;
  11. typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrStatus;
  12. */
  13. void GPIO_WriteOutData(HT_GPIO_TypeDef* HT_GPIOx, u16 Data);
  14. //设置整组IO的输出寄存器,填入数据为u16
 楼主| wangtaohui 发表于 2023-2-25 13:06 | 显示全部楼层
引脚复用功能的定义与查找
基本上合泰的引脚都建议在使用前明确复用为什么功能。避免与SW等的下载接口冲突,而造成IO没有用作想要的功能。

而合泰单片机引脚的复用功能表可以在数据手册(simHT32F52342_52_Datasheetv150.pdf)中查找,截图如下:
 楼主| wangtaohui 发表于 2023-2-25 13:06 | 显示全部楼层
 楼主| wangtaohui 发表于 2023-2-25 13:06 | 显示全部楼层
 楼主| wangtaohui 发表于 2023-2-25 13:06 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

43

主题

529

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部