cancel
Showing results for 
Search instead for 
Did you mean: 

Timer(output compare mode) not working of stm32f411re

DSALI.1
Associate

I am using a stm32f411re nucleo board , i am trying to configure the Tim2_ch1 in output compare mode .

i have written the code by referring the reference manual

https://www.st.com/resource/en/reference_manual/dm00119316-stm32f411xc-e-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf

code:

//PA5:TIM2_CH1

// clock frequency:16Mhz

#include "stm32f4xx.h"         // Device header

// toggle led at 1hz using TIM2 output COMPARE MODE

int main(void)

{

 RCC->AHB1ENR |=1;

GPIOA->MODER |=0X800; //PA_5 is configured in alternate function mode

GPIOA->AFR[0] |=0X00100000; // Tim2_ch1 is assigned to PA_5

//TIM2 OUTPUT COMPARE CONFIG

RCC->APB1ENR |=1;

TIM2->PSC |=1600-1;   

TIM2->ARR |=10000-1;   //clock frequency is divided by prescalar and auto reload

TIM2->CCMR1 |=0X30;    

TIM2->CCR1 |=0;

TIM2->CCER |=1;

TIM2->CNT |=0;

TIM2->CR1 |=1;

while(1)

{}

}

This code is not working ,is there any issue with the code?

1 REPLY 1

> This code is not working ,

Never say this, it is not helpful at all.

Always answer the question: what would you expect it to do and how is the observated behaviour different from the expected one?

> TIM2->PSC |=1600-1;

Prescaler is unconditionally preloaded, you'll see this to get active only after Update, in your case after timer overflows.

> TIM2->ARR |=10000-1;   //clock frequency is divided by prescalar and auto reload

TIM2->ARR is after reset 0xFFFFFFFF, so this does not change it. Timer's period is probably much longer than you thought (calculate).

> TIM2->CCMR1 |=0X30;

Why?

> TIM2->CNT |=0;

What do you want to achieve with this?

Generally, use as little RMW (|=) as needed; use mostly direct =.

Also, read errata for the needed delay between clock setting in RCC and setting peripherals' registers. (If it's not in 'F411 errata, ST sometimes does not update errata timely, look into the 'F407 errata).

Anytime, if in doubt, read back the given peripheral's registers content and check.

JW