I would like a simple MicroPython program example that shows how to use the SPI port on a Micro:Bit board with the https://python.microbit.org/v/2 tools. I want to demonstrate to middle-school kids in a MicroPython class how they can get parallel output of a single byte on a shift register (SN74HCS264) via a clocked-serial output (SPI) from a MicroBit board. It takes only three wires, which the kids find fascinating. (I can "bit bang," but using an SPI function seems better and simpler, I think.)
All examples found so far use a byte buffer with the values already in them. For example, preset bit patterns to control a 7-segment display, or hex bytes for something else. In our case, we will create 8-bit values in software and transmit them. Any help greatly appreciated. Thanks. --Jon
Simple SPI example, please
- OlivierLenoir
- Posts: 126
- Joined: Fri Dec 13, 2019 7:10 pm
- Location: Picardie, FR
Re: Simple SPI example, please
It a draft and under development version a driver for SN74HCS264.
Code: Select all
# Author: Olivier Lenoir - olivier.len02@gmail.com
# Created: 2021-03-20 10:00:08
# License: MIT, Copyright (c) 2021 Olivier Lenoir
# Language: MicroPython v1.14
# Project: SN74HCS264
# Description: MicroPython Driver for SN74HCS264
from machine import Pin
class SN74HCS264(object):
"""Driver for SN74HCS264 8-Bit Parallel-Out Serial Shift Registers
With Schmitt-Trigger Inputs and Inverted Outputs"""
def __init__(self, a_pin, b_pin, clk_pin, clr_pin):
self.a = a_pin
self.b = b_pin
self.clk = clk_pin
self.clr = clr_pin
self.reset()
def write(self, data):
self.b(1)
for offset in range(8):
stat = 0 if data & (1 << offset) else 1
self.a(stat)
self._clk()
self.b(0)
def clear(self):
self.clr(0)
self.clr(1)
def _clk(self):
self.clk(0)
self.clk(1)
def reset(self):
self.a(1)
self.b(0)
self.clk(1)
self.clear()
Olivier Lenoir
https://gitlab.com/olivierlenoir
https://gitlab.com/olivierlenoir
- OlivierLenoir
- Posts: 126
- Joined: Fri Dec 13, 2019 7:10 pm
- Location: Picardie, FR
Re: Simple SPI example, please
Full SN74HCS264 project is available here.
Here a test.
Here a test.
Code: Select all
from sn74hcs264 import SN74HCS264
from machine import Pin
a = Pin(2, Pin.OUT)
b = Pin(4, Pin.OUT)
clk = Pin(16, Pin.OUT)
clr = Pin(17, Pin.OUT)
register = SN74HCS264(a, b, clk, clr)
register.write(37)
register.clear()
Last edited by OlivierLenoir on Sun Jul 11, 2021 4:20 pm, edited 2 times in total.
Olivier Lenoir
https://gitlab.com/olivierlenoir
https://gitlab.com/olivierlenoir
- OlivierLenoir
- Posts: 126
- Joined: Fri Dec 13, 2019 7:10 pm
- Location: Picardie, FR
Re: Simple SPI example, please
Thank you. I greatly appreciate your help! --Jon