本篇介绍合泰HT32F54253开发环境搭建及LED点灯测试 搭建前准备:HT32F54253相关资料下载地址:https://www.holtek.com.cn/esk32-30519 扩展板资料地址:https://www.holtek.com.cn/ESK32-21001 环境搭建,打开KEIL-MDK,点击Pack Installer 点击File,选择Import...,选择Holtek.HT32_DFP.latest.pack,(Holtek.HT32_DFP.latest.pack存放路径需是英文路径) 安装完成后,显示
新建工程 新建工程文件夹HT32F54253_LEDtest,工程文件夹中新建各子文件夹 点击Project--->New uVision Project… 选择进入HT32F54253_LEDtest中Mdk文件夹,输入project工程名,点击保存
选择芯片型号HT32F54253,点击OK
点击OK
点击File Extensions, Books and Environment…
在Manage Project Items中,新建Groups,并在其中添加相应的.c文件,点击OK
Project一栏中显示
点击Options for Target…
Output一栏勾选Create HEX File
C/C++--->IncludePaths中添加工程.h文件路径,Preprocessor Symbols添加USE_HT32_DRIVER, USE_HT32F54253_SK, USE_HT32F54243_53, USE_MEM_HT32F54253
Debug一栏选择仿真器仿真,仿真器选用CMSIS-DAP
硬件
PD1引脚连接LED1,PC14引脚连接LED2,PC14的复用功能为MT_CH3,马达控制定时器MCTM作PWM输出时,PC14可作为CH3输出
软件 PWM初始化,预分频HTCFG_MCTM_PRE设置为60,周期值HTCFG_MCTM_RELOAD 设置为400us void hal_pwmInit(void)
{
TM_TimeBaseInitTypeDef MCTM_TimeBaseInitStructure;
TM_OutputInitTypeDef MCTM_OutputInitStructure;
// MCTM_CHBRKCTRInitTypeDef MCTM_CHBRKCTRInitStructure;
CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.PC = 1;
CKCUClock.Bit.PD = 1;
CKCUClock.Bit.MCTM0 = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
/* Configure MCTM Channel 0 output pin */
// AFIO_GPxConfig(HTCFG_MCTM_CH0_GPIO_ID, HTCFG_MCTM_CH0_AFIO_PIN, AFIO_FUN_MCTM_GPTM);
// AFIO_GPxConfig(HTCFG_MCTM_CH0N_GPIO_ID, HTCFG_MCTM_CH0N_AFIO_PIN, AFIO_FUN_MCTM_GPTM);
/* Configure MCTM Channel 1 output pin */
// AFIO_GPxConfig(HTCFG_MCTM_CH1_GPIO_ID, HTCFG_MCTM_CH1_AFIO_PIN, AFIO_FUN_MCTM_GPTM);
// AFIO_GPxConfig(HTCFG_MCTM_CH1N_GPIO_ID, HTCFG_MCTM_CH1N_AFIO_PIN, AFIO_FUN_MCTM_GPTM);
/* Configure MCTM Channel 2 output pin */
// AFIO_GPxConfig(HTCFG_MCTM_CH2_GPIO_ID, HTCFG_MCTM_CH2_AFIO_PIN, AFIO_FUN_MCTM_GPTM);
// AFIO_GPxConfig(HTCFG_MCTM_CH2N_GPIO_ID, HTCFG_MCTM_CH2N_AFIO_PIN, AFIO_FUN_MCTM_GPTM);
/* Configure MCTM Channel 3 output pin */
AFIO_GPxConfig(HTCFG_MCTM_CH3_GPIO_ID, HTCFG_MCTM_CH3_AFIO_PIN, AFIO_FUN_MCTM_GPTM);
// GPIO_PullResistorConfig(LED2_PORT, LED2_PIN, GPIO_PR_DOWN);
// GPIO_DriveConfig(LED2_PORT, LED2_PIN, GPIO_DV_8MA);
// GPIO_WriteOutBits(LED2_PORT, LED2_PIN, RESET);
// GPIO_DirectionConfig(LED2_PORT, LED2_PIN, GPIO_DIR_OUT);
// GPIO_SetOutBits(LED2_PORT, LED2_PIN);
/* Configure MCTM Break pin */
// AFIO_GPxConfig(HTCFG_MCTM_BREAK_GPIO_ID, HTCFG_MCTM_BREAK_AFIO_PIN, AFIO_FUN_MCTM_GPTM);
/* MCTM Time Base configuration */
MCTM_TimeBaseInitStructure.CounterReload = HTCFG_MCTM_RELOAD - 1;
MCTM_TimeBaseInitStructure.Prescaler = HTCFG_MCTM_PRE - 1;
MCTM_TimeBaseInitStructure.RepetitionCounter = 0;
MCTM_TimeBaseInitStructure.CounterMode = TM_CNT_MODE_UP;
MCTM_TimeBaseInitStructure.PSCReloadTime = TM_PSC_RLD_IMMEDIATE;
TM_TimeBaseInit(HT_MCTM0, &MCTM_TimeBaseInitStructure);
/* MCTM Channel 2 output configuration */
MCTM_OutputInitStructure.Channel = TM_CH_3;
MCTM_OutputInitStructure.OutputMode = TM_OM_PWM1;
MCTM_OutputInitStructure.Control = TM_CHCTL_ENABLE;
// MCTM_OutputInitStructure.ControlN = TM_CHCTL_DISABLE;
MCTM_OutputInitStructure.Polarity = TM_CHP_NONINVERTED;
// MCTM_OutputInitStructure.PolarityN = TM_CHP_NONINVERTED;
MCTM_OutputInitStructure.IdleState = MCTM_OIS_LOW;
// MCTM_OutputInitStructure.IdleStateN = MCTM_OIS_HIGH;
MCTM_OutputInitStructure.Compare = 0;
MCTM_OutputInitStructure.AsymmetricCompare = 0;
TM_OutputInit(HT_MCTM0, &MCTM_OutputInitStructure);
// MCTM_OutputInitStructure.Channel = TM_CH_1;
// MCTM_OutputInitStructure.Compare = HTCFG_MCTM_RELOAD * 1 / 2;
// TM_OutputInit(HT_MCTM0, &MCTM_OutputInitStructure);
// MCTM_OutputInitStructure.Channel = TM_CH_2;
// MCTM_OutputInitStructure.Compare = HTCFG_MCTM_RELOAD * 1 / 4;
// TM_OutputInit(HT_MCTM0, &MCTM_OutputInitStructure);
// /* MCTM Off State, lock, Break, Automatic Output enable, dead time configuration */
// MCTM_CHBRKCTRInitStructure.OSSRState = MCTM_OSSR_STATE_ENABLE;
// MCTM_CHBRKCTRInitStructure.OSSIState = MCTM_OSSI_STATE_ENABLE;
// MCTM_CHBRKCTRInitStructure.LockLevel = MCTM_LOCK_LEVEL_2;
// MCTM_CHBRKCTRInitStructure.Break0 = MCTM_BREAK_ENABLE;
// MCTM_CHBRKCTRInitStructure.Break0Polarity = MCTM_BREAK_POLARITY_LOW;
// MCTM_CHBRKCTRInitStructure.AutomaticOutput = MCTM_CHAOE_ENABLE;
// MCTM_CHBRKCTRInitStructure.DeadTime = HTCFG_MCTM_DEAD_TIME;
// MCTM_CHBRKCTRInitStructure.BreakFilter = 0;
// MCTM_CHBRKCTRConfig(HT_MCTM0, &MCTM_CHBRKCTRInitStructure);
/* MCTM counter enable */
TM_Cmd(HT_MCTM0, ENABLE);
/* MCTM Channel Main Output enable */
MCTM_CHMOECmd(HT_MCTM0, ENABLE);
}
呼吸灯效果 void BreathingProc(void)
{
static unsigned short dir, LEDPwmVal;
if(dir == 0) //由暗到亮
LEDPwmVal++;
else //由亮到暗
LEDPwmVal--;
if(LEDPwmVal > 399)
dir = 1;
if(LEDPwmVal == 0)
dir = 0;
TM_SetCaptureCompare(HT_MCTM0, TM_CH_3, LEDPwmVal);
}
主函数 int main(void)
{
hal_CPUInit();
hal_ledInit();
hal_pwmInit();
OS_TaskInit();
OS_TaskCreat(OS_TASK1, LedProc, 1, OS_SLEEP); //10ms调用一次
OS_TaskCreat(OS_TASK2, BreathingProc, 1, OS_SLEEP); //10ms调用一次
OS_Start();
}
调试验证 LED1实现1秒亮灭,LED2实现呼吸灯 测试代码
|