Push button C program required

Discussion in 'Programmer's Corner' started by spursrn01, Jul 22, 2006.

  1. spursrn01

    Thread Starter New Member

    Jul 22, 2006
    2
    0
    It is required to connect 4 push buttons to a 89c668 microcontroller.

    When a key is pressed an interrupt is generated to read the key pressed.

    The program should include

    1. a 'debounce function' for mechanical bounce of the switches.

    2. An interrupt service routine for the function

    'void EX0_readKeyP(void)interrupt 0 {...}'

    3. A 'main function' to set up the interrupt and place the key read into port 2 of the microcontroller.

    Please help.

    Many Thanks.
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Hi,

    Where are you stuck? What things have you tried with confusing results? What program functions do you not understand?
     
  3. spursrn01

    Thread Starter New Member

    Jul 22, 2006
    2
    0
    All of it to be honest. My C isn't that good, this question came up in an exam. We were supposed to do it as part of a project but i didn't work on the C part of the project and haven't got any lecture material to hand. Any help would be greatly appreciated.

    Cheers, steve.
     
  4. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Hi,

    Don't feel bad - my C is virtually nonexistant. I prefer assembler and Powrbasic (the Quickbasic editor is still the best ever).

    All mechanical contacts bounce and make false closures. I would prefer to do the debounce in hardware, but if you sample the inputs over time - say a millisecond - you can safely decide that the button has been pressed.

    Interrupts are easy. Just remember to disable other interrupts while handling one, or you may never exit the handler. When you've read in the button press, enable interrupts as you exit the routine.

    Transferring a bit from one port to another should be pretty easy.
     
  5. Arm_n_Legs

    Active Member

    Mar 7, 2007
    183
    10
    How i would handle mechanical debounce using software.

    Assuming P1.0 is your active low input port connected to the switch. The first low detection will activate the function to respond to the key press and at the same time start a debounce timer. Before debounce time out, the next low spike at P1.0 will be ignored.

    You can put the debouce count down in the main or implement a timer interrupt routine.


    unsigned char debounced=0; // Global variable

    void main(){
    for(;;){
    if (debounced!=0) debounced--;
    if ((P1.0==0)&&(debounced==0)){
    debounced=10;
    // perform function with respect to key press.
    }
    }
    }
     
Loading...