Pin Interrupts
The Arduino framework’s attachInterrupt() function is more convenient than the low-level mechanisms provided by avr-libc. Unfortunately, for some microcontroller boards, including the Arduino Uno and Nano, it is limited to working with only some pins.
The CowPi library addresses this by providing functions to register and deregister interrupt service routines for any digital input pin. No debouncing is provided, however, the pin_interrupts example demonstrates a macro
-
void cowpi_register_pin_ISR(uint32_t interrupt_mask, void (*isr)(void))
Registers a function to service pin-based interrupts triggered by logic-level changes on one or more pins.
The registered function will be invoked whenever there is a low-to-high or a high-to-low change. If behavior is only required for a rising edge or for a falling edge, or if the behavior for rising and falling edges must differ, then the function should have a conditional to determine the direction of the change.
If the change is generated by a mechanical device, then the function is responsible for debouncing if there is not a hardware debouncing circuit.
The
interrupt_maskargument is used to specify which pins will be serviced by the registered function. Bit0 corresponds to Pin 0, Bit1 corresponds to Pin 1, and so on. A 1 in a particular bit indicates that the function is to be registered for changes on the corresponding pin. If more than one bit has a 1, then the function will be registered for each of the corresponding pins. If there previously was a function registered to handle changes on a specified pin, then the new function will replace the old function. A bit with a 0 signifies nothing more than that the function is not being registered to service changes on that pin at this time.- Parameters:
interrupt_mask – A bit vector specifying which pins will be serviced by the registered ISR
isr – The function that will service interrupts triggered by changes on the specified pins
-
void cowpi_deregister_pin_ISR(uint32_t interrupt_mask)
De-registers the servicing function, if any, for the specified pin(s).
After this function terminates, a logic-level change on the specified pin(s) will no longer be serviced custom function. It is possible that an interrupt would still fire for changes on the pin; if so, it will be serviced by a default, empty function.
The
interrupt_maskargument is used to specify which pins will no longer be serviced by a custom function. Bit0 corresponds to Pin 0, Bit1 corresponds to Pin 1, and so on. A 1 in a particular bit indicates that the pin’s ISR is to be deregistered. If more than one bit has a 1, then the ISRs for each of the pins will be deregistered. A bit with a 0 signifies nothing more than that no deregistration actions are to be take for that pin at this time.- Parameters:
interrupt_mask – A bit vector specifying which pins will no longer be serviced by an ISR