首先知道在HT32F52352中有两个可用的LED灯
可以看出对应为PC14 PC15
当PC15 PC14 为低电平时LED亮
led.h
#ifndef _LED_H
#define _LED_H
#include "ht32f5xxxx_01.h"
void LED_init(void);
#endif
led.c
#include "led.h"
static void RCC_init() //时钟配置
{
CKCU_PeripClockConfig_TypeDef CCLOCK = {{0}}; //不开启外设时钟相应功能无法使用
CCLOCK.Bit.PC = 1; //开启PC时钟
CCLOCK.Bit.AFIO = 1; //开启复用功能时钟
CKCU_PeripClockConfig(CCLOCK, ENABLE); //使能时钟
}
static void LED_GPIO_init()
{
HT_GPIO_TypeDef* LED_gpio;
LED_gpio=HT_GPIOC;
AFIO_GPxConfig(GPIO_PC, GPIO_PIN_14 | GPIO_PIN_15, AFIO_FUN_GPIO); //配置GPIO模式
GPIO_DirectionConfig(LED_gpio, GPIO_PIN_14 | GPIO_PIN_15, GPIO_DIR_OUT);
//配置GPIO输出 输入GPIO_DIR_IN 输出GPIO_DIR_OUT
GPIO_PullResistorConfig(LED_gpio, GPIO_PIN_14 | GPIO_PIN_15, GPIO_PR_DISABLE);
//配置上下拉 上拉GPIO_PR_UP 下拉GPIO_PR_DOWN
GPIO_DriveConfig(LED_gpio, GPIO_PIN_14 | GPIO_PIN_15, GPIO_DV_8MA);
//选择GPIO引脚驱动电压 GPIO_DV_4/8/12/16MA
}
void LED_init()
{
RCC_init();
LED_GPIO_init();
}
main.c
int main(void)
{
GPIO_Configuration();
LED_init();
//0为低电平 1为高电平
//因为低电平LED亮 所以0 LED亮 1 LED灭
while (1)
{
GPIO_WriteOutBits(HT_GPIOC,GPIO_PIN_14 | GPIO_PIN_15,0);
delay_ms(500);
GPIO_WriteOutBits(HT_GPIOC,GPIO_PIN_14 | GPIO_PIN_15,1);
delay_ms(500);
}
}
因为上面会用到延时函数
所以:
delay.h
#ifndef __DELAY_H
#define __DELAY_H
#include "ht32_cm0plus_misc.h"
void delay_s(u16 s);
void delay_ms(u16 ms);
void delay_us(u32 us);
#endif
delay.c
#include "ht32_cm0plus_misc.h"
#include "delay.h"
//mS
void delay_us(u32 us)
{
u32 i;
SYSTICK_ClockSourceConfig(SYSTICK_SRC_STCLK); //
SYSTICK_SetReloadValue(SystemCoreClock / 8 / 1000000); //
SYSTICK_IntConfig(DISABLE); //
SYSTICK_CounterCmd(SYSTICK_COUNTER_CLEAR); //
SYSTICK_CounterCmd(SYSTICK_COUNTER_ENABLE); //
for( i = 0;i < us;i++ )
{
while( !( (SysTick->CTRL) & (1<<16) ) );
}
SYSTICK_CounterCmd(SYSTICK_COUNTER_DISABLE); //
SYSTICK_CounterCmd(SYSTICK_COUNTER_CLEAR); //
}
void delay_ms(u16 ms){ //mS
while( ms-- != 0){
delay_us(1000); //1000
}
}
void delay_s(u16 s){ //S
while( s-- != 0){
delay_ms(1000); //1000
}
}
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 "delay.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();
LED_init();
//
while (1)
{
GPIO_WriteOutBits(HT_GPIOC,GPIO_PIN_14 | GPIO_PIN_15,0);
delay_ms(500);
GPIO_WriteOutBits(HT_GPIOC,GPIO_PIN_14 | GPIO_PIN_15,1);
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
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
实现两个LED灯500ms一闪一灭
注意:烧录程序后,需给单片机断电后,再供电才能实现功能,或者点复位按钮