2015-12-24 09:32 AM
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 #timers2015-01-02 06:36 AM
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? JW2015-01-03 02:01 AM
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
;
2015-01-03 02:57 AM
// 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(). JW2015-12-28 01:45 PM
TIM2->CR1 |= TIM_CR1_CEN //start timer
2015-12-31 02:04 AM
Thanks - but that doesn't seem to fix things (though not enabling the timer was a big mistake).
2016-01-04 12:25 PM
Sorry, that's an error with me copying and pasting from my terminal - it should have been
for(;;) {} Best wishes, David