CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

eeprom read problem after repowering

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dsevgor



Joined: 30 Mar 2013
Posts: 11

View user's profile Send private message

eeprom read problem after repowering
PostPosted: Mon Dec 23, 2013 2:23 pm     Reply with quote

I'm having a weird eeprom problem.
I have 2 float variables which i increase or decrease in my main loop, and 2 buttons which they write each variable to their eeprom addresses, and a third button to read both variables and write to lcd.

So far so good. Everything works fine. I increase or decrease both variables save them to eeprom with buttons. Read from eeprom and write both variables to lcd with third button with no problem.

My problem is when i cut the power, repower my circuit and hit the third button before doing anything else (saving new values to eeprom), i get the first value right but the second one returns always 0. But I don't have this problem if i don't cut the power.

Here is my test code. Might be a little long but its not actually.

Thanks in advance

Code:

#include <18F2520.h>
#fuses H4
#fuses NOWDT, NOPROTECT, NOBROWNOUT, NOLVP, NOPUT,NOWRT,NODEBUG,NOCPD
#device adc = 10
#use delay(clock=48000000)
#include <internal_eeprom.c>
#include "flex_oled_lcd.c"

#DEFINE JS_RIGHT_PIN                   PIN_B6
#DEFINE JS_LEFT_PIN                    PIN_B7
#DEFINE JS_UP_PIN                      PIN_B5
#DEFINE JS_DOWN_PIN                    PIN_B4

#DEFINE JS_RIGHT                       !input(JS_RIGHT_PIN)
#DEFINE JS_LEFT                        !input(JS_LEFT_PIN)   
#DEFINE JS_UP                          !input(JS_UP_PIN)
#DEFINE JS_DOWN                        !input(JS_DOWN_PIN)

#DEFINE EEPROM_ADDRESS_A 0
#DEFINE EEPROM_ADDRESS_B 4

int8 BtnDebounceCount=0;
int8 BtnDebounceCountLimit = 100;

float epromDataA = 0;
float epromDataB = 0;

int1 lcdRefresher = 0;
int8 epromFlag = 0;

#INT_EXT
void readEP(){
   epromFlag = 1;
}
#INT_EXT1
void writeEPA(){
   epromFlag = 2;
}
#INT_EXT2
void writeEPB(){
   epromFlag = 3;
}
void lcdRefresh(){
   lcdRefresher = 0;
         printf(lcd_putc,"\f");
         lcd_gotoxy(1,1);   
         printf(lcd_putc,"%.1f",epromDataA);
         lcd_gotoxy(1,2);   
         printf(lcd_putc,"%.1f",epromDataB);
}
void main()
{
   lcd_init();
   port_b_pullups(0x11110111);
   EXT_INT_EDGE ( 0 , H_TO_L);
   EXT_INT_EDGE ( 1 , H_TO_L);
   EXT_INT_EDGE ( 2 , H_TO_L);
   ENABLE_INTERRUPTS ( INT_EXT );
   ENABLE_INTERRUPTS ( INT_EXT1 );
   ENABLE_INTERRUPTS ( INT_EXT2 );
   ENABLE_INTERRUPTS(GLOBAL);
   while(1){
      if(JS_RIGHT){
         BtnDebounceCount++;
         if(BtnDebounceCount > BtnDebounceCountLimit){
            BtnDebounceCount = 0;
            epromDataA +=1;
            lcdRefresher = 1;
         }
      }
      else if(JS_LEFT){
     
         BtnDebounceCount++;
         if(BtnDebounceCount > BtnDebounceCountLimit){
            BtnDebounceCount = 0;
            epromDataA -=1;
            lcdRefresher = 1;
         }
      }
      else if(JS_UP){
         BtnDebounceCount++;
         if(BtnDebounceCount > BtnDebounceCountLimit){
            BtnDebounceCount = 0;
            epromDataB +=1;
            lcdRefresher = 1;
         }
      }
      else if(JS_DOWN){
         BtnDebounceCount++;
         if(BtnDebounceCount > BtnDebounceCountLimit){
            BtnDebounceCount = 0;
            epromDataB -=1;
            lcdRefresher = 1;
         }
      }
      else {
         BtnDebounceCount = 0;
      }
     
      if(epromFlag==2){
         epromFlag=0;
         write_float_eeprom(EEPROM_ADDRESS_A,epromDataA);   
      }
      else if(epromFlag==3){
         epromFlag=0;
         write_float_eeprom(EEPROM_ADDRESS_B,epromDataB);   
      }
      else if(epromFlag==1){
         epromFlag=0;
         printf(lcd_putc,"\f");
         lcd_gotoxy(1,1);   
         printf(lcd_putc,"%.1f",read_float_eeprom(EEPROM_ADDRESS_A));
         lcd_gotoxy(1,2);
         printf(lcd_putc,"%.1f",read_float_eeprom(EEPROM_ADDRESS_B));
     
      }
     
      if(lcdRefresher){
         lcdRefresher=0;
         lcdRefresh();
      }
   }
}
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Mon Dec 23, 2013 2:47 pm     Reply with quote

One thing I notice that may or may not be a problem but is definitely a potential "gotchya" - the LCD displays typically take a surprisingly long period of time to power up. Running LCD init without a delay of a second or so in front of it can cause the init to fail because the LCD controller has not finished powering up yet (they can be cranky little buggers). Check the spec sheet for the LCD controller for exact timing, but you typically need a delay during startup before doing the init.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Ttelmah



Joined: 11 Mar 2010
Posts: 19330

View user's profile Send private message

PostPosted: Mon Dec 23, 2013 2:52 pm     Reply with quote

Another thing is that you may need to clear the interrupts after setting the edge. Generally, there is no guarantee what 'level' is in the edge latches, when the chip wakes up. When you then set which edge is to be used, this can result in the interrupt triggering when the interrupt is enabled.
If you read the Microchip application notes and data sheets, you will find warnings that this can happen....

Best Wishes
dsevgor



Joined: 30 Mar 2013
Posts: 11

View user's profile Send private message

PostPosted: Wed Dec 25, 2013 6:14 am     Reply with quote

It made no sense when I read your suggestions. This is probably because I'm a "C" noob =)
Nevertheless I've tried what you suggested and it solved my problem.

Thank you so much !! again : )
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group