ARM Embedded Tutorial - Simple Timer/de

From Lazarus wiki
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Deutsch (de) English (en)

Einfache Timer Funktion

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.

Programm Kopf

program Timer_Blink;

uses
  cortexm3;


Konstanten und Bezeichner

const
  TIM_CR1_CEN: uInt16        =     $0001; // Counter enable
  RCC_APB1ENR_TIM2EN: uInt32 = $00000001; // TIM2 clock enabled

  TIM_DIER_CC1IE: uInt16     =     $0002; // Capture/Compare 1 interrupt enable
  TIM_SR_CC1IF: uInt16       =     $0002; // Capture/Compare 1 interrupt Flag
  TIM2_IRQn                  =        28; // TIM2 global Interrupt

// Für einen einfachen Zugriff auf die LED PC13.
type
  TLed = bitpacked array[0..15] of boolean;
var
  LedC: TLed absolute GPIOC.ODR;

Interrupt Funktion

procedure Timer2_Interrupt; public Name 'TIM2_global_interrupt'; interrupt;
begin
  TIM2.SR  := TIM2.SR and not TIM_SR_CC1IF; // clear CC1IF
  LedC[13] := not LedC[13];                 // LED PC13 umschalten
end;

Hauptprogramm und Inizialisierungen

Mit TIM2.PSC und TIM2.ARR kann man die Geschwindigkeit des Timers einstellen, je kleiner die Werte, desto schneller.

begin
  // Ports einschalten
  RCC.APB2ENR := RCC.APB2ENR or (%111 shl 2);

  // Ports auf Ausgabe schalten
  GPIOC.CRL := $33333333;
  GPIOC.CRH := $33333333;

  // --- Timer ---
  // RCC enable for Timer2
  RCC.APB1ENR := RCC.APB1ENR or RCC_APB1ENR_TIM2EN;

  // Various settings in default
  TIM2.CR1   := $0000; // Control register 1; Wait with timer enable until settings complete
  TIM2.CR2   := $0000; // Control register 2
  TIM2.CCMR1 := $0000; // Capture/compare mode register
  TIM2.CCER  := $0000; // Capture/compare enable register

  // Prescaler
  TIM2.PSC := 1000;
  // AutoReload value
  TIM2.ARR := 1000;

  // DMA and Interrupt control register
  TIM2.DIER := TIM_DIER_CC1IE; // Compare 1 interrupt enable

  // Enable Counter
  TIM2.CR1 := TIM2.CR1 or TIM_CR1_CEN;

  // NVIC configuration
  NVIC.IP[TIM2_IRQn] := $20;   // IRQ-Priority, right 4bits always to be zero
  NVIC.ISER[TIM2_IRQn shr 5] := 1 shl (TIM2_IRQn and $1F); // Enable Interrupt

  // --- Hauptschleife ---
  while True do begin
    // Mache irgend etwas.
  end;
end.

Mehrere Timer

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.

Siehe auch

Autor: Mathias