Issue with read_digital() + write_digital()

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Issue with read_digital() + write_digital()

Post by mcauser » Thu Jan 25, 2018 12:20 pm

I was trying to output a pretty simple high/low clock signal and noticed an issue when calling read_digital() before write_digital().

Using firmware: MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit with nRF51822

Each of these, when observed on a logic analyser, produced an incorrect pattern. The read_digital() shortly before the write_digital() seemed to affect it.

Code: Select all

from microbit import *
clk=pin2

for i in range(16):
    clk.write_digital(not clk.read_digital())

for i in range(16):
    state = not clk.read_digital()
    clk.write_digital(state)

for i in range(16):
    state = clk.read_digital()
    clk.write_digital(not state)
Image

This one works as expected. I moved the read_digital() above and simply toggled the state boolean on each iteration.

Code: Select all

from microbit import *
clk=pin2

state = clk.read_digital()
for i in range(16):
    state = not state
    clk.write_digital(state)
Image

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: Issue with read_digital() + write_digital()

Post by cefn » Fri Jan 26, 2018 9:45 pm

Wondering if there is a relationship with the default pull behaviour coupled to digital read calls of the runtime as per...
https://github.com/lancaster-university ... issues/157

In particular if some part of the code temporarily sets a pull as the code passes through a digitalRead call then some later code unsets it, might that create the trace you have shared?

Post Reply