cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F401RE Timer setup

stforum
Associate II
Posted on December 24, 2015 at 18:32

Hi,

I'm getting started with the 'F401 device, I'm trying to blink the LED on the Nucleo-F401RE board when Timer2 triggers. I know this is just register level stuff and I'm probably making things harder for myself doing this, but I'd like to get a feel for what's actually happening.

This is what I have so far - I know I'm missing something, but I can't see what. Any thoughts?

.ExternalClass1321C468BAEA455C841E37159325A9B4 p, .ExternalClass1321C468BAEA455C841E37159325A9B4 li {white-space:pre-wrap;}

//

The

LED

pin

shares

with

TIM2_CH1

/

TIM2_ETR

alternate

functions

AF01

according

to

the

data

sheet

&sharpinclude

''stm32f4xx.h''

int

main

(

void

)

{

RCC

->

AHB1RSTR

|=

RCC_AHB1RSTR_GPIOARST

;

//

Reset

GPIOA

RCC

->

AHB1RSTR

=

0

;

//

Exit

reset

state

RCC

->

AHB1ENR

|=

RCC_AHB1ENR_GPIOAEN

;

//

Enable

GPIOA

clock

RCC

->

APB1ENR

|=

RCC_APB1ENR_TIM2EN

;

//

Enable

Timer

Clock

GPIOA

->

MODER

|=

GPIO_MODER_MODER5_1

;

//

Set

GPIOA.5

to

Alternate

function

(Took

some

digging

to

find

this!)

GPIOA

->

AFR

[

0

]|=

0x100000

;

//

Set

Alternate

function

AF01

on

pin

5

(defines

seem

to

be

missing?)

TIM2

->

PSC

=

29999

;

//

I

think

the

counter

clock

is

84Mhz,

divide

it

down

a

bit.

Prescale

=

PSC

+1

TIM2

->

ARR

=

1

;

//

Auto

start

at

1

on

roll

over

TIM2

->

CCR1

=

500

;

//

Value

to

compare

with,

500

seems

a

nice

number...

TIM2

->

CCMR1

=

TIM_CCMR1_OC1M_0

|

TIM_CCMR1_OC1M_1

;

//

Output

changes

on

channel

1

compare

match.

Also

took

some

finding!

TIM2

->

CCER

=

TIM_CCER_CC1E

;

//

Enable

compare

for

(;;)

return

0

;

}

#nucleo #stm32f #timers
6 REPLIES 6
Posted on January 02, 2016 at 15:36

Basic blinkey (LED at GPIO output, loop delay) works?

TIM2

->

ARR

=

1

;

//

Auto

start

at

1

on

roll

over

TIM2

->

CCR1

=

500

;

//

Value

to

compare

with,

500

seems

a

nice

number...

By default, timers are upcounters and are reset at reaching the TIMx_ARR value, thus TIM2 never reaches 500.

for

(;;)

return

0

;

What?

JW

stforum
Associate II
Posted on January 03, 2016 at 11:01

Thank you!

I'd totally misunderstood the timer roll over.

It's working now.

TIM2

->

ARR

=

501

;

//

a test value

TIM2

->

CCR1

=

500

;

//

Value

to

compare

with,

500

seems

a

nice

number...

// do nothing and keep the compiler happy.

for

(;;)

return

0

;

Posted on January 03, 2016 at 11:57

// do nothing and keep the compiler happy.

for

(;;)

return

0

;

This is equivalent to return 0 alone, which is not ''do nothing''.

The startup code might have saved the day, if it's written with a true infinite loop after call to main().

JW

chernobay
Associate II
Posted on December 28, 2015 at 22:45

TIM2->CR1 |= TIM_CR1_CEN    //start timer

stforum
Associate II
Posted on December 31, 2015 at 11:04

Thanks - but that doesn't seem to fix things (though not enabling the timer was a big mistake).

stforum
Associate II
Posted on January 04, 2016 at 21:25

Sorry, that's an error with me copying and pasting from my terminal - it should have been

for(;;)

{}

Best wishes,

David