def ISR(): ISRCount + 1 # // increment the overlflow counter if (ISRCount == servos[Channel].counter): # // are we on the final iteration for this channel{ TCNT2 = servos[Channel].remainder # // yes, set count for overflow after remainder ticks return TCNT2 elif (ISRCount > servos[Channel].counter): # // we have finished timing the channel so pulse it low and move on if True: bitbangio.SPI() # // check if activated Channel = digitalio.DigitalInOut(board.D12) Channel.value = False # // pulse this channel low if active Channel + 1 # // increment to the next channel ISRCount = 0 # // reset the isr iteration counter TCNT2 = 0 # // reset the clock counter register return if ((Channel != FRAME_SYNC_INDEX) and (Channel <= NBR_CHANNELS)): # // check if we need to pulse this channel # set correct servo later servo1 = pulseio.PWMOUT(board.D5, frequency=5000, duty_cycle=50) while True: # // check if activated Channel = digitalio.DigitalInOut(board.D12) Channel.value = True return elif Channel > NBR_CHANNELS: Channel = 0 # // all done so start over return
) ########################################## print(42 * '*') print("initialise digitalio pins for SPI") spi_clock = digitalio.DigitalInOut(board.SCK) spi_clock.direction = digitalio.Direction.OUTPUT spi_mosi = digitalio.DigitalInOut(board.MOSI) spi_mosi.direction = digitalio.Direction.OUTPUT spi_miso = digitalio.DigitalInOut(board.MISO) spi_miso.direction = digitalio.Direction.INPUT # print((42 * '*') + "\n" + "init busio.SPI") # spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) print("init bitbangio.SPI") spi = bitbangio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) # maximum frequency is currently hardcoded to 6MHz # https://github.com/adafruit/circuitpython/blob/master/ports/atmel-samd/common-hal/pulseio/PWMOut.c#L119 gsclk_freqency = (6000 * 1000) # 6MHz gsclk = pulseio.PWMOut( board.D9, duty_cycle=(2 ** 15), frequency=gsclk_freqency) print("gsclk.frequency: {:}MHz".format(gsclk.frequency / (1000*1000))) latch = digitalio.DigitalInOut(board.D7) latch.direction = digitalio.Direction.OUTPUT ########################################## print(42 * '*') print("define pixel array / init TLC5957") num_leds = 16
## 74hc595 testing ## https://learn.adafruit.com/circuitpython-basics-i2c-and-spi/spi-devices ## https://circuitpython.readthedocs.io/projects/busdevice/en/latest/api.html import board #hardware details of the specific board that you are using import busio import digitalio from board import * #from adafruit_bus_device.spi_device import SPIDevice import bitbangio import time # (SCK, MOSI, MISO) sck to 74hc595 pin 11 and mosi to 74hc595 pin 14 #with bitbangio.SPI(board.D52, board.D51, None) as spi_bus: with bitbangio.SPI(board.D3, board.D2, None) as spi_bus: #cs = digitalio.DigitalInOut(D5) # chip select == Latch_pin == 74hc595 pin 12 cs = digitalio.DigitalInOut( D4) # chip select == Latch_pin == 74hc595 pin 12 cs.direction = digitalio.Direction.OUTPUT cs.value = False bytes_read = bytearray(4) # read buffer bytes_write = bytearray(1) # one byte buffer for write data while True: for testdata in range(256): # test data is all bytes 0 to 255 while not spi_bus.try_lock(): # wait for bus available pass bytes_write[0] = testdata #put one byte in transmit buffer
duty_cycle = 0 servos = pulseio.PWMOUT(board.D5, frequency=5000, duty_cycle=0) FRAME_SYNC_INDEX = 0 NBR_CHANNELS = 8 servo_t = servos[NBR_CHANNELS + 1] DEFAULT_PULSE_WIDTH = duty_cycle # // frame sync delay is the first entry in the channel array FRAME_SYNC_PERIOD = 20000 # // total frame duration in microseconds FRAME_SYNC_DELAY = ((FRAME_SYNC_PERIOD - (NBR_CHANNELS * DEFAULT_PULSE_WIDTH)) / 128) # // number of iterations of the ISR to get the desired frame rate DELAY_ADJUST = time.sleep(.08) # // number of microseconds of calculation overhead to be subtracted # from pulse timings Channel = 0 SPI = bitbangio.SPI(21, MOSI=board.D11, MISO=board.D12) def ISR(): ISRCount + 1 # // increment the overlflow counter if (ISRCount == servos[Channel].counter): # // are we on the final iteration for this channel{ TCNT2 = servos[Channel].remainder # // yes, set count for overflow after remainder ticks return TCNT2 elif (ISRCount > servos[Channel].counter): # // we have finished timing the channel so pulse it low and move on if True: bitbangio.SPI()