- No category
advertisement
9 Timers
A peripheral found in all microcontrollers is the timer. Timers is a counter fed by a clock. It can be considered to be a register which increments or decrements
(depending on the timer settings) by one on every tick of a clock connected to it.
The microcontroller on the Nucleo has seven general purpose timers: TIM2
-TIM5 and TIM9 - TIM11 of which the latter only count upwards. The timers can be fed with different clock sources, internally or externally.
This clock source is fed through a programmable 16-bit clock divider called "prescaler" to generate a clock signal for the timer. If the counter TIMx_CNT reaches a so called auto-reload value TIMx_ARR it resets the counter to zero and throws a event.
The counter is connected to four input capture/ output compare channels
TIMx_OC. These channels can be connected to a output pin and can work in different modes ranging from measuring the pulse length on a input to toggle a output. In figure
a simplified sketch is made to give insight in the purpose of the auto-reload and the output compare registers. Of course is this only one of the possible modes of operation. Look in the reference manual in chapter 13 or
14 for more in depth information about which registers to use.
There are also timers with more specific task: The main purpose of a watchdog is to check whether or not the code not hangs, it does this by being set periodically to an initial value, if for some reason this does not happen, then the device gets reset or an event is thrown.
Another timer is the Systick timer. This timer runs on the background and is used to generate so called ticks which are used by a realtime operating system to schedule slots for the different task.
Exercise 5
Use a Timer 2 to make an LED blink. To do this follow the following steps
1. Set up the the LED as output.
2. Enable the clock to timer 2.
3. Initialize the timer using a TIM_HandleTypeDef from the HAL.
4. Set up the timer interrupt NVIC priority and enable this interrupt using the HAL.
5. Start the timer using the HAL.
6. Make an interrupt routine for TIM2_IRQHandler to call to HAL_TIM_IRQHandler
7. Make the LED blink on the "period elapsed" routine (HAL_TIM_PeriodElapsedCallback).
27
Table 11 List of possible GPIO settings in ARM microcontrollers
Register
TIMx_CR1
TIMx_CR2
CH.
Full name
13.4.1 Control register one
13.4.2 Control register two
Function
Main control register, used for enabling the counter and setting the counter mode
Second control register, used for setting DMA and other triggering
Settings used for using the timer in slave mode TIMx_SMCR
TIMx_DIER
TIMx_SR
TIMx_EGR
TIMx_PSC
TIMx_ARR
TIMx_CCR1
13.4.3 Slave mode control register
13.4.4 DMA / interupt enable register
TIMx_CCMR1 13.4.7 Capture/ Compare mode register 1
TIMx_CCER
TIMx_CNT
TIMx_DCR
TIMx_DMAR
TIM2_OR
13.4.5 Status register
13.4.6 Event generation register
13.4.9 Capture/ Compare enable register
13.4.10 Counter register
Used for enabling the different interupt/ dma events
Houses all the interrupt flags
Settings for generating DMA events
Mode selection of the different channels
Setting for the polarity of the channels and enabling them
The register containing the counter value
13.4.11 Prescaler register
13.4.12 Auto-reload register
Prescaler used for dividing the clock
The register containing the auto-reload value
13.4.13 Capture/ Compare register channel 1
The register containing the capture/ compare value of channel one
13.4.17 DMA control register Settings for the DMA
13.4.18 DMA adress register for full transfer
The register containing the adress for burst
DMA
13.4.19 TIM2 option register Extra options
28
Figure 1: Simplified timing diagram
29
advertisement
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Related manuals
advertisement
Table of contents
- 4 Introduction
- 4 Preliminary Schedule
- 4 Acknowledgments
- 5 Setup Eclipse and Toolchain
- 5 Windows
- 5 Eclipse
- 5 Toolchain
- 6 STLink v2
- 6 Debugger - OpenOCD
- 6 Packs - Device Support
- 6 Linux
- 6 Toolchain and Eclipse
- 7 STLink flasher for Linux
- 7 Debugger - OpenOCD
- 8 Packs - Device Support
- 9 Starting a Project
- 9 Clock Configuration
- 9 Flashing the Program Memory
- 9 STLink Utility under Windows
- 9 STLink Flash under Linux
- 11 Debugging
- 12 Programming C
- 12 Data Types
- 12 Integer Data types
- 13 Floating point data types
- 13 Arrays
- 14 Structs
- 15 Enumerated type
- 15 Operators
- 15 Standard Operators
- 15 Logical Operators
- 16 Bitwise operators
- 16 Compound assignment operators
- 18 Statements in C
- 18 Conditional Statements
- 19 Iteration Statements
- 20 Functions
- 21 Microcontroller Settings
- 21 Registers
- 21 Libraries with “typedef”s
- 21 ARM clock design
- 22 GPIO
- 22 Polling and Interrupts
- 23 Polling
- 24 Interrupts
- 25 Polling vs. Interrupts
- 27 Timers
- 30 Analog Peripherals
- 32 SPI
- 32 Talking to the DAC
- 33 Project: build a Function Generator
- 33 Energy Consumption
- 34 Nucleo Board Hints
- 34 Breaking the board
- 35 Function Generator Shield