Ok I tried your version
Thonny print out is way too late against the statemachine fifo.
Even if you stall with the autopush IN command the data store are late by 200ms per fifo data(your delay in the get).
So the next get will output data from the fifo which is a passed value.
I.M.O. You will have a lot of overrun using the print. But if it is only one information at a time just push once.
I use my cycle pulse statemachine to check your code and it is very accurate. I need to figure out your pulse but it is always the same +/- 1
pulse.set(1) => pulse of 1us give 62 63 63 after 2 minutes.
pulse.set(1000) => pulse of 1ms gives 62500 62500 62499 after 2 minutes.
Obviously your counter is working fine.
Maybe it's the timer. I will need to verify it.
This is my code to generate pulse [1us LOW][ n X 1us HIGH]
The PIN 13 is the output and by default is statemachine 1. This way you could connect Pin 13 to pin 16
Code: Select all
'''
*
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Daniel Perron
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
'''
import utime
import rp2
from rp2 import PIO, asm_pio
from machine import Pin
#
# A B
# ___
# _/ \
#
# A = 1 cycle pulse always 1us
# B = pulse length in us
@asm_pio(set_init=(PIO.OUT_HIGH),autopush=True, push_thresh=8)
def PULSE_PIO():
pull() # 0 get pulse length
mov(y,osr) # 1 store pulse length into y
jmp(y_dec,'decY')
label('decY')
mov(x,y)
set(pindirs,1) [1]
label('pulseLoop')
set(pins,1) [18]
jmp(x_dec,'pulseLoop')
set(pins,0) [17]
mov(x,y)
jmp('pulseLoop')
class cyclePulse:
def __init__(self,outputPin,smID=1):
self.outputPin = outputPin
self.smID = smID
self.outputPin.init(Pin.OUT)
self.outputPin.value(0)
self.sm= rp2.StateMachine(smID)
def set(self, pulseLength):
#start state machine
self.sm.init(PULSE_PIO,freq=20000000,
set_base=self.outputPin)
self.sm.put(pulseLength)
self.sm.active(1)
def stop(self):
self.sm.active(0)
self.outputPin.value(0)
if __name__ == "__main__":
from machine import Pin
from cyclePulse import cyclePulse
pulse = cyclePulse(Pin(13,Pin.OUT))
while True:
for i in range(1,1000):
pulse.set(i)
utime.sleep_ms(500)
Code: Select all
from machine import Pin
from cyclePulse import cyclePulse
pulse=cyclePulse(Pin(13,Pin.OUT))
pulse.set(1000)