/** * Shift Register library * * Author: Jan Dvořák z Vozerovic * E-mail: dvorkaman@gmail.com * Web: dvorkaman.asp2.cz * Created: 2014 */ /* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef __dvorkaman_shiftRegister_base_c__ #define __dvorkaman_shiftRegister_base_c__ #include "libShiftRegister_Base.h" #include "libGPIO_base.h" #include "libSystemTime_base.h" /** * Initialize shift register - simple only */ void shiftRegister_initialize(libShiftRegister* shiftRegister, GPIO_TypeDef* gpio, uint16_t clockPin, uint16_t dataPin, uint16_t storagePin) { shiftRegister_initializeBits(shiftRegister, gpio, clockPin, dataPin, storagePin, SHIFT_REGISTER_STORAGE_BITS); } /** * Initialize shift register - BITS */ void shiftRegister_initializeBits(libShiftRegister* shiftRegister, GPIO_TypeDef* gpio, uint16_t clockPin, uint16_t dataPin, uint16_t storagePin, uint8_t bits) { shiftRegister_initializeFull(shiftRegister, gpio, clockPin, dataPin, storagePin, bits, SHIFT_REGISTER_CLOCK_PULSE, SHIFT_REGISTER_DATA_SETUP, SHIFT_REGISTER_STORAGE_PULSE, SHIFT_REGISTER_STORAGE_PULSE_SETUP); } /** * Initialize shift register - FULL */ void shiftRegister_initializeFull(libShiftRegister* shiftRegister, GPIO_TypeDef* gpio, uint16_t clockPin, uint16_t dataPin, uint16_t storagePin, uint8_t bits, uint8_t clockPulse, uint8_t dataSetup, uint8_t storagePulse, uint8_t storagePulseSetup) { if (clockPulse <= dataSetup) { return; } // not valid data // set pins gpio_initializeOutputPin(gpio, clockPin); gpio_initializeOutputPin(gpio, dataPin); gpio_initializeOutputPin(gpio, storagePin); // create structure shiftRegister->gpio = gpio; shiftRegister->clockPin = clockPin; shiftRegister->dataPin = dataPin; shiftRegister->storagePin = storagePin; shiftRegister->bits = bits; shiftRegister->clockPulse = clockPulse; // ns shiftRegister->dataSetup = dataSetup; // ns shiftRegister->storagePulse = storagePulse; // ns shiftRegister->storagePulseSetup = storagePulseSetup; // ns // clear values gpio_writeOutputPin(gpio, clockPin, Bit_RESET); gpio_writeOutputPin(gpio, storagePin, Bit_RESET); } /** * Write value to shift register */ void shiftRegister_write(libShiftRegister* shiftRegister, uint16_t value) { uint8_t i; BitAction b; uint16_t mask = 0x1 << (shiftRegister->bits - 1); // MSB first int16_t storageSetup = shiftRegister->storagePulseSetup - shiftRegister->clockPulse - shiftRegister->dataSetup; // Set data for (i = 0; i < shiftRegister->bits; i++) { b = (value & mask) > 0 ? Bit_SET : Bit_RESET; // true or false // Set Data gpio_writeOutputPin(shiftRegister->gpio, shiftRegister->dataPin, b); systemTime_delayNs(shiftRegister->dataSetup); // - clock is 0 -> raise gpio_writeOutputPin(shiftRegister->gpio, shiftRegister->clockPin, Bit_SET); // data hold is covered here systemTime_delayNs(shiftRegister->clockPulse); // - put down clock gpio_writeOutputPin(shiftRegister->gpio, shiftRegister->clockPin, Bit_RESET); systemTime_delayNs(shiftRegister->clockPulse - shiftRegister->dataSetup); // covered partially in setup value = value << 1; // shift left - make another MSB } // Store data if (storageSetup > 0) { systemTime_delayNs(storageSetup); } // wait before storage gpio_writeOutputPin(shiftRegister->gpio, shiftRegister->storagePin, Bit_SET); systemTime_delayNs(shiftRegister->storagePulse); gpio_writeOutputPin(shiftRegister->gpio, shiftRegister->storagePin, Bit_RESET); } #endif