PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 28, 2005 5:28 pm |
|
|
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);
}
} |
|
|