1

We are storing timer in EEPROM_Write but data wipes out due to unknown reason. Your help is highly appreciated. In short if you have a code to run a life-timer ( Mileage Meter in Bike, storage last stage on system poweroff and restore from last point ) in PIC 18F452, Kindly share with us.

<pre>
/*
==============================LCD
EN    RB0    PIN # 33
RS    RB1    PIN # 34
D4    RB4    PIN # 37
D5    RB5    PIN # 38
D6    RB6    PIN # 39
D7    RB7    PIN # 40
MICROCHIP
*/
unsigned int time = 0;
unsigned int sec=0;
unsigned int minutes=0;
unsigned int hour=0;
char Select_Mode=0;
unsigned int LT_minutes;
unsigned int LT_hour;

char txt2[]="0000000000";
char txt3[]="000000000000";
char txt4[]="0000000000";
char txt5[]="0000000000";
char txt10[]="000000000000";
char txt11[]="000000000000";

// LCD module connections
sbit LCD_RS at RB1_bit;
sbit LCD_EN at RB0_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB1_bit;
sbit LCD_EN_Direction at TRISB0_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

//char uart_rd;

char txt8[] = "Tele Fun";
char txt9[] = "0900-78601";

//Timer1
//Prescaler 1:8; TMR1 Preload = 3035; Actual Interrupt Time : 100 ms

//Place/Copy this part in declaration section

void InitTimer1(){
  T1CON         = 0x31;
  TMR1IF_bit    = 0;
  TMR1H         = 0x6D;
  TMR1L         = 0x84;
  TMR1IE_bit    = 1;
  INTCON        = 0xC0;
}

void Interrupt(){
  if (TMR1IF_bit){
    TMR1IF_bit = 0;
    TMR1H         = 0x6D;
    TMR1L         = 0x84;
    //Enter your code here
    time++;

    // time 1 = 100ms, time 2 = 200ms
    if (time==10){
          time=0;
          sec++;
          if (sec==60){sec=0;minutes++;LT_minutes++;}
          if (minutes==60){minutes=0;hour++;}
          if (LT_minutes==60){
            //EEPROM_Write(0x03,LT_minutes);

          LT_minutes=0;
          LT_hour++;
          //EEPROM_Write(0x04,LT_hour);
          }
          if (LT_hour==9999){LT_hour=0;}
      }
  }
}

void main() {

  ADCON0=0b00010101;
  ADCON1=0b10001110;
  Lcd_Init();                      // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display

  TRISE0_bit = 0;                  // set RB0 pin as output
  TRISE1_bit = 0;

  LT_minutes = EEPROM_Read(0x03);
  if ((LT_minutes>=0)&&(LT_minutes<=60))LT_minutes = EEPROM_Read(0x03);
  else EEPROM_Write(0x03,0);LT_minutes = EEPROM_Read(0x03);
  delay_ms(10);


  if ((LT_hour>=0)&&(LT_hour<=9999))LT_hour = EEPROM_Read(0x04);
  else EEPROM_Write(0x04,0);LT_hour = EEPROM_Read(0x04);
  delay_ms(10);

  if (LT_hour==255){LT_hour=0;}

  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

  InitTimer1();

  while (1) {                           // Endless loop
     if (sec==15){
         EEPROM_Write(0x03,LT_minutes);
         EEPROM_Write(0x04,LT_hour);

      }

        Lcd_Out(1,11,"  ");            // Write text in first row
        Lcd_Out(2,9,"   ");            // Write text in first row
        Lcd_Out(1,1,"LT");             // Write text in first row
        Lcd_Out(2,1,"RT");             // Write text in first row
        Lcd_Out(2,6,":");              // Write text in first row
        Lcd_Out(1,8,":");              // Write text in first row
        Lcd_Out(3,2,txt8);             // Write text in first row
        Lcd_Out(4,5,txt9);             // Write text in first row


        WordToStrWithZeros(minutes, txt2);
        WordToStrWithZeros(hour, txt3);


         EEPROM_Write(0x03,LT_minutes);
         EEPROM_Write(0x04,LT_hour);


        WordToStrWithZeros(LT_minutes, txt10);
        WordToStrWithZeros(LT_hour, txt11);

        Lcd_Chr(1,4,txt11[1]);            // LT H
        Lcd_Chr(1,5,txt11[2]);            // LT H
        Lcd_Chr(1,6,txt11[3]);            // LT H
        Lcd_Chr(1,7,txt11[4]);            // LT H

        Lcd_Chr(1,9,txt10[3]);           // LT M
        Lcd_Chr(1,10,txt10[4]);          // LT M

        Lcd_Chr(2,7,txt2[3]);
        Lcd_Chr(2,8,txt2[4]);

        Lcd_Chr(2,4,txt3[3]);
        Lcd_Chr(2,5,txt3[4]);
    }
    }
<code>
comsian
  • 165
  • 1
  • 1
  • 7
  • Which event should triger the EEPROM_Write procedure? – GJ. Jul 28 '16 at 14:50
  • It should store time, Let me explain the scenario, I am running two counters here. 1- Life time (Hours up to 9999, Minutes up to 59) 2- Reset able timer (or power reset). When we restart the system, some times Life timer save it very well, Some time remove hour from the time and make it zero. e.g current life time is 10 hours and five minutes. On first restart time remains the same and continue. On Second restart same behavior but on third restart it might be on five minutes and zero hours. – comsian Jul 28 '16 at 16:57

1 Answers1

1

If you are writing in EEPROM at the time of power-off then I would say check how long it takes to write into EEPROM and I think you might loosing power totally before it finished writing into EEPROM. Putting some Capacitor between VCC-GND to store some extra power might resolve if problem is related to hardware and specifically missing at the time of power-off.

HallMark
  • 101
  • 9
  • Thank you. Kindly recommend the Capacitor. – comsian Aug 02 '16 at 06:32
  • Actually this depends on what is your whole board current consumption but you can try out with 4700uf/16v. But is that confirmed that you are facing this issue at the time of power OFF mainly? – HallMark Aug 02 '16 at 08:03