Simple SPI example, please

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
MicroGuy
Posts: 17
Joined: Sun Jan 17, 2021 8:31 pm

Simple SPI example, please

Post by MicroGuy » Fri Mar 19, 2021 11:47 pm

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

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Simple SPI example, please

Post by OlivierLenoir » Sat Mar 20, 2021 9:02 am

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()

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Simple SPI example, please

Post by OlivierLenoir » Sat Mar 20, 2021 9:35 am

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()
Last edited by OlivierLenoir on Sun Jul 11, 2021 4:20 pm, edited 2 times in total.

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Simple SPI example, please

Post by OlivierLenoir » Sat Mar 20, 2021 12:35 pm

Pulse view.
Screenshot at 2021-03-20 13-25-18.png
Screenshot at 2021-03-20 13-25-18.png (32.72 KiB) Viewed 2497 times

MicroGuy
Posts: 17
Joined: Sun Jan 17, 2021 8:31 pm

Re: Simple SPI example, please

Post by MicroGuy » Sun Mar 21, 2021 12:21 am

Thank you. I greatly appreciate your help! --Jon

Post Reply