Page 1 of 1
Simple SPI example, please
Posted: Fri Mar 19, 2021 11:47 pm
by MicroGuy
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
Re: Simple SPI example, please
Posted: Sat Mar 20, 2021 9:02 am
by OlivierLenoir
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()
Re: Simple SPI example, please
Posted: Sat Mar 20, 2021 9:35 am
by OlivierLenoir
Full SN74HCS264 project is available
here.
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()
Re: Simple SPI example, please
Posted: Sat Mar 20, 2021 12:35 pm
by OlivierLenoir
Pulse view.
- Screenshot at 2021-03-20 13-25-18.png (32.72 KiB) Viewed 2634 times
Re: Simple SPI example, please
Posted: Sun Mar 21, 2021 12:21 am
by MicroGuy
Thank you. I greatly appreciate your help! --Jon