Global IRQ function

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Jerremy09
Posts: 28
Joined: Wed May 08, 2019 7:40 am

Global IRQ function

Post by Jerremy09 » Wed Mar 11, 2020 4:36 pm

Hello All,

I would like to ask you for help with "Global Function".

What I need:
I need a program to react on impulse (machine.IN) from PWM (also generated by ESP32) to count impulse and when the count reaches a specific amount (like 100, 10, 32000 or different) then break while cycle in "Blink.py".

what I use:
Board: ESP32 (LED on pin 2)
Micropython Firmware:
VS Code:
Version: 1.43.0 (user setup)
Commit: 78a4c91400152c0f27ba4d363eb56d2835f9903a
Date: 2020-03-09T19:47:57.235Z
Electron: 7.1.11
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Windows_NT x64 10.0.18363

I prepared a simplified example of my design:
Designed structure:
Image

My codes:
Boot.py:

Code: Select all

   import machine
   import utime
   LED_PWM = machine.PWM(machine.Pin(2, machine.Pin.OUT))
   LED_PWM.deinit()
   LED_PWM.init()
   utime.sleep(0.1)
   Counter = 0
- meaning is to set PIN to be for generating PWM signal to LED (LED is set on PIN2 on my board)

Main.py

Code: Select all

   def Count(pin):
       global Counter
       Counter = Counter + 1
       return(Counter)

   import machine
   LED_Count = machine.Pin(26, machine.Pin.IN)
   LED_Count.irq(handler=Count, trigger=machine.Pin.IRQ_FALLING)

   import Blink
   Blink.Blink()
- Definition of LED_Count IRQ function (on PIN 26), this IRQ must be with the highest priority because I really need to count every impulse (impulses may be in interval 0 - 1023)
- meaning of this code is to Run external function "Blink" one-time-only (in real project call of "Blink.py" will be multiple times)

Blink.py

Code: Select all

   def Blink():   
       import machine
       import math
       import Wait_Function
       import LED_Start
       import LED_Stop

       Counter = 0
       LED_PWM = machine.PWM(machine.Pin(2, machine.Pin.OUT))
       while True:
           print(str(Counter))
           Wait_Function.Wait_Time()
           if Counter<10:
               LED_Start.LED_Run(5, LED_PWM)
           else:
               LED_Stop.LED_Stop(LED_PWM)
               break
- Meaning of this code is to:
1) Assign Value into Global variable "Counter"
2) Run while cycle till the "Counter" reach 10

LED_Start.py

Code: Select all

   def LED_Run(speed, LED_PWM):
       LED_PWM.deinit()
       LED_PWM.init()
       LED_PWM.freq(speed)
       LED_PWM.duty(512)
- Meaning of this code is to start Blink of LED on defined frequency

LED_Stop.py

Code: Select all

   def LED_Stop(LED_PWM):
       LED_PWM.deinit()
- Meaning of this code is to stop blinking of LED

Wait_Function.py

Code: Select all

   def Wait_Time():
       import utime

       Counter = 0
       while Counter < 5:
           utime.sleep(1)
           Counter = Counter + 1
- While loop is there because I have another code in while loop in real project, to simulate same functoin I use "utime.sleep(1)"

Result of my code:
- While look in "Blink.py" is running forever because "Counter =0" (not updating)
- nevertheless, if I add "print(str(Counter))" into Main.py "function Count" it prints increasing number with every impulse (1, 2, 3, 4, 5, ....)

Don't you know where is problem? what I do badly?

Thank you for any answer I appreciate

Jan Vaško

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Global IRQ function

Post by pythoncoder » Thu Mar 12, 2020 9:24 am

I've not trawled through all your code but what does stand out is that Counter in wait_Time is a local variable and is not the same as your global with the same name.

As a general point it's best not to put code in boot.py. Either put it in main.py, or (better) put everything into a user module and simply have main.py import that module. That way you can comment out the import in main.py and debug your module at the REPL.
Peter Hinch
Index to my micropython libraries.

Jerremy09
Posts: 28
Joined: Wed May 08, 2019 7:40 am

Re: Global IRQ function

Post by Jerremy09 » Thu Mar 12, 2020 3:24 pm

Hello,

I changed the variable in "Wait_Time.py" into "Counter_Inter". But still do not work. I also add "Print(some informaiton here)" into:
1) Count function - that one which is called by IRQ - "print("Loop: "+str(Counter))"
2) while cycle (Blink.py) - "print("While: "+str(Counter))"
3) Wait cycle (Wait_Function.py) - "print("Wait Time 4s")"

Results (prints on REPL):
While: 0
Wait Time 4s
Loop: 1
Loop: 2
Loop: 3
Loop: 4
Loop: 5
Loop: 6
Loop: 7
Loop: 8
Loop: 9
While: 0
Wait Time 4s
Loop: 10
Loop: 11
Loop: 12
Loop: 13
Loop: 14
Loop: 15
Loop: 16
Loop: 17
Loop: 18
Loop: 19
Loop: 20
While: 0
Wait Time 4s
Loop: 21
.....
- this continues till I unplug the device from electricity

Point 2: Why I have code in Boot.py
- not in this example, but in a real project, I have Stepp Motors and DRV8825 and I need as soon as possible to Dispable driver othervise motors starts "jumps".

Thank you

Post Reply