Intro: It has been a long time since I have worked with microcontrollers, and I was looking at getting back into it. So, naturally, I chose a project that is way beyond my skill level and started at it. I decided to break my project into parts and write them out into individual programs, that way I could test them individually and put it all together when it was working properly.
Problem: I'm currently on the input side of the project, which is the simplest part, basically just two switches which need to be debounced in software to ensure no erratic behavior. The test program uses an array of values that will change the output port of the micro to indicate the successful debouncing of the switch by energizing an LED on that output.
Initially, I wrote a for loop with a specific time delay to handle debouncing a single switch, and I wrote an interrupt to flash an LED to ensure the loop was not locking up my program. My initial code worked in debouncing the switch, but locked up my program. I changed it to a while loop, and included a boolean function to prevent multiple state changes from a single button press, but this has produced erratic behavior from my circuit. For example, the ISR LED will not begin flashing until I have pressed the switch, and then it will stop flashing after a few switch presses.
I have also attached the files in a .zip file for examination. Does anyone have any suggestions as to what I can do to fix the issues I am seeing.
Problem: I'm currently on the input side of the project, which is the simplest part, basically just two switches which need to be debounced in software to ensure no erratic behavior. The test program uses an array of values that will change the output port of the micro to indicate the successful debouncing of the switch by energizing an LED on that output.
Initially, I wrote a for loop with a specific time delay to handle debouncing a single switch, and I wrote an interrupt to flash an LED to ensure the loop was not locking up my program. My initial code worked in debouncing the switch, but locked up my program. I changed it to a while loop, and included a boolean function to prevent multiple state changes from a single button press, but this has produced erratic behavior from my circuit. For example, the ISR LED will not begin flashing until I have pressed the switch, and then it will stop flashing after a few switch presses.
Rich (BB code):
/******************************************************************************/
/* User Global Variable Declaration */
/******************************************************************************/
unsigned char LED_Number = 0; // 8-bit variable
const unsigned char LED_LookupTable[3] = {0x01, 0x08, 0x80}; // LED array
bool switchState = false;
unsigned char debounceDelay = 20;
/******************************************************************************/
/* Main Program */
/******************************************************************************/
void main(void)
{
/* Configure the oscillator for the device */
ConfigureOscillator();
/* Initialize I/O and Peripherals for application */
InitApp();
while(1)
{
// use lookup table to energize one LED based on LED_Number value
PORTC = LED_LookupTable[LED_Number];
if (RC6 == 0 && switchState == false) // button press loop
{
uint8_t db_cnt = 0;
LED_Number++; // rotate display by 1
if (LED_Number >= 3)
{
LED_Number = 0; // go back to LED 0.
}
while(db_cnt<=debounceDelay) // button debounce loop
{
wait_ms(1);
db_cnt++;
if (RC6 == 1) // if button press
{
db_cnt = 0;
}
}
switchState = true;
}
if (RC6 == 1)
{
switchState = false;
}
}
}
Attachments
-
3.6 KB Views: 26