ARM Embedded Tutorial - Simple Timer/de: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: ARM Embedded Tutorial - Simple Timer/de
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # Einfache Timer Funktion | |
| 2 | + | ||
| 3 | + | In diesem Beispiel wird ein einfacher Timer von einem STM32F103C verwendet. Dieser Controller besitzt 4 Timer, wovon man man Timer 2-4 sehr einfach verwenden kann. Timer1 besitzt mehrere Möglichkeiten, daher ist er komplizierter in der Verwendung. | |
| 4 | + | ||
| 5 | + | ## Programm Kopf | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | program Timer_Blink; | |
| 9 | + | ||
| 10 | + | uses | |
| 11 | + | cortexm3; | |
| 12 | + | ``` | |
| 13 | + | ||
| 14 | + | ## Konstanten und Bezeichner | |
| 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 | + | // Für einen einfachen Zugriff auf die LED PC13. | |
| 26 | + | type | |
| 27 | + | TLed = bitpacked array[0..15] of boolean; | |
| 28 | + | var | |
| 29 | + | LedC: TLed absolute GPIOC.ODR; | |
| 30 | + | ``` | |
| 31 | + | ||
| 32 | + | ## Interrupt Funktion | |
| 33 | + | ||
| 34 | + | ``` pascal | |
| 35 | + | procedure Timer2_Interrupt; public Name 'TIM2_global_interrupt'; interrupt; | |
| 36 | + | begin | |
| 37 | + | TIM2.SR := TIM2.SR and not TIM_SR_CC1IF; // clear CC1IF | |
| 38 | + | LedC[13] := not LedC[13]; // LED PC13 umschalten | |
| 39 | + | end; | |
| 40 | + | ``` | |
| 41 | + | ||
| 42 | + | ## Hauptprogramm und Inizialisierungen | |
| 43 | + | ||
| 44 | + | Mit **TIM2.PSC** und **TIM2.ARR** kann man die Geschwindigkeit des Timers einstellen, je kleiner die Werte, desto schneller. | |
| 45 | + | ||
| 46 | + | ``` pascal | |
| 47 | + | begin | |
| 48 | + | // Ports einschalten | |
| 49 | + | RCC.APB2ENR := RCC.APB2ENR or (%111 shl 2); | |
| 50 | + | ||
| 51 | + | // Ports auf Ausgabe schalten | |
| 52 | + | GPIOC.CRL := $33333333; | |
| 53 | + | GPIOC.CRH := $33333333; | |
| 54 | + | ||
| 55 | + | // --- Timer --- | |
| 56 | + | // RCC enable for Timer2 | |
| 57 | + | RCC.APB1ENR := RCC.APB1ENR or RCC_APB1ENR_TIM2EN; | |
| 58 | + | ||
| 59 | + | // Various settings in default | |
| 60 | + | TIM2.CR1 := $0000; // Control register 1; Wait with timer enable until settings complete | |
| 61 | + | TIM2.CR2 := $0000; // Control register 2 | |
| 62 | + | TIM2.CCMR1 := $0000; // Capture/compare mode register | |
| 63 | + | TIM2.CCER := $0000; // Capture/compare enable register | |
| 64 | + | ||
| 65 | + | // Prescaler | |
| 66 | + | TIM2.PSC := 1000; | |
| 67 | + | // AutoReload value | |
| 68 | + | TIM2.ARR := 1000; | |
| 69 | + | ||
| 70 | + | // DMA and Interrupt control register | |
| 71 | + | TIM2.DIER := TIM_DIER_CC1IE; // Compare 1 interrupt enable | |
| 72 | + | ||
| 73 | + | // Enable Counter | |
| 74 | + | TIM2.CR1 := TIM2.CR1 or TIM_CR1_CEN; | |
| 75 | + | ||
| 76 | + | // NVIC configuration | |
| 77 | + | NVIC.IP[TIM2_IRQn] := $20; // IRQ-Priority, right 4bits always to be zero | |
| 78 | + | NVIC.ISER[TIM2_IRQn shr 5] := 1 shl (TIM2_IRQn and $1F); // Enable Interrupt | |
| 79 | + | ||
| 80 | + | // --- Hauptschleife --- | |
| 81 | + | while True do begin | |
| 82 | + | // Mache irgend etwas. | |
| 83 | + | end; | |
| 84 | + | end. | |
| 85 | + | ``` | |
| 86 | + | ||
| 87 | + | ## Mehrere Timer | |
| 88 | + | ||
| 89 | + | Man kann 3 einfacher Timer parallel verwenden. Das sind dir Timer2 bis Timer4. Timer1 wird anders verwendet. Will man die Timer 3 & 4 verwenden, muss man einfach TIM2 durch TIM3 und/oder TIM4 ersetzen. | |
| 90 | + | ||
| 91 | + | ## Siehe auch | |
| 92 | + | ||
| 93 | + | - Übersichtseite [[ARM Embedded Tutorials/de]] | |
| 94 | + | ||
| 95 | + | Autor: Mathias | |
| 96 | + | ||
| 97 | + | --- | |
| 98 | + | *From the Free Pascal wiki, page "ARM Embedded Tutorial - Simple Timer/de", 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: Arduino | |
| 101 | + | Category: Deutsch | |
| 102 | + | Category: Embedded |
