[Microbit-Python] Pulsing I/O pins

Damien George damien.p.george at gmail.com
Sat Jun 11 09:07:32 EDT 2016


If your aim is to make short pulses on a pin, try using the SPI
peripheral and writing a bit pattern that has a pulse.

On Fri, Jun 10, 2016 at 1:06 PM, Nigel Kendrick
<nigel.kendrick at gmail.com> wrote:
> Agreed on both counts (timing and bytecode) - just keeping things simple!
>
> On 10 June 2016 at 12:47, Radomir Dopieralski <microbit at sheep.art.pl> wrote:
>>
>> I'm not sure this is a good way to time things. It may change
>> arbitrarily with updates or even depending on the version of compiler
>> you use to compile the micropython code...
>>
>> Comments will not have any effect, because Micropython is not, in fact,
>> an "interpreted" language in the sense that BASIC was. It is compiled
>> into bytecode, that is then executed on a virtual machine. Comments get
>> removed in the compilation step.
>>
>> On Fri, 10 Jun 2016 12:37:36 +0100
>> Nigel Kendrick <nigel.kendrick at gmail.com> wrote:
>>
>> > Hi folks,
>> >
>> > I had to take some timing measurements so I thought I'd share just in
>> > case it's of use:
>> >
>> > For those without access to a 'scope (or who can't be bothered to
>> > drag it out!), if you toggle an I/O pin by writing something like
>> > this in the Block Editor:
>> >
>> > digital write 0 to pin P2
>> > digital write 1 to pin P2
>> > digital write 0 to pin P2
>> >
>> > You get a pulse with a 5uS duration.
>> >
>> > Adding extra 'high' stages adds 5uS per stage - example:
>> >
>> > digital write 0 to pin P2
>> > digital write 1 to pin P2
>> > digital write 1 to pin P2
>> > digital write 0 to pin P2
>> >
>> > Gives a 10uS pulse and so on.
>> >
>> > Repeating the same experiment in micropython, things are slower
>> > because the language is interpreted:
>> >
>> > from microbit import *
>> >
>> > pin2.write_digital(0)
>> >
>> > while True:
>> >     pin2.write_digital(1)
>> >     pin2.write_digital(0)
>> >     sleep(1)
>> > # End of while loop
>>
>> https://bitbucket.org/thesheep/micropython-ili9341/src/4fe3cd28006e171eec16aff9e7cbae8ed394f88d/ili9341.py?fileviewer=file-view-default#ili9341.py-142:157>
>> > 1 x pin2.write_digital(1) = 68uS
>> >
>> > 2 x pin2.write_digital(1) = 136uS
>> >
>> > etc.
>> >
>> > You could stretch the timing using loops - and above 1mS you could
>> > pause/sleep - but just repeating the pin writes seems more controlled
>> > and easier to work out.
>> >
>> > I tried padding the highs with comments - eg:
>> >
>> > while True:
>> >     pin2.write_digital(1)
>> >     #
>> >     #
>> >     pin2.write_digital(0)
>> >     sleep(1)
>> > # End of while loop
>> >
>> > but the difference, if anything, was negligible.
>> >
>> > -- Nigel
>>
>>
>>
>> --
>> Radomir Dopieralski
>>
>> --
>> Radomir Dopieralski
>> _______________________________________________
>> Microbit mailing list
>> Microbit at python.org
>> https://mail.python.org/mailman/listinfo/microbit
>
>
>
> _______________________________________________
> Microbit mailing list
> Microbit at python.org
> https://mail.python.org/mailman/listinfo/microbit
>


More information about the Microbit mailing list