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

write / read eeprom -- integers
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 28, 2005 5:28 pm     Reply with quote

This program will display the number of times a button has been
pushed. It writes the count to eeprom, and if the PIC is powered-down,
and then powered back up, it will restore the count value to the
previously saved value. I think this is what you want to do,
but I really don't know. Anyway, this should give you some ideas.
Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
//#use rs232(baud = 9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <lcd.c>

#ROM 0x2100 = {0x00}     // Initialize EEPROM address 0 = 0x00.

#define BUTTON_PIN  PIN_B0   // This pin must have a pull-up.
#define DEBOUNCE_PERIOD_IN_MS  10
#define DEBOUNCE_COUNT  2

void wait_for_keypress(void);

//====================================
void main()
{
int8 count;

port_b_pullups(TRUE);

lcd_init();

// Read count that was stored in EEPROM and display it.
count = read_eeprom(0);
lcd_gotoxy(1,1);
printf(lcd_putc, "%u  ", count);


while(1)
  {
   wait_for_keypress();
   count++;
   write_eeprom(0, count);

   lcd_gotoxy(1,1);
   printf(lcd_putc, "%u  ", count);
  }

}
//==================================

void wait_for_keypress(void)
{
char count;

// First, wait for the button to be released.   With the debounce
// values as given above, the button must be in the "up" state
// for two consecutive readings, spaced 10 ms apart.

count = 0;

while(1)
  {
   if(input(BUTTON_PIN) == 1)
      count++;
   else
      count = 0;
   
   if(count == DEBOUNCE_COUNT)
      break;

   delay_ms(DEBOUNCE_PERIOD_IN_MS);
  }

// Now that the button is up, wait until the user presses it.
// In order for the keypress to be considered valid, based
// on the debounce values listed at the beginning of the
// program, the button must be held down for two consecutive
// readings, spaced 10 ms apart.

count = 0;

while(1)
  {
   if(input(BUTTON_PIN) == 0)
      count++;
   else
      count = 0;
   
   if(count == DEBOUNCE_COUNT)
      break;

   delay_ms(DEBOUNCE_PERIOD_IN_MS);
  }

}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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