Difference between revisions of "AVR Embedded Tutorial - Timer, Counter/de"

From Free Pascal wiki
Jump to navigationJump to search
Line 7: Line 7:
 
==Beispiel-Code==
 
==Beispiel-Code==
 
<syntaxhighlight lang="pascal">program Project1;
 
<syntaxhighlight lang="pascal">program Project1;
const
 
  WGM10 = 0;
 
  
   procedure sei; assembler; inline;
+
   procedure sei; assembler; inline; // Interrupt aus
 
   asm
 
   asm
 
           Sei
 
           Sei
 
   end;
 
   end;
  
   procedure cli; assembler; inline;
+
   procedure cli; assembler; inline; // Interrupt ein
 
   asm
 
   asm
 
           Cli
 
           Cli
Line 22: Line 20:
 
   procedure Timer0_Interrupt; public Name 'TIMER0_COMPA_ISR'; interrupt;
 
   procedure Timer0_Interrupt; public Name 'TIMER0_COMPA_ISR'; interrupt;
 
   const
 
   const
     t = 10;
+
     t = 10;         // LED sollte nur bei jedem 10. Durchlauf umschalten.
 
   const
 
   const
 
     p: integer = 0;
 
     p: integer = 0;
 
   begin
 
   begin
//    TCNT0 := 128;
+
    TCNT0 := 128;                       // Speed halbieren  0 = langsm (default)
 
     Inc(p);
 
     Inc(p);
 
     if (p = t) then begin
 
     if (p = t) then begin
       PORTD := PORTD or (1 shl 0);
+
       PORTD := PORTD or (1 shl 0);     // LED Pin0 ein
 
     end;
 
     end;
 
     if (p = t * 2) then begin
 
     if (p = t * 2) then begin
       PORTD := PORTD and not (1 shl 0);
+
       PORTD := PORTD and not (1 shl 0); // LED Pin1 aus
 
       p := 0;
 
       p := 0;
 
     end;
 
     end;
Line 39: Line 37:
 
   procedure Timer1_Interrupt; public Name 'TIMER1_COMPA_ISR'; interrupt;
 
   procedure Timer1_Interrupt; public Name 'TIMER1_COMPA_ISR'; interrupt;
 
   const
 
   const
     t = 500;
+
     t = 500;       // LED sollte nur bei jedem 500. Durchlauf umschalten.
 
   const
 
   const
 
     p: integer = 0;
 
     p: integer = 0;
Line 45: Line 43:
 
     Inc(p);
 
     Inc(p);
 
     if (p = t) then begin
 
     if (p = t) then begin
       PORTD := PORTD or (1 shl 1);
+
       PORTD := PORTD or (1 shl 1);     // LED Pin1 ein
 
     end;
 
     end;
 
     if (p = t * 2) then begin
 
     if (p = t * 2) then begin
       PORTD := PORTD and not (1 shl 1);
+
       PORTD := PORTD and not (1 shl 1); // LED Pin1 aus
 
       p := 0;
 
       p := 0;
 
     end;
 
     end;
Line 55: Line 53:
  
 
begin
 
begin
 +
  // Interupt unterbinden.
 
   cli();
 
   cli();
   DDRB := 0;
+
 
 +
   // -- Bit 0 und 1 von PortD auf Ausgabe stellen.
 
   DDRD := DDRD or (1 shl 0) or (1 shl 1);
 
   DDRD := DDRD or (1 shl 0) or (1 shl 1);
  
   // -- Timer0 initialisieren
+
   // -- Timer0 initialisieren.
 
   TCCR0A := 0;
 
   TCCR0A := 0;
   TCCR0B := %101;
+
   TCCR0B := %101; // CPU-Takt / 1024
 
   TIMSK := TIMSK or (1 shl OCIE0A);
 
   TIMSK := TIMSK or (1 shl OCIE0A);
  
   // -- Timer1 initialisieren
+
   // -- Timer1 initialisieren.
   TCCR1A := (1 shl WGM10);
+
   TCCR1A := 1;
   TCCR1B := TCCR1B or %010;
+
   TCCR1B := %010; // CPU-Takt / 8
 
   TIMSK := TIMSK or (1 shl OCIE1A);
 
   TIMSK := TIMSK or (1 shl OCIE1A);
  
 +
  // -- Interrupt aktivieren.
 
   sei();
 
   sei();
 
   repeat
 
   repeat
 +
    // Mache Irgendwas.
 
   until 1 = 2;
 
   until 1 = 2;
 
end.</syntaxhighlight>
 
end.</syntaxhighlight>

Revision as of 21:03, 26 October 2017

Titel

Dieses Beispiel zeigt, wie man die beiden Timer eines ATtiny2313 verwenden kann. Jeder Timer steuert eine LED an, welche an Bit 0 und 1 des PortD sind. Hier geht es nicht, das man alle Register versteht, dies sollte nur ein praktisches Beispiel sein, wie man es in FPC umsetzt. Genauere Details gibt es hier: https://www.mikrocontroller.net/articles/AVR-GCC-Tutorial/Die_Timer_und_Z%C3%A4hler_des_AVR

Beispiel-Code

program Project1;

  procedure sei; assembler; inline; // Interrupt aus
  asm
           Sei
  end;

  procedure cli; assembler; inline; // Interrupt ein
  asm
           Cli
  end;

  procedure Timer0_Interrupt; public Name 'TIMER0_COMPA_ISR'; interrupt;
  const
    t = 10;         // LED sollte nur bei jedem 10. Durchlauf umschalten.
  const
    p: integer = 0;
  begin
    TCNT0 := 128;                       // Speed halbieren  0 = langsm (default)
    Inc(p);
    if (p = t) then begin
      PORTD := PORTD or (1 shl 0);      // LED Pin0 ein
    end;
    if (p = t * 2) then begin
      PORTD := PORTD and not (1 shl 0); // LED Pin1 aus
      p := 0;
    end;
  end;

  procedure Timer1_Interrupt; public Name 'TIMER1_COMPA_ISR'; interrupt;
  const
    t = 500;        // LED sollte nur bei jedem 500. Durchlauf umschalten.
  const
    p: integer = 0;
  begin
    Inc(p);
    if (p = t) then begin
      PORTD := PORTD or (1 shl 1);      // LED Pin1 ein
    end;
    if (p = t * 2) then begin
      PORTD := PORTD and not (1 shl 1); // LED Pin1 aus
      p := 0;
    end;
  end;


begin
  // Interupt unterbinden.
  cli();

  // -- Bit 0 und 1 von PortD auf Ausgabe stellen.
  DDRD := DDRD or (1 shl 0) or (1 shl 1);

  // -- Timer0 initialisieren.
  TCCR0A := 0;
  TCCR0B := %101;  // CPU-Takt / 1024
  TIMSK := TIMSK or (1 shl OCIE0A);

  // -- Timer1 initialisieren.
  TCCR1A := 1;
  TCCR1B := %010;  // CPU-Takt / 8
  TIMSK := TIMSK or (1 shl OCIE1A);

  // -- Interrupt aktivieren.
  sei();
  repeat
    // Mache Irgendwas.
  until 1 = 2;
end.