参考之前自己写的STM32的程序中利用按键控制LED亮灭完成了HT32F52352版的按键控制,经过昨天写点亮LED的程序觉得顺手多了。这个程序是在之前的LED(https://blog.csdn.net/Unlimited_Bit/article/details/86651974)的基础上加入了按键控制,在初始化过程中与STM32不同的是STM32只需选择浮空输入模式即可,而HT32F52352选择输入模式后还需要输入使能这一步,详细见代码。
主要的代码;
(1)按键的初始化
static void KEY_CCKU_Config()
{
CKCU_PeripClockConfig_TypeDef CCLOCK;
CCLOCK.Bit.PA = 1;
CCLOCK.Bit.AFIO = 1;
CKCU_PeripClockConfig(CCLOCK, ENABLE);
}
static void KEY_GPIO_Config()
{
HT_GPIO_TypeDef* GPIOx;
GPIOx = HT_GPIOA;
AFIO_GPxConfig(KEY1_GPIO_ID, KEY1_GPIO_PIN, KEY1_AFIO_MODE);
/* 输入使能函数 */
GPIO_InputConfig(GPIOx, KEY1_GPIO_PIN, ENABLE);
/* Configure the GPIO pin */
// GPIO_PullResistorConfig(GPIOx, KEY1_GPIO_PIN, GPIO_PR_DISABLE);
// GPIO_DriveConfig(GPIOx, KEY1_GPIO_PIN, GPIO_DV_8MA);
GPIO_DirectionConfig(GPIOx, KEY1_GPIO_PIN, GPIO_DIR_IN);
}
void KEY_Init()
{
KEY_CCKU_Config();
KEY_GPIO_Config();
}
(2)按键检测函数
u8 KEY_Scan(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_n)
{
if( GPIO_ReadInBit(HT_GPIOx, GPIO_PIN_n) == RESET )
{
while( GPIO_ReadInBit(HT_GPIOx, GPIO_PIN_n) == RESET );
return ON;
}
else
return OFF;
}
(3)LED初始化
#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(LED1_GPIO_ID, LED1_GPIO_PIN, LED1_AFIO_MODE);
/* Configure the GPIO pin */
// GPIO_PullResistorConfig(GPIOx, LED1_GPIO_PIN, GPIO_PR_DISABLE);
// GPIO_DriveConfig(GPIOx, LED1_GPIO_PIN, GPIO_DV_8MA);
GPIO_DirectionConfig(GPIOx, LED1_GPIO_PIN, GPIO_DIR_OUT);
}
void LED_Init()
{
LED_CKCU_Config();
LED_GPIO_Config();
}
(4)main函数
#include "ht32.h"
#include "ht32_board.h"
#include "led.h"
#include "key.h"
int main()
{
LED_Init();
KEY_Init();
while(1)
{
if( KEY_Scan(HT_GPIOA, KEY1_GPIO_PIN) == ON )
{
LEDToggle(HT_GPIOC, GPIO_PIN_14);
}
}
}
对硬件方面(如引脚、模式选择等的封装)
#ifndef _KEY_H
#define _KEY_H
#include "ht32f5xxxx_01.h"
#define KEY1_GPIO_ID (GPIO_PA)
#define KEY1_GPIO_PIN (GPIO_PIN_4)
#define KEY1_AFIO_MODE (AFIO_FUN_GPIO)
#define KEY2_GPIO_ID (GPIO_PA)
#define KEY2_GPIO_PIN (GPIO_PIN_1)
#define KEY2_AFIO_MODE (AFIO_FUN_GPIO)
#define KEY3_GPIO_ID (GPIO_PA)
#define KEY3_GPIO_PIN (GPIO_PIN_2)
#define KEY3_AFIO_MODE (AFIO_FUN_GPIO)
#define ON (1)
#define OFF (0)
#define LEDToggle(p,i) ({p->DOUTR ^=i;})
void KEY_Init(void);
u8 KEY_Scan(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_n);
#endif
#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)
#define LEDToggle(p,i) {p->DOUTR ^=i;} //输出反转状态,p:HT_GPIOA~D
void LED_Init(void);
#endif
对用到的库函数的摘录
(1)输入使能函数
/*********************************************************************************************************//**
* @brief Enable or Disable the input control 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 Cmd: This parameter can be ENABLE or DISABLE.
* @retval None
************************************************************************************************************/
void GPIO_InputConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, ControlStatus Cmd)
(2)获取指定引脚数据函数
/*********************************************************************************************************//**
* @brief Get the input data of specified port pin.
* @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
* @param GPIO_PIN_n: This parameter can be GPIO_PIN_x.
* @retval SET or RESET
************************************************************************************************************/
FlagStatus GPIO_ReadInBit(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_n)
特别说明下:关于设置引脚驱动电流及电阻的库函数也可以不调用(所以我就把它们注释了),这时相关的数值都为默认值。配置电流由PxDRVR寄存器实现。但是引脚复用初始化函数一定要调用。
配置电阻由PxPUR、PxPDR寄存器实现。