2020-08-15 10:30 PM
here is settings i did..but little bit confused about prescalar value
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 0xFFFFFFFF;//0;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
2020-08-16 01:00 AM
If you want TIM_CNT to increment at 1ms rate, set the prescaler to timer input clock frequency / 1000 -1. So, for example, if the system frequency is 48MHz and APB divider is 1, set the prescaler to 47999.
JW
2020-08-16 02:26 AM
Actually i trying to do this but not got the proper result...
I want to achieve is, my timer 2 should count from 0 to max level i.e 0xFFFFFFFF.
and using this timer count(TIM->CNT) i want to handle led on/off operations for 70 leds...
whenever led ON command is received i am saving timer current count i.e TIM->CNT in uint32 variable.
and after every 20ms it should on & off...below is the code...
To start the timer i am using this API:
HAL_TIM_Base_Start(&htim2);
using structure to save counts:
struct
{
uint32_t timerValue;
uint8_t ledAction;
}L1,L2,L3,L4,L5,L6,L7,L8,L9,L10;
while(1)
{
L1.timerValue = __HAL_TIM_GetCounter(&htim2);
if(nled21val == 0)
{
//turn on led
HAL_GPIO_WritePin(GPIOE, LR2_Pin, GPIO_PIN_SET);
nled21val = L21.timerValue + 20;
}
else if(L21.timerValue >= nled21val)
{
//turn off led
HAL_GPIO_WritePin(GPIOE, LR2_Pin, GPIO_PIN_RESET);
nled21val = 0;
}
}