c - Why doesn't this function properly toggle an LED on and off? -


i using atmel sam3x8e micro-controller , trying simple led toggle when press button. using pull-up configuration button trigger interrupt routine.

this initialization interrupt:

// set button pins pull-up inputs  pio_set_input(pioc, button_1, pio_pullup); pio_set_input(pioc, button_2, pio_pullup);   // configure button input pin interrupt mode , handler (rising edge) pio_handler_set(pioc, id_pioc, button_1,  pio_it_rise_edge, button_press_handler); pio_handler_set(pioc, id_pioc, button_2,  pio_it_rise_edge, button_press_handler);  // enable interrupts pio_enable_interrupt(pioc, button_1);  pio_enable_interrupt(pioc, button_2);  nvic_enableirq(pioc_irqn);  nvic_enableirq(pioc_irqn);  

then interrupt routine:

// interrupt handler button press void button_press_handler(uint32_t a, uint32_t b) {    pio_toggle_pin_group(pioc, blue_led4); // not toggling led (only turns on) } 

yet when run it, cannot led toggle. turns on , stays on. function pio_toggle_pin_group calls following:

 * \param p_pio pointer pio instance.  * \param ul_mask bitmask of 1 or more pin(s) configure.  */ void pio_toggle_pin_group(pio *p_pio, uint32_t ul_mask) {     if (p_pio->pio_odsr & ul_mask) {         /* value driven on i/o line: 0. */         p_pio->pio_codr = ul_mask;     } else {         /* value driven on i/o line: 1. */         p_pio->pio_sodr = ul_mask;     } } 

any ideas why led not toggling way want? i've refereed atmel asf documentation still cannot figure out.

i cannot actual function calls, suppose use edge interrupt. far see, call interrupt handler each rising edge. however, after first rising edge, need trigger on button release, falling edge, need change edge within interrupt handler.

but must take account mechanical buttons not generate clean, single edge when pressed or released. instead bounce. normal momentary contact buttons pullup (or down) resistor, results in multiple pulses each event, led may turn on/off multiple times , stay in arbitrary state might - chance - "on" far times. if available, check oscilloscope.

this can circumvented in hardware capacitor or in software using timer dead time after relevant edge before reacting other button event. dead time depends on type of button, typical values 5 20ms , should mentioned in datasheet of button. if in doubt, use highest acceptable value.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -