33
loading...
This website collects cookies to deliver better user experience
from rp2 import PIO, StateMachine, asm_pio
from machine import Pin
import time
@asm_pio(set_init=PIO.OUT_LOW)
def pio_program():
set(pins, 1) [31]
nop() [31]
set(pins, 0) [31]
state_machine = StateMachine(0, pio_program, freq = 2000, set_base=Pin(25))
state_machine.active(1)
import
statements define the required objects of the program@asm_pio
identifies the very next method as a PIO program. The decorator accepts all the arguments with which to customize the state machine, like initial direction of pins, the shift direction of the ISR/OSR, and auto or threshold values for the push and pull methods2000 Hz = 500us
, 5000 Hz = 200us
, and 10000 Hz = 100us
. However, the frequency defines how fast the sum of all instructions inside the program runs. Therefore, we first calculate the cycle of all instructions as cycle = target_frequency * (number of statements + wait cycles)
, and the state machines frequency as state_machine_frequency = cycle * * (number of statements + wait cycles)
2000Hz / 20 = 100Hz = 10ms = 10000us
- too fast sending the 40us signal. And the other way around 25000 / 20 = 25000 Hz = 0.04ms = 40us
- this would force use to wait for 500 instructions.1us * (16+31)= 21277Hz
, and the total frequency is 21277Hz * (16 + 31) = 1000019Hz
.dht_reader = StateMachine(1,dht11_reader, freq = 1000019)
dht_pin = Pin(15, Pin.OUT, Pin.PULL_DOWN)
dht_pin.value(1)
utime.sleep_ms(250)
dht_pin.value(0)
utime.sleep_ms(20)
dht_pin = Pin(15, Pin.IN, Pin.PULL_UP)
dht_reader = StateMachine(1,dht11_reader, freq = 1000019, in_base=dht_pin, jmp_pin=dht_pin)
read_bit
loop, which will again wait for the pin to go low0b1
, otherwise its 0b0
high_detected
and low_detected
code block to write 3-bit data to the ISR@asm_pio(autopush=True, push_thresh=24)
def dht11_reader():
# ínitial LOW-HIGH confirmation pulse
wait(0,pin,0)
wait(1,pin,0)
# Main loop
# General idea: if pin changes from high to low within 32 cycles, its logic 1, otherwise logic 0
label("read_bit")
wait(1,pin,0) [31]
jmp(pin, "high_detected")
jmp("low_detected")
# Write 0b111 = (7), return to pattern loop
label("high_detected")
set(y,0b111)
in_(y,3)
wait(0,pin,0)
jmp("read_bit")
# Write 0b100 =(4), return to pattern loop
label("low_detected")
set(y,0b100)
in_(y,3)
wait(0,pin,0)
jmp("read_bit")
print("Starting DHT11 Program ...")
i = 0
while True:
sleep(2.0)
i +=1
print("Cycle {} - Running state machine ...".format(i))
dht_pin = Pin(15, Pin.OUT, Pin.PULL_DOWN)
dht_pin.value(1)
utime.sleep_ms(250)
dht_pin.value(0)
utime.sleep_ms(30)
dht_pin = Pin(15, Pin.IN, Pin.PULL_UP)
dht_reader = StateMachine(1,dht11_reader, freq = 1000019, in_base=dht_pin, jmp_pin=dht_pin)
dht_reader.active(1)
data = []
for _ in range(5):
data.append(bin(dht_reader.get()))
dht_reader.active(0)
sleep(0.5)
for d in data:
print(d)
Starting DHT11 Program ...
Cycle 1 - Running state machine ...
0b100111111100100100111111
0b111111100111111111111100
0b111111100100100111100100
0b111111111100100111100100
0b100100100100111100100111
Temperature: 24.2 Degree Celcius
Humidity: 48.1 %
Temperature: 24.3 Degree Celcius
Humidity: 48.2 %