如何使用HT32F52352微控制器的GPIO模块来控制一个针脚的输入。通过这个例子,您将学习如何将HT32F52352与按钮进行连接,并从中读取输入信号!、
在程序中我们选择将PAO PA1设置为输入代码如下:
key.h
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
#define HT_KEY_GPIO_X (HT_GPIOA)
#define KEY_GPIO_GROUP (GPIO_PA)
#define KEY1_PIN (GPIO_PIN_0)
#define KEY2_PIN (GPIO_PIN_1)
#define KEY_AFIO_MODE (AFIO_MODE_DEFAULT) //ĬÈÏģʽ
void KEY_Init(void);//开关控制初始化
#endif
key.c
#include "key.h"
void KEY_Init(void){
HT_GPIO_TypeDef* GPIO_GROUP;
GPIO_GROUP = HT_KEY_GPIO_X ;
//选择A
AFIO_GPxConfig(KEY_GPIO_GROUP, KEY1_PIN|KEY2_PIN, KEY_AFIO_MODE); //模式
GPIO_InputConfig(GPIO_GROUP,KEY1_PIN|KEY2_PIN, ENABLE);
//输入使能函数, 上拉电阻默认电流 (此配置等价于下面三个)
// GPIO_DirectionConfig(GPIO_GROUP,KEY1_PIN|KEY2_PIN, GPIO_DIR_IN);
// 输入 GPIO_DIR_IN 输出 GPIO_DIR_OUT
// GPIO_PullResistorConfig(GPIO_GROUP,KEY1_PIN|KEY2_PIN, GPIO_PR_UP);
// 上拉,使IO口为高电平
// GPIO_DriveConfig(GPIO_GROUP,LED1_PIN|LED2_PIN, GPIO_DV_4MA);
// 设置GPIO的驱动电流 GPIO_DV_4/8/12/16MA
}
这段代码用来初始化按键,包括选择使用的GPIO端口、配置外设模式和使能输入功能。
详细注释如下:
- HT_GPIO_TypeDef是HT32F系列芯片GPIO控制器HT_GPIO结构体的别名,定义在"ht32fxxxx_gpio.h"文件中。
- GPIO_GROUP是一个HT_GPIO_TypeDef类型的指针变量,用来指向要使用的GPIO端口。
- HT_KEY_GPIO_X是一个宏定义,根据实际需要选择要使用的GPIO端口,例如#define HT_KEY_GPIO_X HT_GPIOA表示要使用PA端口。
- KEY_GPIO_GROUP是一个宏定义,根据实际需要选择要使用的GPIO端口,例如#define KEY_GPIO_GROUP HT_GPIOA表示要使用PA端口。
- KEY1_PIN和KEY2_PIN是宏定义,表示要配置的GPIO引脚,例如#define KEY1_PIN GPIO_PIN_0表示要配置PA0引脚。
- KEY_AFIO_MODE是宏定义,表示要配置的GPIO外设模式,例如#define KEY_AFIO_MODE AFIO_FUN_GPIO表示要将GPIO模式设置为复用模式。
- AFIO_GPxConfig()函数用来配置GPIO的复用模式,第一个参数为GPIO_GROUP所指的GPIO端口,第二个参数为要配置的GPIO引脚,第三个参数为要配置的GPIO外设模式。
- GPIO_InputConfig()函数用来使能GPIO端口的输入功能,第一个参数为GPIO_GROUP所指的GPIO端口,第二个参数为要使能的GPIO引脚,第三个参数为ENABLE或DISABLE,分别表示使能和禁用。
- 该函数将KEY1_PIN和KEY2_PIN所对应的GPIO端口配置为输入模式,并使能上拉电阻,用于检测按键是否按下(低电平)。
在这里需要对时钟进行初始化,添加上时钟配置
sys.h
#ifndef __SYS_H
#define __SYS_H
#include "ht32f5xxxx_01.h"
void NVIC_Configuration(void); //中断控制的设置
void RCC_Configuration(void); //RCC时钟配置
#endif
sys.c
#include "sys.h"
void NVIC_Configuration(void){ //ÖжϿØÖƵÄÉèÖÃ
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置为2
}
void RCC_Configuration(void){ //RCC时钟配置
CKCU_PeripClockConfig_TypeDef CCLOCK = {{0}};//不开启外设时钟相应功能无法使用
CCLOCK.Bit.PA = 1;//开启PA时钟
CCLOCK.Bit.PB = 1;//开启PB时钟
CCLOCK.Bit.PC = 1;//开启PC时钟
CCLOCK.Bit.PD = 1;//开启PD时钟
CCLOCK.Bit.AFIO = 1;//开启复用功能时钟
CKCU_PeripClockConfig(CCLOCK, ENABLE);//使能时钟
//这样开启了HT ABCD的时钟
}
这段代码用来配置HT32F52352的时钟,包括GPIO端口所连接的时钟。
详细注释如下:
- CKCU_PeripClockConfig_TypeDef是HT32F系列芯片时钟控制单元CKCU_PeripClockConfig结构体的别名,定义在"ht32fxxxx_ckcu.h"文件中。
- CCLOCK是一个CKCU_PeripClockConfig_TypeDef类型的结构体变量,用来配置要开启的外设时钟,其中PA、PB、PC、PD、AFIO五个GPIO端口所对应的时钟都被开启。
- CKCU_PeripClockConfig()函数用来开启或关闭外设的时钟,第一个参数为CKCU_PeripClockConfig_TypeDef类型的结构体变量,第二个参数为ENABLE或DISABLE,分别表示开启和关闭。
- 在这段代码中,开启了HT ABCD的时钟。
main.c
int main(void)
{
GPIO_Configuration();
RCC_Configuration(); //时钟初始化
LED_Init(); //LED初始化
KEY_Init(); //按键初始化¯
//SET 高电平 RESET 低电平
while (1)
{
if(GPIO_ReadInBit(HT_GPIOA,GPIO_PIN_0) == RESET)
{
int i;
for(i=0;i<=3;i++)
{
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_14,RESET);
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_15,SET);
delay_ms(500);
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_14,SET);
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_15,RESET);
delay_ms(500);
}
}
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_14 | GPIO_PIN_15,RESET);
delay_ms(500);
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_14 | GPIO_PIN_15,SET);
delay_ms(500);
}
}
这段代码是用来控制一个HT32F52352开发板上的LED灯和按键。当使PA0接地时,LED灯会闪烁,闪烁的次数为4次。当没有按下按键时,LED灯会交替闪烁。
解释:
- GPIO_Configuration()用来配置GPIO端口,包括GPIO的模式、速度、上下拉等参数。
- RCC_Configuration()用来配置时钟,因为HT32F52352需要提前配置好时钟才能正常工作。
- LED_Init()用来初始化LED灯,将其所连接的GPIO口设置为输出模式。
- KEY_Init()用来初始化按键,将其所连接的GPIO口设置为输入模式。
- GPIO_ReadInBit()用来读取GPIO口状态,返回值为SET或RESET。
- GPIO_WriteOutBits()用来设置GPIO口状态,第一个参数是GPIO端口,第二个参数是要设置的GPIO口,第三个参数为SET或RESET。
- delay_ms()用来延时,单位为毫秒。
- 这里SET为高电平 RESET为低电平
注意:这段代码中没有给出delay_ms()函数的实现,需要另外查找相关函数库。
因为在主函数中使用到了对LED的配置和延时函数参考上次内容
附上main.c(全)
/*********************************************************************************************************//**
* @file GPIO/Output/main.c
* @version $Rev:: 5805 $
* @date $Date:: 2022-04-12 #$
* @brief Main program.
*************************************************************************************************************
* @attention
*
* Firmware Disclaimer Information
*
* 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
* code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
* proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
* other intellectual property laws.
*
* 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
* code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
* other than HOLTEK and the customer.
*
* 3. The program technical documentation, including the code, is provided "as is" and for customer reference
* only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
* the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
* the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
*
* <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
************************************************************************************************************/
/* Includes ------------------------------------------------------------------------------------------------*/
#include "ht32.h"
#include "ht32_board.h"
#include "led.h"
#include "key.h"
#include "delay.h"
#include "sys.h"
/** @addtogroup HT32_Series_Peripheral_Examples HT32 Peripheral Examples
* @{
*/
/** @addtogroup GPIO_Examples GPIO
* @{
*/
/** @addtogroup Output
* @{
*/
/* Private function prototypes -----------------------------------------------------------------------------*/
void GPIO_Configuration(void);
void GPIO_OutputBit(void);
void GPIO_OutputData(void);
static void __Delay(u32 count);
/* Global functions ----------------------------------------------------------------------------------------*/
/*********************************************************************************************************//**
* @brief Main program.
* @retval None
***********************************************************************************************************/
int main(void)
{
GPIO_Configuration();
RCC_Configuration(); //初始化时钟
LED_Init(); //LED初始化
KEY_Init(); //按钮初始化
//SET 高电平 RESET 低电平
while (1)
{
if(GPIO_ReadInBit(HT_GPIOA,GPIO_PIN_0) == RESET)
{
int i;
for(i=0;i<=3;i++)
{
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_14,RESET);
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_15,SET);
delay_ms(500);
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_14,SET);
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_15,RESET);
delay_ms(500);
}
}
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_14 | GPIO_PIN_15,RESET);
delay_ms(500);
GPIO_WriteOutBits(HT_GPIOC, GPIO_PIN_14 | GPIO_PIN_15,SET);
delay_ms(500);
}
}
/*********************************************************************************************************//**
* @brief Configure the GPIO as output mode.
* @retval None
***********************************************************************************************************/
void GPIO_Configuration(void)
{
{ /* Enable peripheral clock */
CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.PB = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
}
{ /* Configure GPIO as output mode */
/* Configure AFIO mode as GPIO */
AFIO_GPxConfig(GPIO_PB, AFIO_PIN_1, AFIO_FUN_GPIO);
/* Configure GPIO pull resistor */
GPIO_PullResistorConfig(HT_GPIOB, GPIO_PIN_1, GPIO_PR_DOWN);
/* Default value RESET/SET */
GPIO_WriteOutBits(HT_GPIOB, GPIO_PIN_1, RESET);
/* Configure GPIO direction as output */
GPIO_DirectionConfig(HT_GPIOB, GPIO_PIN_1, GPIO_DIR_OUT);
}
}
/*********************************************************************************************************//**
* @brief GPIO Output bit test
* @retval None
***********************************************************************************************************/
void GPIO_OutputBit(void)
{
GPIO_SetOutBits(HT_GPIOB, GPIO_PIN_1); // GPIO = HIGH
__Delay(500000);
GPIO_ClearOutBits(HT_GPIOB, GPIO_PIN_1); // GPIO = LOW
__Delay(5000000);
}
/*********************************************************************************************************//**
* @brief GPIO Output data test
* @retval None
***********************************************************************************************************/
void GPIO_OutputData(void)
{
u16 uOutputData;
uOutputData = GPIO_ReadOutData(HT_GPIOB);
uOutputData |= GPIO_PIN_1; // GPIO = HIGH
GPIO_WriteOutData(HT_GPIOB, uOutputData);
__Delay(500000);
uOutputData = GPIO_ReadOutData(HT_GPIOB);
uOutputData &= ~(GPIO_PIN_1); // GPIO = LOW
GPIO_WriteOutData(HT_GPIOB, uOutputData);
__Delay(5000000);
}
#if (HT32_LIB_DEBUG == 1)
/*********************************************************************************************************//**
* @brief Report both the error name of the source file and the source line number.
* @param filename: pointer to the source file name.
* @param uline: error line source number.
* @retval None
***********************************************************************************************************/
void assert_error(u8* filename, u32 uline)
{
/*
This function is called by IP library that the invalid parameters has been passed to the library API.
Debug message can be added here.
Example: printf("Parameter Error: file %s on line %d\r\n", filename, uline);
*/
while (1)
{
}
}
#endif
/* Private functions ---------------------------------------------------------------------------------------*/
/*********************************************************************************************************//**
* @brief delay function
* @param count: delay count for loop
* @retval None
***********************************************************************************************************/
static void __Delay(u32 count)
{
while (count--)
{
__NOP(); // Prevent delay loop be optimized
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/