Cronometru pentru taxare (lei/ora)

 

  1 /*
  2   * sfarnitoare.c
  3   *
  4   * Created: 03.07.2016 07:51:31
  5   * Author : Simpalean Nicolae
  6   */ 
  7  
  8  #include <avr/io.h>
  9  #include <avr/interrupt.h>
 10  #include <util/delay.h>
 11  #include <avr/eeprom.h>
 12  #include "lcd_lib.h"
 13  #include <stdlib.h>
 14  ////////////////////////////////////////
 15  volatile unsigned int   milisecunde=0;
 16  /////////////////////////////////////////
 17  volatile uint8_t  secunde=0;
 18  uint8_t EEMEM secunde_memo=0;
 19  ////////////////////////////////////
 20  volatile uint8_t  minute=0;
 21  uint8_t EEMEM minute_memo=0;
 22  uint8_t minut_anterior=0;
 23  ///////////////////////////////////
 24  volatile float   ore=0;
 25  float EEMEM ore_memo=0;
 26  //////////////////////////////////
 27  uint8_t StareActualaPinB2=0;
 28  uint8_t StarePrecedentaPinB2=0;
 29  //////////////////////////////////
 30  uint16_t nr_porniri=0;
 31  uint16_t EEMEM nr_porniri_mem=0;
 32  //////////////////////////////////
 33  float tarifPeOra=1;
 34  float EEMEM tarifPeOra_mem=1;
 35  ///////////////////////////////////
 36  int crono_reset=0;
 37  float tarif=0;
 38  uint8_t display=0;
 39  ///////////////////////////////////
 40   char ore_afisate[12];
 41   char tarif_afisat[12];
 42   char nr_porniri_afisat[12];
 43   char tarifPeOra_afisat[12];
 44   //////////////////////////////////
 45   void afisare_text(void)
 46   {
 47       LCDclr();
 48       LCDGotoXY(0,0);     
 49       LCDWriteString("** Cronometru **");
 50       LCDGotoXY(0,1);
 51       LCDWriteString("Tarif    lei/ora");     
 52       dtostrf(tarifPeOra,4,0,tarifPeOra_afisat) ;
 53       LCDGotoXY(5,1);
 54       LCDlitere(tarifPeOra_afisat,4);
 55       _delay_ms(2500);
 56       LCDclr();
 57   }
 58  
 59  
 60   void timer_0_init()
 61   {   
 62       TCCR0A |= (1 << WGM01);
 63       TCCR0B=(1<<WGM02)|(1<<CS01)|(1<<CS00);
 64       OCR0A=250;  
 65       TIMSK0|=(1<<OCIE0A);
 66   }
 67  
 68  int main(void)
 69  {
 70      DDRB &=~(1<<(PINB,2));
 71      PORTB|=(1<<(PINB,2));           
 72      LCDinit();
 73      _delay_ms(200); 
 74     timer_0_init();
 75     eeprom_busy_wait();
 76     nr_porniri=eeprom_read_word(&nr_porniri_mem);
 77     eeprom_busy_wait();
 78     minute=eeprom_read_byte(&minute_memo);
 79      eeprom_busy_wait();
 80      ore=eeprom_read_float(&ore_memo);
 81      eeprom_busy_wait();
 82      tarifPeOra=eeprom_read_float(&tarifPeOra_mem);
 83      afisare_text();
 84      nr_porniri++;
 85      eeprom_update_word(&nr_porniri_mem,nr_porniri); 
 86      if (bit_is_clear(PINB,2))
 87      {  tarifPeOra=1;
 88          eeprom_busy_wait();
 89          eeprom_update_float(&tarifPeOra_mem,tarifPeOra);
 90          LCDclr();
 91          LCDGotoXY(0,0);
 92          LCDWriteString("  Setare tarif: ");
 93          LCDGotoXY(0,1);
 94          LCDWriteString("         lei/ora");
 95          while(1)
 96          {           
 97                dtostrf(tarifPeOra,4,0,tarifPeOra_afisat) ;
 98                LCDGotoXY(4,1);
 99                LCDlitere(tarifPeOra_afisat,4);
100                _delay_ms(350);
101                if (bit_is_clear(PINB,2))
102                {
103                    tarifPeOra++;
104                    eeprom_update_float(&tarifPeOra_mem,tarifPeOra);
105                }         
106          }
107      }
108     sei(); 
109      while (1) 
110      {
111          if (bit_is_set(PINB,2))
112          {StareActualaPinB2=1;
113              crono_reset=0;
114          } 
115          else
116          { StareActualaPinB2=0;
117              crono_reset++;
118              if (crono_reset>100)
119              {
120                   secunde=0;
121                  minute=0;
122                  eeprom_busy_wait();
123                  eeprom_update_byte(&minute_memo,minute);
124                   ore=0;
125                   eeprom_busy_wait();
126                   eeprom_update_float(&ore_memo,ore);
127                   nr_porniri=0;
128                   eeprom_busy_wait();
129                   eeprom_update_word(&nr_porniri_mem,nr_porniri);
130              }
131          }
132          if (StareActualaPinB2!=StarePrecedentaPinB2)
133          {
134              if (StareActualaPinB2==0)
135              {
136                  if (display==0)
137                  {display=1;
138                  } 
139                  else if (display==1)
140                  { display=0;
141                  }
142              }
143          }
144          StarePrecedentaPinB2=StareActualaPinB2;
145          ////////////////////////////////////////////////////        
146          if (display==0)
147          {               
148          dtostrf(ore,3,0,ore_afisate);
149           LCDGotoXY(0,0);
150           LCDWriteString("Crono: ");
151          LCDGotoXY(7,0);
152          LCDlitere(ore_afisate,3);
153          LCDWriteString(":");
154          LCDWriteInt(minute,2);
155          LCDWriteString(":");                
156          LCDWriteInt(secunde,2);
157          ////////////////////////////////
158          //tarif=(minute*0.25)+(ore*15);
159          tarif=(minute*tarifPeOra/60)+(ore*tarifPeOra);
160          dtostrf(tarif,6,2,tarif_afisat);
161          LCDGotoXY(0,1);
162          LCDWriteString("Tarif:");
163          LCDGotoXY(6,1);
164          LCDlitere(tarif_afisat,6);
165          LCDWriteString(" Lei");     
166      }
167      if (display==1)
168      {  LCDGotoXY(0,0);
169          LCDWriteString("Numar de porniri");
170          LCDGotoXY(0,1);
171          LCDWriteString("motor = ");
172          dtostrf(nr_porniri,3,0,nr_porniri_afisat);
173          LCDlitere(nr_porniri_afisat,3);
174          LCDWriteString("     ");
175          
176      }
177          
178          if (minute!=minut_anterior)
179          {
180              eeprom_update_byte(&minute_memo,minute);
181              eeprom_busy_wait();
182              eeprom_update_float(&ore_memo,ore);
183          }
184          minut_anterior=minute;              
185      }
186  }
187  
188  
189  ISR(TIMER0_COMPA_vect)
190  {
191      milisecunde++;
192      if(milisecunde==998)
193      {
194          secunde++;
195          milisecunde=0;
196          if(secunde==60)
197          {
198              minute++;
199              secunde=0;
200              if (minute==60)
201              {   ore++;
202                  minute=0;
203              }
204          }
205      }
206