PIC 18F452寿命计时器存储

时间:2016-07-28 12:00:38

标签: c microcontroller pic eeprom

我们将计时器存储在EEPROM_Write中,但由于未知原因数据被清除。非常感谢您的帮助。 简而言之,如果您有一个代码来运行PIC 18F452中的终身计时器(自行车里程表,系统断电时存储最后阶段并从最后一点恢复),请与我们分享。

<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>

1 个答案:

答案 0 :(得分:1)

如果你在断电时写入EEPROM,我会说要检查写入EEPROM需要多长时间,我认为你可能在写入EEPROM之前完全失去电源。 如果问题与硬件有关并且在断电时特别丢失,则可以在VCC-GND之间放置一些电容以存储一些额外的功率。

相关问题