第一次尝试点亮一个LED,花了不少时间在找官网资料,终于算是完成了点亮LED这个大项目了。
用的是最小系统板上附带的LED,最小系统板型号是ESK32-30501v2.0。
主要的代码:
#include "led.h"
static void LED_CKCU_Config()
{
CKCU_PeripClockConfig_TypeDef CCLOCK;
CCLOCK.Bit.PC = 1;
CCLOCK.Bit.AFIO = 1;
CKCU_PeripClockConfig(CCLOCK, ENABLE);
}
static void LED_GPIO_Config()
{
HT_GPIO_TypeDef* GPIOx;
GPIOx = HT_GPIOC;
AFIO_GPxConfig(LED2_GPIO_ID, LED2_GPIO_PIN, LED2_AFIO_MODE);
/* Configure the GPIO pin */
GPIO_PullResistorConfig(GPIOx, LED2_GPIO_PIN, GPIO_PR_DISABLE);
GPIO_DriveConfig(GPIOx, LED2_GPIO_PIN, GPIO_DV_8MA);
GPIO_DirectionConfig(GPIOx, LED2_GPIO_PIN, GPIO_DIR_OUT);
}
void LED_Init()
{
LED_CKCU_Config();
LED_GPIO_Config();
}
对硬件方面(如引脚、模式选择等的封装)
#ifndef _LED_H
#define _LED_H
#include "ht32f5xxxx_01.h"
#define LED1_GPIO_ID (GPIO_PC)
#define LED1_GPIO_PIN (GPIO_PIN_14)
#define LED1_AFIO_MODE (AFIO_FUN_GPIO)
#define LED2_GPIO_ID (GPIO_PC)
#define LED2_GPIO_PIN (GPIO_PIN_15)
#define LED2_AFIO_MODE (AFIO_FUN_GPIO)
#define LED3_GPIO_ID (GPIO_PC)
#define LED3_GPIO_PIN (GPIO_PIN_1)
#define LED3_AFIO_MODE (AFIO_FUN_GPIO)
void LED_Init(void);
#endif
对用到的库函数的摘录
1、时钟使能函数
/*********************************************************************************************************//**
* @brief Enable or Disable the peripheral clock.
* @param Clock: specify the peripheral clock enable bits.
* @param Cmd: This parameter can be ENABLE or DISABLE.
* @retval None
************************************************************************************************************/
void CKCU_PeripClockConfig(CKCU_PeripClockConfig_TypeDef Clock, ControlStatus Cmd)
2、复用功能使能函数
/*********************************************************************************************************//**
* @brief Configure alternated mode of GPIO with specified pins.
* @param GPIO_Px: GPIO_PA ~ GPIO_PD.
* @param AFIO_PIN_n: This parameter can be any combination of AFIO_PIN_x.
* @param AFIO_MODE_n: This parameter can be one of the following values:
* @arg AFIO_MODE_DEFAULT : The default I/O function
* @arg AFIO_MODE_1 : Alternated mode 1
* @arg AFIO_MODE_2 : Alternated mode 2
* @arg AFIO_MODE_3 : Alternated mode 3
* @arg AFIO_MODE_4 : Alternated mode 4
* @arg AFIO_MODE_5 : Alternated mode 5
* @arg AFIO_MODE_6 : Alternated mode 6
* @arg AFIO_MODE_7 : Alternated mode 7
* @arg AFIO_MODE_8 : Alternated mode 8
* @arg AFIO_MODE_9 : Alternated mode 9
* @arg AFIO_MODE_10 : Alternated mode 10
* @arg AFIO_MODE_11 : Alternated mode 11
* @arg AFIO_MODE_12 : Alternated mode 12
* @arg AFIO_MODE_13 : Alternated mode 13
* @arg AFIO_MODE_14 : Alternated mode 14
* @arg AFIO_MODE_15 : Alternated mode 15
* @retval None
************************************************************************************************************/
void AFIO_GPxConfig(u32 GPIO_Px, u32 AFIO_PIN_n, AFIO_MODE_Enum AFIO_MODE_n)
3、初始化GPIO引脚的函数
(1)设置指定引脚电阻函数
/*********************************************************************************************************//**
* @brief Configure the pull resistor of specified GPIO pins.
* @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
* @param GPIO_PIN_nBITMAP: The port pins.
* This parameter can be any combination of GPIO_PIN_x.
* @param GPIO_PR_x: Selection of Pull resistor.
* This parameter can be one of below:
* @arg GPIO_PR_UP : The pins with internal pull-up resistor
* @arg GPIO_PR_DOWN : The pins with internal pull-down resistor
* @arg GPIO_PR_DISABLE : The pins without pull resistor
* @retval None
************************************************************************************************************/
void GPIO_PullResistorConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, GPIO_PR_Enum GPIO_PR_x)
(2)设置驱动电流函数
/*********************************************************************************************************//**
* @brief Select the driving current of specified GPIO pins.
* @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
* @param GPIO_PIN_nBITMAP: The port pins.
* This parameter can be any combination of GPIO_PIN_x.
* @param GPIO_DV_nMA:
* This parameter can be one of below:
* @arg GPIO_DV_4MA : Select output driving current as 4 mA
* @arg GPIO_DV_8MA : Select output driving current as 8 mA
* @arg GPIO_DV_12MA : Select output driving current as 12 mA
* @arg GPIO_DV_16MA : Select output driving current as 16 mA
* @retval None
************************************************************************************************************/
void GPIO_DriveConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, GPIO_DV_Enum GPIO_DV_nMA)
(3)设置输入/输出函数
/*********************************************************************************************************//**
* @brief Configure the direction of specified GPIO pins.
* @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
* @param GPIO_PIN_nBITMAP: The port pins.
* This parameter can be any combination of GPIO_PIN_x.
* @param GPIO_DIR_INorOUT:
* This parameter can be one of below:
* @arg GPIO_DIR_IN : The pins are input mode
* @arg GPIO_DIR_OUT : The pins are output mode
* @retval None
************************************************************************************************************/
void GPIO_DirectionConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, GPIO_DIR_Enum GPIO_DIR_INorOUT)