AVR Embedded Tutorial - Timer, Counter/de

From Free Pascal wiki
Revision as of 20:52, 26 October 2017 by Mathias (talk | contribs) (→‎Code)
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.

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;
const
  WGM10 = 0;

  procedure sei; assembler; inline;
  asm
           Sei
  end;

  procedure cli; assembler; inline;
  asm
           Cli
  end;

  procedure Timer0_Interrupt; public Name 'TIMER0_COMPA_ISR'; interrupt;
  const
    t = 10;
  const
    p: integer = 0;
  begin
//    TCNT0 := 128;
    Inc(p);
    if (p = t) then begin
      PORTD := PORTD or (1 shl 0);
    end;
    if (p = t * 2) then begin
      PORTD := PORTD and not (1 shl 0);
      p := 0;
    end;
  end;

  procedure Timer1_Interrupt; public Name 'TIMER1_COMPA_ISR'; interrupt;
  const
    t = 500;
  const
    p: integer = 0;
  begin
    Inc(p);
    if (p = t) then begin
      PORTD := PORTD or (1 shl 1);
    end;
    if (p = t * 2) then begin
      PORTD := PORTD and not (1 shl 1);
      p := 0;
    end;
  end;


begin
  cli();
  DDRB := 0;
  DDRD := DDRD or (1 shl 0) or (1 shl 1);

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

  // -- Timer1 initialisieren
  TCCR1A := (1 shl WGM10);
  TCCR1B := TCCR1B or %010;
  TIMSK := TIMSK or (1 shl OCIE1A);

  sei();
  repeat
  until 1 = 2;
end.