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 CCS Technical Support

Store timer data in eeprom (hour,minutes)

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



Joined: 06 Nov 2010
Posts: 22
Location: montreal qc

View user's profile Send private message Send e-mail

Store timer data in eeprom (hour,minutes)
PostPosted: Sat Nov 06, 2010 3:28 am     Reply with quote

Hi everybody I'm a newbie. I have a countdown timer and I want to store elapsed time (hours,minutes) in eeprom of a 16f628a. The stored to be cumulative, mean every time you start timer, timer will add to data stored in eeprom. Somebody have idea to do it ? If you have code example would be great. Thanks in advance.
HGHK



Joined: 29 Jun 2010
Posts: 33

View user's profile Send private message Yahoo Messenger

Use addressmod for EEPROM writing
PostPosted: Sat Nov 06, 2010 4:44 am     Reply with quote

In ccs help you can find text like it.
Code:

void DataEE_Read(int32 addr, int8 * ram, int bytes) {
     int i;
     for(i=0;i<=bytes;i++,ram++,addr++)
     *ram=read_eeprom(addr);
     }
void DataEE_Write(int32 addr, int8 * ram, int bytes) {
     int i;
     for(i=0;i<=bytes;i++,ram++,addr++)
     write_eeprom(addr,*ram);
     }
addressmod (eepromdata,DataEE_read,DataEE_write,0x00,0xff);   

Try to make variable as eepromdata and enjoy it ! Wink
You can adjust EEPROM using Memory by manipulating 0x00 and 0xff in end line. Surprised Very Happy Laughing
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Sun Nov 07, 2010 3:47 am     Reply with quote

As a comment, what is going to trigger the writing?.

If this is being done at a regular interval, then you need to consider the 'write life' of the EEPROM. The 628, has a high figure for this at 1M cycles minimum, _but_ for example, if you wrote the a value every second, this would be used in just under 12 days. If you only write every minute, this shoots up to a couple of years. If instead you add circuitry to detect the power being switched off, and have a supply that remains running for a small fraction of a second after the unit is turned off, writing the EEPROM in this time, the 'life' changes to a million power-off cycles, and will never be seen.....

Best Wishes
bauche



Joined: 06 Nov 2010
Posts: 22
Location: montreal qc

View user's profile Send private message Send e-mail

reply
PostPosted: Sun Nov 07, 2010 7:22 am     Reply with quote

it's minutes interval..my timer works in second and minutes but i want to keep tracking about minutes of elapsed time...in ex: i program 3:00 (3 minutes) it's countdown 2:59...2:58...but every minutes the timer work ...it's add minutes elapsed in eeprom ans it's not resetable and if i can put minutes and hours elapsed in eeprom could be great...if someboby knows where i can find example...i want to do it..but understand how to do it...Thanks...
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Sun Nov 07, 2010 9:34 am     Reply with quote

HGHK, gives you a method to write/read _any_ variable to the EEPROM.

If (for instance), you have a 'clock' variable you make yourself with a structure:
Code:

struct {
   int8 minutes;
   int8 hours;
   int16 day_of_year;
   int16 year;
} clock;

//Then you can write this to the start of the EEPROM (address 0), with:

DataEE_Write(0, &clock, sizeof(clock));

//and read it with

DataEE_Read(0, &clock, sizeof(clock));


Obviously, change the address if you want to put it elsewhere in the EEPROM...

Is your project unlikely to go beyond a couple of years running duration?. If not, then re-consider the approach...

Best Wishes
bauche



Joined: 06 Nov 2010
Posts: 22
Location: montreal qc

View user's profile Send private message Send e-mail

PostPosted: Sun Nov 07, 2010 10:29 am     Reply with quote

i found a way to do it but now my prob..it's a coundown timer....and the value write in the eeprom seem to goes down too...
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sun Nov 07, 2010 1:17 pm     Reply with quote

This seems obvious so I might have misunderstood your issue. I assume you want an elapsed time meter often called an run time hour meter.
On programming set the eeprom values to say 0hrs 0 minutes. Use a 32 bit integer for hours and 8 bits for mins.. 5 bytes of eeprom
2 ^32 hours is several years
On power up read the eeprom and post to ram variables the 32bit integer hrs and 8 bit seconds.
Start your timer now after say 10 mins update the eeprom by adding the 10 min timer time to the variables in ram... it should work for 15 to 20 years ( 1 million times 10 mins). On power down update your eeprom if your elapsed time meter needs to be more accurate than the nearest 10 mins. Write this code yourself don't ask others to do it for you that way you'll learn a lot.

Impossible n'est pas francais
bauche



Joined: 06 Nov 2010
Posts: 22
Location: montreal qc

View user's profile Send private message Send e-mail

PostPosted: Sun Nov 07, 2010 2:41 pm     Reply with quote

hé... I don't want anybody to do it.... I'm only trying to find the way to do it... if I want somebody to do it...I give the code of my project and tell people to do it....did you see my code??? I don't think so.... I'm not a kind of guy that born and knows everything. I ASK and try to LEARN...if somebody when it's born knows everything...that's GOD.... I'm not really happy about your comments Douglas!!!!!... Mad When I found how to do it by myself.... for sure after I remember!!!!
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Sun Nov 07, 2010 4:36 pm     Reply with quote

Quote:
i found a way to do it but now my prob..it's a coundown timer....and the value write in the eeprom seem to goes down too...


You have a timer event of some sort that triggers the countdown of your elapsed time, right?
You need an additional set of variables for another elapsed time. This one counts up. This is the one you would use to store the 'run time'.

On power-up you read it from eprom, on power-down you update the eeprom value.

Again, the critical thing is to not 'wear out' the EEPROM. If you have a trigger that tells you when power is shut off, and enough time before power to the PIC is actually gone, then your okay. That is what everyone is trying to caution you about.

It sounds like you've got something started. If you get stuck, simplify the program as much as possible to try to figure out what's going wrong. If you still have trouble, post the simplified program here and we'll help you.
bauche



Joined: 06 Nov 2010
Posts: 22
Location: montreal qc

View user's profile Send private message Send e-mail

PostPosted: Sun Nov 07, 2010 4:42 pm     Reply with quote

EXACTLY....you got it!!!! now I'm put a variable in eeprom...I read this value and keep it... as my timer countdown...my variable goes up(add) and re-write in eeprom for the next cycle.....simple....in theory...!!! Very Happy
bauche



Joined: 06 Nov 2010
Posts: 22
Location: montreal qc

View user's profile Send private message Send e-mail

PostPosted: Sun Nov 07, 2010 5:20 pm     Reply with quote

Thats the code. I done a test to be sure it's ok. Thanks for help. I'm like a guy turning around the problem!!!!
Code:

#include <16F628A.h>

#FUSES NOWDT,INTRC,NOPUT,NOPROTECT,NOLVP

#use delay(clock=4000000)

#include "flex_LCD.c"

#define  START_STOP  (input(PIN_A3))   
#define  SET         (input(PIN_A4))
#define  CLEAR       (input(PIN_B0))   
#define  LED1        PIN_A6            //down-counter indicator
#define  LED2        PIN_A2            // run indicator

#define  INCREMENTS  30          //increment of time in seconds at SET TIME.

//prototype
void title(void);
int16 set_timer(void);


//global variables
int1  timer0_flag;
int   timer_counter = 0;
int1  _LED1 = 0;


#int_TIMER0
void  TIMER0_isr(void)
{
   //4ms interrupt
   timer_counter++;
   if (timer_counter >= 250)
   {
      timer_counter = 0;
      timer0_flag = TRUE;        //timer0 flag every 1 sec.
      _LED1 ^= 1;
      output_bit(LED1,_LED1);
   }
}



void main()
{
   int16 seconds_counter;
   int   minutes;
   int   seconds;
   
   lcd_init();
   
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);     //overflow 4000us
   set_timer0(8);                               //just to be exact
   
   loop:
   
   title();                         
   
   seconds_counter=set_timer() + 1; // sets countdown timer
   while(!START_STOP);              // wait for START_STOP pin to return HIGH
   
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);

   while(TRUE)
   {
     
      if(timer0_flag)
      {
         timer0_flag = 0;

         
         output_high(LED2);
         
         seconds_counter--;

         minutes = seconds_counter/60;
         seconds = seconds_counter%60;
         lcd_gotoxy(8,1);
         printf(lcd_putc,"%02u:%02u",minutes,seconds);
         lcd_gotoxy(1,2);
         printf(lcd_putc,"           ");

         
         if(seconds_counter <= 0)
         {
           

            output_low(LED2);
            disable_interrupts(INT_TIMER0);
            disable_interrupts(GLOBAL);
            lcd_putc('\f');
            lcd_gotoxy(1,1);
            printf(lcd_putc,"    Finished!");
            lcd_putc('\f');

       goto loop;

         }
      }
     
      if (!START_STOP)
      {
         while(!START_STOP);
         output_low(LED2);
         output_low(LED1);
         disable_interrupts(INT_TIMER0);
         disable_interrupts(GLOBAL);
         lcd_putc('\f');
         lcd_gotoxy(1,1);
         printf(lcd_putc,"   Stopped!");
         lcd_putc('\f');

      goto loop;

      }
     
     
   }

}

void title(void)
{
   //Start up Screen for 1 Sec
   lcd_gotoxy(1,1);
   printf(lcd_putc," <Timer>");
   lcd_gotoxy(1,2);
   printf(lcd_putc,"Test Timer");
   delay_ms(1000);
 
}

int16 set_timer(void)
{
   int16 set_counter = 0;
   int min,sec;
   set_counter = 0;
   printf(lcd_putc,"\f");
   lcd_gotoxy(1,1);
   printf(lcd_putc,"Time: 00:00");
   lcd_gotoxy(1,2);
   printf(lcd_putc,"Add Time: ");
   
   
   do{
     
      if(!SET)
      {
         while(!SET);
         set_counter += INCREMENTS;         
      }
     
      if(!CLEAR)
      {
         while(!CLEAR);
         set_counter = 0;
      }
     
      min = set_counter/60;
      sec = set_counter%60;

      lcd_gotoxy(12,2);
      printf(lcd_putc,"%02u:",min);
      lcd_gotoxy(15,2);
      printf(lcd_putc,"%02u",sec);
      delay_ms(100);
   }while(START_STOP);
   
   return set_counter;
   
}
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