![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Board Basis für Auftrag
Revision 2:78dd1100a67b, committed 2021-09-10
- Comitter:
- rogerzuber
- Date:
- Fri Sep 10 08:19:02 2021 +0000
- Parent:
- 1:7da2cb94da6c
- Commit message:
- Board Beispiel Zuber
Changed in this revision
diff -r 7da2cb94da6c -r 78dd1100a67b ShiftOut.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ShiftOut.cpp Fri Sep 10 08:19:02 2021 +0000 @@ -0,0 +1,127 @@ +#include "mbed.h" +#include "ShiftOut.h" + + +#define SET_LATCH() (LATCH = 0) +#define RESET_LATCH() (LATCH = 1) + +#define ENABLE_RESET() (RESET = 0) +#define DISABLE_RESET() (RESET = 1) + +static char stateArr[8*REGISTER_CNT] = {0}; +static bool hasChanged = false; + +ShiftOut::ShiftOut(PinName ser, PinName srclk, PinName rclk, + PinName oe, PinName reset) : DSERIAL(ser), SRCLK(srclk), RCLK(rclk), LATCH(oe), RESET(reset) +{ + writeByte(0x00); // Reset the values of the registers to 0 + if(RESET != NC){ + DISABLE_RESET(); + } +} + +//Pulses the register +void ShiftOut::updateRegister(){ + SRCLK = 1; + wait_us(2); + SRCLK = 0; +} +//Updates the output register +void ShiftOut::updateOutput(){ + RCLK = 1; + wait_us(2); + RCLK = 0; +} +//Writes a byte to the shift register +void ShiftOut::writeByte(unsigned char byte){ + hasChanged = true; + for(int i = 0; i<8*REGISTER_CNT; i++){ + DSERIAL = (byte & 0x01<<i)>>i; + updateRegister(); + } + updateOutput(); +} +//Writes a byte to the shift register +void ShiftOut::write2Byte(unsigned int word){ + hasChanged = true; + for(int i = 0; i<8*REGISTER_CNT; i++){ + DSERIAL = (word & 0x01<<i)>>i; + updateRegister(); + } + updateOutput(); +} +//Writes a bit to the shift register +void ShiftOut::writeBit(unsigned char bit){ + DSERIAL = bit & 0x01; + updateRegister(); + updateOutput(); + } +//Writes multiple bits from an array to create an animation +void ShiftOut::animate(int arr[][8], int lines, int delay_ms){ + hasChanged = true; + for(int i = 0; i < lines; i++){ + for(int j = 0; j < 8; j++){ + writeBit(arr[i][j]); + } + wait_ms(delay_ms); + } +} + +void ShiftOut::animationExample(){ + hasChanged = true; + int strobe[][8]= {{1,0,0,0,0,0,0,0}, + {0,1,0,0,0,0,0,0}, + {0,0,1,0,0,0,0,0}, + {0,0,0,1,0,0,0,0}, + {0,0,0,0,1,0,0,0}, + {0,0,0,0,0,1,0,0}, + {0,0,0,0,0,0,1,0}, + {0,0,0,0,0,0,0,1}}; + + int nightrider[18][8]= {{1,0,0,0,0,0,0,0}, + {1,1,0,0,0,0,0,0}, + {1,1,1,0,0,0,0,0}, + {0,1,1,1,0,0,0,0}, + {0,0,1,1,1,0,0,0}, + {0,0,0,1,1,1,0,0}, + {0,0,0,0,1,1,1,0}, + {0,0,0,0,0,1,1,1}, + {0,0,0,0,0,0,1,1}, + {0,0,0,0,0,0,0,1}, + {0,0,0,0,0,0,1,1}, + {0,0,0,0,0,1,1,1}, + {0,0,0,0,1,1,1,0}, + {0,0,0,1,1,1,0,0}, + {0,0,1,1,1,0,0,0}, + {0,1,1,1,0,0,0,0}, + {1,1,1,0,0,0,0,0}, + {1,1,0,0,0,0,0,0}}; + + animate(nightrider, 18, 50); + wait(1); + animate(strobe, 8, 200); + } + +void ShiftOut::writeBitAtPos(unsigned char pin, bool state){ + if(hasChanged){ + clearStateArray(); + hasChanged = false; + } + if(pin < 8*REGISTER_CNT){ + stateArr[pin] = state; + } + writeArray(stateArr); +} + +void ShiftOut::writeArray(char arr[8*REGISTER_CNT]){ + for(int i = (8*REGISTER_CNT)-1; i >= 0; i--) { + writeBit(arr[i]); + } + +} + +void ShiftOut::clearStateArray(){ + for(int i = 0; i < 8*REGISTER_CNT; i++){ + stateArr[i] = 0; + } + }
diff -r 7da2cb94da6c -r 78dd1100a67b ShiftOut.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ShiftOut.h Fri Sep 10 08:19:02 2021 +0000 @@ -0,0 +1,144 @@ +/* + * Copyright 2015 Benjamin R. Moeklegaard + * + * Licensed 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 SHIFT_H +#define SHIFT_H + +//Constant for managing n-numbers of registers, Function supported (writeByte, writeBit, writeBitAtPos) +#define REGISTER_CNT 2 +/** + * This is a library for easy interfacing with the SN74HC595N 8-bit Shift Register. + * The library includes functions for writing bits, bytes and animation array to the register. + * The Functions are mainly based for writting 8-bits or one byte and can be moddified to work + * with multiple shift registers. + *@code + * #include "mbed.h" + * #include "ShiftOut.h" + * ShiftOut reg(PA_8, PA_9, PC_7, PB_6, D1); + * + * int main(){ + * while(1){ + * reg.writeBitAtPos(3, 1); + * wait(2); + * reg.writeByte(0x30); + * wait(2); + * } + * }@endcode + */ + +class ShiftOut +{ +public: + + /** Create a ShiftOut interface + * + * @param ser Serial data line + * @param srclk Data register clock + * @param rclk Output register clock + * @param oe Output enable (Active Low) + * @param reset Reset line for data register (Active Low) + * Writing Byte Example: + * @code + * #include "mbed.h" + * #include "ShiftOut.h" + * + * ShiftOut reg(PA_8, PA_9, PC_7, PB_6, D1); + * int main() + * { + * reg.writeByte(0x00); //Writes each bit to the SN74HC595N + * while(1) { + * wait_ms(300); + * } + * } + * @endcode + */ + + +ShiftOut(PinName ser, PinName srclk, PinName rclk, + PinName oe, PinName reset); + +/** + * Writes a byte to the shift register + * @param byte 0xXX or numbers from 0-255 + */ +void writeByte(unsigned char); + +void write2Byte(unsigned int); + + +/** + * Writes a bit to the first output on the shift register + * @param bit 0 or 1 + */ + +void writeBit(unsigned char); + +/** + * Writes bits from an 2D array, with configurable delay + * @param int array, int lines, int delay_ms + * writes a 2D array with n-lines with a configurable delay in ms + * @code + * #include "mbed.h" + * #include "ShiftOut.h" + * ShiftOut reg(PA_8, PA_9, PC_7, PB_6, D1); + * + * int main(){ + * int strobe[][8]= {{1,0,0,0,0,0,0,0}, + * {0,1,0,0,0,0,0,0}, + * {0,0,1,0,0,0,0,0}, + * {0,0,0,1,0,0,0,0}, + * {0,0,0,0,1,0,0,0}, + * {0,0,0,0,0,1,0,0}, + * {0,0,0,0,0,0,1,0}, + * {0,0,0,0,0,0,0,1}}; + * while(1){ + * reg.animate(strobe, 8, 200); + * } + * } + * @endcode + */ + +void animate(int[][8], int, int); + +/** + * Demonstrates two animation examples by using the animate function + */ + +void animationExample(void); + +/** + * Writes the desired state to the output on the shift register + * @param char output, state + */ + +void writeBitAtPos(unsigned char, bool); + +/** + * Writes the corresponding array item to the output on the shift register + * @param char array writes to the output from a state array + */ + +void writeArray(char[8]); + +protected: +void updateRegister(void); +void updateOutput(void); +void clearStateArray(void); +DigitalOut DSERIAL, LATCH, RCLK, SRCLK, RESET; + +}; + +#endif
diff -r 7da2cb94da6c -r 78dd1100a67b ShiftOut.lib --- a/ShiftOut.lib Fri Sep 18 06:07:52 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://os.mbed.com/users/rogerzuber/code/HSL_4_8_A14_7seg_base/#a5f14c6814df