2018-12-31 08:14 AM
Hi,
I dont know whats wrong with my code, I just want to test the timer's working .
the purpose of the program is to turn off the led after a number of cycle . but this have never been happen, the led still on.
PS im working wit HSI_VALUE ((uint32_t)16000000) as the system clock. And here is the code
TIM_HandleTypeDef s_TimerInstance;
void InitializeTimer()
{ TIM_HandleTypeDef s_TimerInstance;
s_TimerInstance.Instance = TIM2;
__TIM2_CLK_ENABLE();
s_TimerInstance.Init.Prescaler = 16000;
s_TimerInstance.Init.CounterMode = TIM_COUNTERMODE_UP;
s_TimerInstance.Init.Period = 500;
s_TimerInstance.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
s_TimerInstance.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&s_TimerInstance);
HAL_TIM_Base_Start(&s_TimerInstance);
}
void InitializeLED()
{
__GPIOD_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_PIN_12;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
/* USER CODE BEGIN 2 */
HAL_Init();
InitializeLED();
InitializeTimer();
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
int timerValue = __HAL_TIM_GET_COUNTER(&s_TimerInstance);
if (timerValue == 10)
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Please help.
Thanks in advance
2018-12-31 08:18 AM
You'd need to iteratively test the counter, you can't test it once and expect to be at 10 immediately after starting it.
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
while(1)
{
int timerValue = __HAL_TIM_GET_COUNTER(&s_TimerInstance);
if (timerValue == 10)
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
}
2018-12-31 04:03 PM
HI clive,
Thanks for your reply,
But i test your proposition without success: led still blinks
Any suggestions please to make proram working as expected.
Thanks in advance,
2018-12-31 04:12 PM
>>But i test your proposition without success: led still blinks
Well you said it stayed on earlier, so progress I guess.
>>Any suggestions please to make program working as expected.
What do you expect? The program conveys an idea, Turns on the LED, and some time later turns it off, when the count is exactly 10. Likely to be fairly brief in human terms.
Perhaps timeValue >= 10 would cover the cases where you missed the situation where it counts past you.
Does the timer count? Should return a value in range 0-500 on a repetitive basis.
2019-01-01 02:09 AM
If you just want to know if the timer is working, better start from:
Using timer with 1second tick with 500 seconds period seems to say that there is no oscilloscope, otherwise, you could use PWM mode to implement LED Dimming.
I've got a simple driver for LEDs control where there is dimming and pulsing(s) option.
Here is an extract of the code for reference with dimmed LED
const IO_Pin_t LED3_AsPWM = { // LED3 = PE5 Digital WPM T3.3 AF2
GPIOE, { GPIO_PIN_5, GPIO_MODE_AF_PP, GPIO_NOPULL, GPIO_SPEED_FREQ_LOW, GPIO_AF2_TIM3, },
};
...
{ (IO_Pin_t*)&LED3_AsPWM, TIM3, TIM_CHANNEL_3, 0 },
...
/* Timer handler declaration */
TIM_HandleTypeDef LED_TimHandle;
/* Timer Output Compare Configuration Structure declaration */
TIM_OC_InitTypeDef sConfigLED;
void LEDsConfigureTimers(TIM_TypeDef* Timer) {
LED_TimHandle.Instance = Timer;
LED_TimHandle.Init.Prescaler = BASE_TIMER_CLOCK_MHZ-1; // SYSCLK = 48 MHz, timer will run at 1MHz
LED_TimHandle.Init.Period = 999; // counter period is 1000 steps
LED_TimHandle.Init.ClockDivision = 0;
LED_TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
LED_TimHandle.Init.RepetitionCounter = 0;
if (HAL_TIM_PWM_Init(&LED_TimHandle) != HAL_OK)
{ /* Initialization Error */
TrapError();
}
}
void LEDsConfigureChannel(TIM_TypeDef* Timer, uint32_t TIM_CHANNEL) {
LED_TimHandle.Instance = Timer;
sConfigLED.OCMode = TIM_OCMODE_PWM1;
sConfigLED.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigLED.OCFastMode = TIM_OCFAST_DISABLE;
sConfigLED.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigLED.OCNIdleState = TIM_OCNIDLESTATE_RESET;
sConfigLED.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigLED.Pulse = 1;
if (HAL_TIM_PWM_ConfigChannel(&LED_TimHandle, &sConfigLED, TIM_CHANNEL ) != HAL_OK)
{
TrapError();
}
if (HAL_TIM_PWM_Start(&LED_TimHandle, TIM_CHANNEL) != HAL_OK)
{ /* PWM Generation Error */
TrapError();
}
}
void LEDsSetDutyCycle(LED_t* pLED, uint32_t duty) {
uint32_t TIM_CHANNEL;
if(pLED->TIM==0) TrapError();
LED_TimHandle.Instance = pLED->TIM;
TIM_CHANNEL = pLED->TIM_CH;
sConfigLED.Pulse = duty;
if (HAL_TIM_PWM_ConfigChannel(&LED_TimHandle, &sConfigLED, TIM_CHANNEL) != HAL_OK)
{
TrapError();
}
if (HAL_TIM_PWM_Start(&LED_TimHandle, TIM_CHANNEL) != HAL_OK)
{ /* PWM Generation Error */
TrapError();
}
}
Consider this as pseudo code. Here the timer is used with its output pin as PWM to perform dimming. For on-time and pulses, this is done using 10msec tick interrupt and SW counters are it's not time consuming.
2019-01-01 04:57 AM
What do you expect? The program conveys an idea, Turns on the LED, and some time later turns it off, when the count is exactly 10. Likely to be fairly brief in human terms.
===> yes thats the idea: at the begining of thre program, the led could be turned on and after, a number of timer count, i want to turn it off. But, it seems i have problem with timer's configuration . See the attached picture, it seems i have problem with the timers count using this funtion: __HAL_TIM_GET_COUNTER
2019-01-01 04:58 AM
2019-01-01 09:30 AM
Perhaps don't fold the lines over each other. And use a "static int" so the variable is held in memory, not a register, and then break-point on the line of the comparison.
Use ">= 90" so if you miss the exact tick you still catch it passing.
2019-01-01 10:24 AM
Hi clive,
Is that your proposition:
while (1)
{ static int timerValue ;
timerValue=__HAL_TIM_GET_COUNTER(&s_TimerInstance);
if ( timerValue >= 90)
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
}
But it fails again, the led didnt turn off!
2019-01-01 10:43 AM
Try looking a the TIM2 registers via the Peripheral View, drill down via the "Peripherals" menu in the debugger.
Is this an STM32F4-DISCO board?
Perhaps try toggling the GPIO in a loop and single-stepping that?