Sunday, May 20, 2018

Attiny2313 Sequential Timer

(********************************************************************
Project ............: Sequential Timer 4 Outputs
Author .............: Indrit Bekteshi / I-BEK Car Electrician
Chip name ..........: Attiny2113A
Version ............: 1.0 (2017)
Clock frequency ....: 8,0000 MHz (int.)
Ide/Compiler .......: mikroPascal PRO for AVR 6.1

 
                       !!!!!KUJDES, KY PROGRAM ESHTE PERSONAL/SHEMBULL!!!!!
 
********************************************************************)
 
Program Sequetial_Timer;
 
Type PByte = ^Byte; //pointer (max 255)
 
Type
     TValvula = Record
       Koha_Ndeze : Word;
       Koha_Fike  : Word;
     End;
 
Type TSeq_Timer = Record
       Valvula      : TValvula; //reference per strukturen e valvulave.
       Butoni       : Byte;
       Koha_Butonit : Byte;
       Koha_Ndeze   : Word;
       Koha_Fike    : Word;
       Dalja        : Byte;
   End;
 
Const
      NDEZE            = 1;
      FIKE             = 0;
 
      BUTONI_OK        = 0;
      BUTONI_PLUS      = BUTONI_OK + 1;
      BUTONI_MINUS     = BUTONI_PLUS + 1;
 
      KOHA_PUNE_MAX    = 10;
      KOHA_PUNE_MIN    = 1;
 
      KOHA_PUSHIM_MAX  = 2000;
      KOHA_PUSHIM_MIN  = 10;
 
      BAITET_EE        = SizeOf(TValvula);
      ADRESA_EE        : Byte = $00;
 
Var
    butoni_pin_p       : sbit at PINB.0;
    butoni_pin_o       : sbit at PINB.1;
    butoni_pin_m       : sbit at PINB.2;
 
    led_pune           : sbit at PORTB.3//tregon gradimin pune
    led_pushim         : sbit at PORTB.4//tregon gradimin pushim
    led_buton          : sbit at PORTD.6;
 
    Seq_Timer          : TSeq_Timer;
 
Procedure Shkruaj_EEprom(Ptr_Data : PByte); Forward;
Procedure Lexo_EEprom(Ptr_Data : PByte); Forward;
Procedure Gjendja_Valvulat(Gjendja : Byte); Forward;
Procedure Rinis_Valvulat(); Forward;
 
 
Procedure Shkruaj_EEprom(Ptr_Data : PByte);
Var i : Byte;
Begin
  For i := 0 To (BAITET_EE - 1) Do
  Begin
    While ((EECR) And (1 Shl EEPE)) Do begin end;
    EECR := (0 Shl EEPM1) Or (0 Shl EEPM0);
    EEAR := ADRESA_EE + i; { max is 255 }
    EEDR := Ptr_Data^;
    EECR := (EECR Or (1 Shl EEMPE));
    EECR := (EECR Or (1 Shl EEPE));
    Inc(Ptr_Data);
  End;
End;
 
Procedure Lexo_EEprom(Ptr_Data : PByte);
Var i : Byte;
Begin
  For i := 0 To (BAITET_EE - 1) Do
  Begin
    While ((EECR) And (1 Shl EEPE)) Do begin end;
    EEAR := ADRESA_EE + i; { max is 255 }
    EECR := (EECR Or (1 Shl EERE));
    Ptr_Data^ := EEDR;
    Inc(Ptr_Data);
  End;
End;
 
Procedure Gjendja_Valvulat(Gjendja : Byte);
Begin
  If (Gjendja = NDEZE) Then
  Seq_Timer.Dalja := 2;
  If (Seq_Timer.Dalja > 5) Then
  Inc(Seq_Timer.Dalja);
  PORTD.Seq_Timer.Dalja := Gjendja;
End;
 
Procedure Rinis_Valvulat();
Begin
 
  for Seq_Timer.Dalja := 2 To 5 Do
    PORTD.Seq_Timer.Dalja := 0;
  Seq_Timer.Koha_Ndeze := 0;
  Seq_Timer.Koha_Fike := 0;
  Seq_Timer.Dalja := 1;
End;
 
Procedure T0_OVF_Int(); iv IVT_ADDR_TIMER0_OVF;
Begin
  If (Seq_Timer.Koha_Butonit >= 10) Then
  Begin
    If (butoni_pin_o = 0) Then Seq_Timer.Butoni := BUTONI_OK;
    If (butoni_pin_p = 0) Then Seq_Timer.Butoni := BUTONI_PLUS;
    If (butoni_pin_m = 0) Then Seq_Timer.Butoni := BUTONI_MINUS;
    If (Seq_Timer.Butoni <> 0) Then
    Begin
      TIMSK := %00000010;
      Seq_Timer.Koha_Butonit := 0;
      Rinis_Valvulat();
      TIMSK := %01000010;
    End;
  End;
  Inc(Seq_Timer.Koha_Butonit);
  TCNT0 := 0;
End;
 
Procedure T1_CMPA_ISR(); iv IVT_ADDR_TIMER1_COMPA;
Begin
  If (Seq_Timer.Koha_Ndeze >= Seq_Timer.Valvula.Koha_Ndeze) Then
  Begin
    Gjendja_Valvulat(FIKE);
    Seq_Timer.Koha_Fike := 0;
  End;
  Inc(Seq_Timer.Koha_Ndeze);
  If (Seq_Timer.Koha_Fike >= Seq_Timer.Valvula.Koha_Fike) Then
  Begin
    Gjendja_Valvulat(NDEZE);
    Seq_Timer.Koha_Ndeze := 0;
  End;
  Inc(Seq_Timer.Koha_Fike);
End;
 
Begin
  //Cilsimet per Portat
  DDRA  := %00000000;
  PORTA := %11111111;
  DDRB  := %00011000;
  PORTB := %11110111;
  DDRD  := %01111100;
  PORTD := %10000011;
  //timer0
  TCCR0B := %00000101;
  //timer1 
  //10ms
  OCR1AH := $30;
  OCR1AL := $D4;
  TCCR1B := %00001011;
  TIMSK := %01000010;
 
  //lexo ne eprom vlerat e ruajtura.
  Lexo_EEProm(@Seq_Timer.Valvula);
  //kontollo nese vlerat jane ne brenda limitit te lejuar,
  //perndryshe korigjo me vlera te pracaktuara.
  If (Seq_Timer.Valvula.Koha_Ndeze > KOHA_PUNE_MAX)  Or
     (Seq_Timer.Valvula.Koha_Ndeze < KOHA_PUNE_MIN)  Or
     (Seq_Timer.Valvula.Koha_Fike > KOHA_PUSHIM_MAX) Or
     (Seq_Timer.Valvula.Koha_Fike < KOHA_PUSHIM_MIN) Then
    Begin
      Seq_Timer.Valvula.Koha_Ndeze := 0;
      Seq_Timer.Valvula.Koha_Fike := 10;
    End;
 
  Seq_Timer.Butoni := 0;
 
  Rinis_Valvulat();
 
  //Nis interruptet ktu
  Asm Sei End;
 
  //cikel pa fund.
  While (True) Do
  Begin
    butoni_pin_o := 0;
    Case Seq_Timer.Butoni Of
      BUTONI_OK : // Butoni OK
      Begin
        If (PINB3_bit = 0) Then
        Begin
          led_pushim := 0;
          led_pune := 1;
        End
        Else
        Begin
          led_pune := 0;
          led_pushim := 1;
        End;
        Delay_Ms(500);
        Seq_Timer.Butoni := 0;
      End;
      BUTONI_PLUS : //
      Begin
        If (PINB3_bit = 1) Then
        Begin
          If (Seq_Timer.Valvula.Koha_Ndeze < KOHA_PUNE_MAX) Then
          Begin
            Seq_Timer.Valvula.Koha_Ndeze := Seq_Timer.Valvula.Koha_Ndeze + 1;
            Shkruaj_EEprom(@Seq_Timer.Valvula);
            led_buton := Not led_buton;
          End;
        End
        Else
        Begin
          If (Seq_Timer.Valvula.Koha_Fike < KOHA_PUSHIM_MAX) Then
          Begin
            Seq_Timer.Valvula.Koha_Fike := Seq_Timer.Valvula.Koha_Fike + 5;
            Shkruaj_EEprom(@Seq_Timer.Valvula);
            led_buton := Not led_buton;
          End;
        End;
        Seq_Timer.Butoni := 0;
      End;
      BUTONI_MINUS : // Butoni Minus
      Begin
        If (PINB3_bit = 1) Then
        Begin
          If (Seq_Timer.Valvula.Koha_Ndeze > KOHA_PUNE_MIN) Then
          Begin
            Seq_Timer.Valvula.Koha_Ndeze := Seq_Timer.Valvula.Koha_Ndeze - 1;
            Shkruaj_EEprom(@Seq_Timer.Valvula);
            led_buton := Not led_buton;
          End;
        End
        Else
        Begin
          If (Seq_Timer.Valvula.Koha_Fike > KOHA_PUSHIM_MIN) Then
          Begin
            Seq_Timer.Valvula.Koha_Fike := Seq_Timer.Valvula.Koha_Fike - 5;
            Shkruaj_EEprom(@Seq_Timer.Valvula);
            led_buton := Not led_buton;
          End;
        End;
        Seq_Timer.Butoni := 0;
      End;
    End;
    // Ktu Mbaron Kontrolli Butonave
    // Komentetimi programit eshte per fillestare
    // Programi ska nevoje per komente, vet shpjegohet!
    //===============================================================
 
    //pjesa tjeter e programit kryhet nga interruptet
 
  End;
End.