ARM Embedded Tutorial - Simple Timer: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: ARM Embedded Tutorial - Simple Timer
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # Simple timer function | |
| 2 | + | ||
| 3 | + | In this example, a simple timer for an STM32F103C is used. This microcontroller has 4 timers and you can use timers 2-4 very easily. Timer 1 has several options, so it is more complicated to use. | |
| 4 | + | ||
| 5 | + | ## Program head | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | program Timer_Blink; | |
| 9 | + | ||
| 10 | + | uses | |
| 11 | + | cortexm3; | |
| 12 | + | ``` | |
| 13 | + | ||
| 14 | + | ## Constants and identifiers | |
| 15 | + | ||
| 16 | + | ``` pascal | |
| 17 | + | const | |
| 18 | + | TIM_CR1_CEN : uInt16 = $0001; // Counter enable | |
| 19 | + | RCC_APB1ENR_TIM2EN : uInt32 = $00000001; // TIM2 clock enabled | |
| 20 | + | ||
| 21 | + | TIM_DIER_CC1IE : uInt16 = $0002; // Capture / Compare 1 interrupt enable | |
| 22 | + | TIM_SR_CC1IF : uInt16 = $0002; // Capture / Compare 1 interrupt flag | |
| 23 | + | TIM2_IRQn = 28; // TIM2 global interrupt | |
| 24 | + | ||
| 25 | + | // For easy access to the LED PC13. | |
| 26 | + | Type | |
| 27 | + | TLed = bitpacked array[0..15] of boolean; | |
| 28 | + | ||
| 29 | + | var | |
| 30 | + | LedC : TLed absolute GPIOC.ODR; | |
| 31 | + | ``` | |
| 32 | + | ||
| 33 | + | ## Interrupt function | |
| 34 | + | ||
| 35 | + | ``` pascal | |
| 36 | + | procedure Timer2_Interrupt; public name 'TIM2_global_interrupt'; interrupt; | |
| 37 | + | begin | |
| 38 | + | TIM2.SR := TIM2.SR and not TIM_SR_CC1IF; // Clear CC1IF | |
| 39 | + | LedC[13] := not LedC[13]; // Toggle LED PC13 | |
| 40 | + | end; | |
| 41 | + | ``` | |
| 42 | + | ||
| 43 | + | ## Main program and initialization | |
| 44 | + | ||
| 45 | + | With **TIM2.PSC** and **TIM2.ARR** you can set the speed of the timer, the smaller the values, the faster it runs. | |
| 46 | + | ||
| 47 | + | ``` pascal | |
| 48 | + | begin | |
| 49 | + | // Turn on ports | |
| 50 | + | RCC.APB2ENR := RCC.APB2ENR or (%111 shl 2); | |
| 51 | + | ||
| 52 | + | // Switch ports to output | |
| 53 | + | GPIOC.CRL := $33333333; | |
| 54 | + | GPIOC.CRH := $33333333; | |
| 55 | + | ||
| 56 | + | // --- timer --- | |
| 57 | + | // RCC enable for Timer2 | |
| 58 | + | RCC.APB1ENR := RCC.APB1ENR or RCC_APB1ENR_TIM2EN; | |
| 59 | + | ||
| 60 | + | // Various default settings | |
| 61 | + | TIM2.CR1 := $0000; // Control register 1; Wait with timer enable until settings complete | |
| 62 | + | TIM2.CR2 := $0000; // Control register 2 | |
| 63 | + | TIM2.CCMR1 := $0000; // Capture / compare mode register | |
| 64 | + | TIM2.CCER := $0000; // Capture / compare enable register | |
| 65 | + | ||
| 66 | + | // Prescaler | |
| 67 | + | TIM2.PSC := 1000; | |
| 68 | + | ||
| 69 | + | // AutoReload value | |
| 70 | + | TIM2.ARR := 1000; | |
| 71 | + | ||
| 72 | + | // DMA and interrupt control register | |
| 73 | + | TIM2.DIER := TIM_DIER_CC1IE; // Compare 1 interrupt enable | |
| 74 | + | ||
| 75 | + | // Enable counter | |
| 76 | + | TIM2.CR1 := TIM2.CR1 or TIM_CR1_CEN; | |
| 77 | + | ||
| 78 | + | // NVIC configuration | |
| 79 | + | NVIC.IP[TIM2_IRQn] := $20; // IRQ priority, right 4 bits always to be zero | |
| 80 | + | NVIC.ISER[TIM2_IRQn shr 5] := 1 shl (TIM2_IRQn and $1F); // Enable interrupt | |
| 81 | + | ||
| 82 | + | // --- main loop --- | |
| 83 | + | while True do begin | |
| 84 | + | // Do something | |
| 85 | + | end; | |
| 86 | + | end. | |
| 87 | + | ``` | |
| 88 | + | ||
| 89 | + | ## Multiple timers | |
| 90 | + | ||
| 91 | + | You can use 3 simple timers in parallel. These are Timer2 to Timer4. Timer1 is used differently. If you want to use timers 3 and 4, you simply have to replace TIM2 with TIM3 and/or TIM4. | |
| 92 | + | ||
| 93 | + | # See also | |
| 94 | + | ||
| 95 | + | - [[ARM Embedded Tutorials]] - Overview | |
| 96 | + | ||
| 97 | + | --- | |
| 98 | + | *From the Free Pascal wiki, page "ARM Embedded Tutorial - Simple Timer", as it stood on 2021-02-01 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 99 | + | Category: ARM | |
| 100 | + | Category: Embedded | |
| 101 | + | Category: Tutorials |
