Keywords: Interrupts, GPIO, ESP8266, MicroPython, uPyCraft
Prerequisites
- Complete set up of uPyCraftIDE
- Hardware – ESP8266
Dictionary meaning of Interrupt is to break some continuity.
If we relate this to micro controller terminology this means when an interrupt happens, the processor stops the execution of the main program to execute a task or process or thread, and then gets back to the main program.
Hardware interrupt
A hardware interrupt is an electronic alerting signal sent to the micro controller from an external device, like a button or an external peripheral. For example, when we press a key on the keyboard or move the mouse, they trigger hardware interrupts which cause the processor to read the keystroke or mouse position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
''' Author : Seetha Akella Purpose : Hardware interrupt - Glow an LED when button is pressed Language : MicroPython Hardware : ESP8266 ''' ''' A pin object is used to control I/O pins (also known as GPIO - general-purpose input/output). Pin objects are commonly associated with a physical pin that can drive an output voltage and read input voltages. The pin class has methods to set the mode of the pin (IN, OUT, etc) and methods to get and set the digital logic level. For analog control of a pin, see the ADC class. ''' from machine import Pin ''' The sleep() function suspends execution of the current thread for a given number of seconds. ''' from time import sleep # A global variable press = False led = Pin(2, Pin.OUT) button = Pin(14, Pin.IN) def handle_interrupt(pin): global press press = True global interrupt_pin interrupt_pin = pin button.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt) while True: if press: print('Press detected! Interrupt caused by:', interrupt_pin) led.value(1) press = False sleep(1) led.value(0) |
Software interrupt
A software interrupt is caused either by an exceptional condition or a special instruction in the instruction set which causes an interrupt when it is executed by the processor. For example, if there is a piece of work to be done for a very defined time then we can use a timer interrupt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
''' Author : Seetha Akella Purpose : Timer interrupt - Blink an LED when timer expires Language : MicroPython Hardware : ESP8266 ''' ''' A pin object is used to control I/O pins (also known as GPIO - general-purpose input/output). Pin objects are commonly associated with a physical pin that can drive an output voltage and read input voltages. The pin class has methods to set the mode of the pin (IN, OUT, etc) and methods to get and set the digital logic level. ''' from machine import Pin import machine ''' The sleep() function suspends execution of the current thread for a given number of seconds. ''' from time import sleep interruptOccured = False high = 1 timer = machine.Timer(0) led = Pin(2,Pin.OUT) def handleInterrupt(timer): global interruptOccured interruptOccured = True print("Interrupt has occurred: ") timer.init(period=1000, mode=machine.Timer.PERIODIC, callback=handleInterrupt) while True: if interruptOccured: led.value(high) if(high): high = 0 else: high = 1 interruptOccured = False |
Hope you understood how to configure and generate hardware and timer interrupts using MicroPython.
THANK YOU and many more programs coming your way!
Please put your queries in comment section, I will get back to you with answers ASAP.
Please like and share Techawarey. Find and Like Techawarey on Facebook.😄