/**
******************************************************************************
* 文件名程: bsp_EmulatorTIM.c
* 作 者: 硬石嵌入式开发团队
* 版 本: V1.0
* 编写日期: 2015-10-04
* 功 能: 模拟输出编码器信号
******************************************************************************
* 说明:
* 本例程配套硬石stm32开发板YS-F1Pro使用。
*
* 淘宝:
* 论坛:http://www.ing10bbs.com
* 版权归硬石嵌入式开发团队所有,请勿商用。
******************************************************************************
*/
/* 包含头文件 ----------------------------------------------------------------*/
#include "EmulatorTIM/bsp_EmulatorTIM.h"
/* 私有类型定义 --------------------------------------------------------------*/
/* 私有宏定义 ----------------------------------------------------------------*/
/* 私有变量 ------------------------------------------------------------------*/
TIM_HandleTypeDef htimx_emulator;
TIM_OC_InitTypeDef sConfigOC;
/* 扩展变量 ------------------------------------------------------------------*/
/* 私有函数原形 --------------------------------------------------------------*/
/* 函数体 --------------------------------------------------------------------*/
/**
* 函数功能: 定时器硬件初始化配置
* 输入参数: htim:定时器句柄类型指针
* 返 回 值: 无
* 说 明: 该函数被EMULATOR_TIMx_Init函数调用
*/
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(htim->Instance==EMULATOR_TIMx)
{
/* 定时器通道功能引脚端口时钟使能 */
EMULATOR_TIM_GPIO_RCC_CLK_ENABLE();
/* 定时器通道1功能引脚IO初始化 */
GPIO_InitStruct.Pin = EMULATOR_TIM_CH2_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(EMULATOR_TIM_CH2_PORT, &GPIO_InitStruct);
/* 定时器通道2功能引脚IO初始化 */
GPIO_InitStruct.Pin = EMULATOR_TIM_CH3_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(EMULATOR_TIM_CH3_PORT, &GPIO_InitStruct);
/* 定时器通道2功能引脚IO初始化 */
GPIO_InitStruct.Pin = EMULATOR_TIM_CH4_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(EMULATOR_TIM_CH4_PORT, &GPIO_InitStruct);
}
}
/**
* 函数功能: 基本定时器初始化
* 输入参数: 无
* 返 回 值: 无
* 说 明: 无
*/
void EMULATOR_TIMx_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
/* 定时器外设时钟使能 */
EMULATOR_TIM_RCC_CLK_ENABLE();
htimx_emulator.Instance = EMULATOR_TIMx;
htimx_emulator.Init.Prescaler = EMULATOR_TIM_PRESCALER;
htimx_emulator.Init.CounterMode = TIM_COUNTERMODE_UP;
htimx_emulator.Init.Period = EMULATOR_TIM_PERIOD;
htimx_emulator.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htimx_emulator);
HAL_TIM_OC_Init(&htimx_emulator);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htimx_emulator, &sClockSourceConfig);
sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.Pulse = EMULATOR_TIM_PERIOD*1/3;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
HAL_TIM_PWM_ConfigChannel(&htimx_emulator, &sConfigOC, TIM_CHANNEL_2);
sConfigOC.Pulse = EMULATOR_TIM_PERIOD*3/3;
HAL_TIM_PWM_ConfigChannel(&htimx_emulator, &sConfigOC, TIM_CHANNEL_3);
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.Pulse = EMULATOR_TIM_PERIOD*2/3;
HAL_TIM_PWM_ConfigChannel(&htimx_emulator, &sConfigOC, TIM_CHANNEL_4);
HAL_TIM_MspPostInit(&htimx_emulator);
}
/**
* @brief Configures a Timer to emulate an encoder sensor outputs in Forward
* direction
* @param htim : TIM handle
* @retval None
*/
void Emulate_Forward_Direction(TIM_HandleTypeDef* htim)
{
/*## -1- Re-Configure the Pulse ########################################## */
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.Pulse = (EMULATOR_TIM_PERIOD * 1 )/3;
HAL_TIM_OC_ConfigChannel(htim, &sConfigOC, TIM_CHANNEL_2);
sConfigOC.Pulse = (EMULATOR_TIM_PERIOD * 3 )/3;
HAL_TIM_OC_ConfigChannel(htim, &sConfigOC, TIM_CHANNEL_3);
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.Pulse = (EMULATOR_TIM_PERIOD * 2 )/3;
HAL_TIM_OC_ConfigChannel(htim, &sConfigOC, TIM_CHANNEL_4);
/*## -2- Start signals generation ######################################### */
HAL_TIM_OC_Start(htim, TIM_CHANNEL_2);
HAL_TIM_OC_Start(htim, TIM_CHANNEL_3);
HAL_TIM_OC_Start(htim, TIM_CHANNEL_4);
}
/**
* @brief Configures a Timer to emulate an encoder sensor outputs in Backward
* direction
* @param htim : TIM handle
* @retval None
*/
void Emulate_Backward_Direction(TIM_HandleTypeDef* htim)
{
/*## -1- Re-Configure the Pulse ########################################## */
sConfigOC.Pulse = (EMULATOR_TIM_PERIOD * 2 )/3;
HAL_TIM_OC_ConfigChannel(htim, &sConfigOC, TIM_CHANNEL_2);
sConfigOC.Pulse = (EMULATOR_TIM_PERIOD * 3 )/3;
HAL_TIM_OC_ConfigChannel(htim, &sConfigOC, TIM_CHANNEL_3);
sConfigOC.Pulse = (EMULATOR_TIM_PERIOD * 1 )/3;
HAL_TIM_OC_ConfigChannel(htim, &sConfigOC, TIM_CHANNEL_4);
/*## -2- Start signals generation ######################################### */
HAL_TIM_OC_Start(htim, TIM_CHANNEL_2);
HAL_TIM_OC_Start(htim, TIM_CHANNEL_3);
HAL_TIM_OC_Start(htim, TIM_CHANNEL_4);
}
/******************* END OF FILE****/ |