Please teach me how to modify micropython code #11776
Unanswered
carl336
asked this question in
RP2040 / Pico
Replies: 2 comments
-
You should first draw a state diagram that documents your desired response to the different keystrokes. This will guide your code. You will need to implement a simple state machine that provides the behavior in your state diagram. The SPI output of steps 1, 2, 3, and 5 could be mapped directly into states, and the keypresses mapped into transitions between those states. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Please also see https://github.com/orgs/micropython/discussions/9111 about how to format code in posts. State machines are definitely the way to go here. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I would like to modify the MicroPython code, but I have no idea how to modify it so that it runs like this.
Vdd(1)
time.sleep(0.1)
cs(0)
spi_write_data_1()
spi_write_data_4()
spi_write_data_5()
spi_write_data_3()
spi_write_data_2()
cs(1)
time.sleep(0.008)
cs(0)
spi_write_data_6()
cs(1)
cs(0)
spi_write_data_7()
cs(1)
cs(0)
spi_write_data_1()
spi_write_data_2()
spi_write_data_8()
spi_write_data_4()
spi_write_data_5()
cs(1)
Vdd.value(0)
time.sleep(0.1)
I will upload the waveform diagram analyzed by Logic Analyzer. Please refer to it.
As shown in the picture,
Here are the codes that I wrote below:
from machine import Pin, SPI
import time
KEY1 = Pin(0, Pin.IN, Pin.PULL_UP)
KEY2 = Pin(15, Pin.IN, Pin.PULL_UP)
Vdd = Pin(2, mode=Pin.OUT, value=0)
spi = SPI(0, baudrate=7500000, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=Pin(6), mosi=Pin(7))
cs = Pin(1, mode=Pin.OUT, value=0)
cs(1)
Vdd(0)
state = False
def spi_write_data_1():
spi.write(bytes([0x01]))
def spi_write_data_2():
spi.write(bytes([0x02]))
def spi_write_data_3():
spi.write(bytes([0x03]))
def spi_write_data_4():
spi.write(bytes([0x04]))
def spi_write_data_5():
spi.write(bytes([0x05]))
def spi_write_data_6():
spi.write(bytes([0x06]))
def spi_write_data_7():
spi.write(bytes([0x07]))
def spi_write_data_8():
spi.write(bytes([0x08]))
def reverseGPIO():
if Vdd.value() == 1:
cs(0)
spi_write_data_1()
spi_write_data_2()
spi_write_data_8()
spi_write_data_4()
spi_write_data_5()
cs(1)
time.sleep(0.008)
try:
while True:
if KEY1.value() == 0:
time.sleep_ms(10)
if KEY1.value() == 0:
reverseGPIO()
except:
pass
Beta Was this translation helpful? Give feedback.
All reactions