A library for interfacing with the SN74HC595N Shift register. Includes functions for writing bits, bytes, animation and bits at spesified positions.
ShiftOut.h@5:0bb6aa206b86, 2016-01-14 (annotated)
- Committer:
- benrammok
- Date:
- Thu Jan 14 22:14:40 2016 +0000
- Revision:
- 5:0bb6aa206b86
- Parent:
- 4:61198ffab8ca
Redone docs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benrammok | 0:e576e892f0ca | 1 | /* |
benrammok | 4:61198ffab8ca | 2 | * Copyright 2015 Benjamin R. Moeklegaard |
benrammok | 4:61198ffab8ca | 3 | * |
benrammok | 4:61198ffab8ca | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
benrammok | 4:61198ffab8ca | 5 | * you may not use this file except in compliance with the License. |
benrammok | 4:61198ffab8ca | 6 | * You may obtain a copy of the License at |
benrammok | 4:61198ffab8ca | 7 | * |
benrammok | 4:61198ffab8ca | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
benrammok | 4:61198ffab8ca | 9 | * |
benrammok | 4:61198ffab8ca | 10 | * Unless required by applicable law or agreed to in writing, software |
benrammok | 4:61198ffab8ca | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
benrammok | 4:61198ffab8ca | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
benrammok | 4:61198ffab8ca | 13 | * See the License for the specific language governing permissions and |
benrammok | 4:61198ffab8ca | 14 | * limitations under the License. |
benrammok | 0:e576e892f0ca | 15 | */ |
benrammok | 0:e576e892f0ca | 16 | |
benrammok | 0:e576e892f0ca | 17 | #ifndef SHIFT_H |
benrammok | 0:e576e892f0ca | 18 | #define SHIFT_H |
benrammok | 0:e576e892f0ca | 19 | |
benrammok | 4:61198ffab8ca | 20 | //Constant for managing n-numbers of registers, Function supported (writeByte, writeBit, writeBitAtPos) |
benrammok | 4:61198ffab8ca | 21 | #define REGISTER_CNT 1 |
benrammok | 4:61198ffab8ca | 22 | /** |
benrammok | 4:61198ffab8ca | 23 | * This is a library for easy interfacing with the SN74HC595N 8-bit Shift Register. |
benrammok | 4:61198ffab8ca | 24 | * The library includes functions for writing bits, bytes and animation array to the register. |
benrammok | 4:61198ffab8ca | 25 | * The Functions are mainly based for writting 8-bits or one byte and can be moddified to work |
benrammok | 4:61198ffab8ca | 26 | * with multiple shift registers. |
benrammok | 4:61198ffab8ca | 27 | *@code |
benrammok | 4:61198ffab8ca | 28 | * #include "mbed.h" |
benrammok | 4:61198ffab8ca | 29 | * #include "ShiftOut.h" |
benrammok | 4:61198ffab8ca | 30 | * ShiftOut reg(PA_8, PA_9, PC_7, PB_6, D1); |
benrammok | 4:61198ffab8ca | 31 | * |
benrammok | 4:61198ffab8ca | 32 | * int main(){ |
benrammok | 4:61198ffab8ca | 33 | * while(1){ |
benrammok | 4:61198ffab8ca | 34 | * reg.writeBitAtPos(3, 1); |
benrammok | 4:61198ffab8ca | 35 | * wait(2); |
benrammok | 4:61198ffab8ca | 36 | * reg.writeByte(0x30); |
benrammok | 4:61198ffab8ca | 37 | * wait(2); |
benrammok | 4:61198ffab8ca | 38 | * } |
benrammok | 5:0bb6aa206b86 | 39 | * }@endcode |
benrammok | 4:61198ffab8ca | 40 | */ |
benrammok | 4:61198ffab8ca | 41 | |
benrammok | 0:e576e892f0ca | 42 | class ShiftOut |
benrammok | 0:e576e892f0ca | 43 | { |
benrammok | 0:e576e892f0ca | 44 | public: |
benrammok | 4:61198ffab8ca | 45 | |
benrammok | 4:61198ffab8ca | 46 | /** Create a ShiftOut interface |
benrammok | 4:61198ffab8ca | 47 | * |
benrammok | 4:61198ffab8ca | 48 | * @param ser Serial data line |
benrammok | 4:61198ffab8ca | 49 | * @param srclk Data register clock |
benrammok | 4:61198ffab8ca | 50 | * @param rclk Output register clock |
benrammok | 4:61198ffab8ca | 51 | * @param oe Output enable (Active Low) |
benrammok | 4:61198ffab8ca | 52 | * @param reset Reset line for data register (Active Low) |
benrammok | 4:61198ffab8ca | 53 | * Writing Byte Example: |
benrammok | 4:61198ffab8ca | 54 | * @code |
benrammok | 4:61198ffab8ca | 55 | * #include "mbed.h" |
benrammok | 4:61198ffab8ca | 56 | * #include "ShiftOut.h" |
benrammok | 4:61198ffab8ca | 57 | * |
benrammok | 4:61198ffab8ca | 58 | * ShiftOut reg(PA_8, PA_9, PC_7, PB_6, D1); |
benrammok | 4:61198ffab8ca | 59 | * int main() |
benrammok | 4:61198ffab8ca | 60 | * { |
benrammok | 4:61198ffab8ca | 61 | * reg.writeByte(0x00); //Writes each bit to the SN74HC595N |
benrammok | 4:61198ffab8ca | 62 | * while(1) { |
benrammok | 4:61198ffab8ca | 63 | * wait_ms(300); |
benrammok | 4:61198ffab8ca | 64 | * } |
benrammok | 4:61198ffab8ca | 65 | * } |
benrammok | 4:61198ffab8ca | 66 | * @endcode |
benrammok | 4:61198ffab8ca | 67 | */ |
benrammok | 4:61198ffab8ca | 68 | |
benrammok | 4:61198ffab8ca | 69 | |
benrammok | 0:e576e892f0ca | 70 | ShiftOut(PinName ser, PinName srclk, PinName rclk, |
benrammok | 0:e576e892f0ca | 71 | PinName oe, PinName reset); |
benrammok | 0:e576e892f0ca | 72 | |
benrammok | 2:88581826b53c | 73 | /** |
benrammok | 4:61198ffab8ca | 74 | * Writes a byte to the shift register |
benrammok | 4:61198ffab8ca | 75 | * @param byte 0xXX or numbers from 0-255 |
benrammok | 4:61198ffab8ca | 76 | */ |
benrammok | 0:e576e892f0ca | 77 | void writeByte(unsigned char); |
benrammok | 0:e576e892f0ca | 78 | |
benrammok | 2:88581826b53c | 79 | |
benrammok | 2:88581826b53c | 80 | |
benrammok | 2:88581826b53c | 81 | |
benrammok | 2:88581826b53c | 82 | /** |
benrammok | 4:61198ffab8ca | 83 | * Writes a bit to the first output on the shift register |
benrammok | 4:61198ffab8ca | 84 | * @param bit 0 or 1 |
benrammok | 4:61198ffab8ca | 85 | */ |
benrammok | 4:61198ffab8ca | 86 | |
benrammok | 4:61198ffab8ca | 87 | void writeBit(unsigned char); |
benrammok | 4:61198ffab8ca | 88 | |
benrammok | 4:61198ffab8ca | 89 | /** |
benrammok | 4:61198ffab8ca | 90 | * Writes bits from an 2D array, with configurable delay |
benrammok | 4:61198ffab8ca | 91 | * @param int array, int lines, int delay_ms |
benrammok | 4:61198ffab8ca | 92 | * writes a 2D array with n-lines with a configurable delay in ms |
benrammok | 4:61198ffab8ca | 93 | * @code |
benrammok | 4:61198ffab8ca | 94 | * #include "mbed.h" |
benrammok | 4:61198ffab8ca | 95 | * #include "ShiftOut.h" |
benrammok | 4:61198ffab8ca | 96 | * ShiftOut reg(PA_8, PA_9, PC_7, PB_6, D1); |
benrammok | 4:61198ffab8ca | 97 | * |
benrammok | 4:61198ffab8ca | 98 | * int main(){ |
benrammok | 4:61198ffab8ca | 99 | * int strobe[][8]= {{1,0,0,0,0,0,0,0}, |
benrammok | 4:61198ffab8ca | 100 | * {0,1,0,0,0,0,0,0}, |
benrammok | 4:61198ffab8ca | 101 | * {0,0,1,0,0,0,0,0}, |
benrammok | 4:61198ffab8ca | 102 | * {0,0,0,1,0,0,0,0}, |
benrammok | 4:61198ffab8ca | 103 | * {0,0,0,0,1,0,0,0}, |
benrammok | 4:61198ffab8ca | 104 | * {0,0,0,0,0,1,0,0}, |
benrammok | 4:61198ffab8ca | 105 | * {0,0,0,0,0,0,1,0}, |
benrammok | 4:61198ffab8ca | 106 | * {0,0,0,0,0,0,0,1}}; |
benrammok | 4:61198ffab8ca | 107 | * while(1){ |
benrammok | 4:61198ffab8ca | 108 | * reg.animate(strobe, 8, 200); |
benrammok | 4:61198ffab8ca | 109 | * } |
benrammok | 4:61198ffab8ca | 110 | * } |
benrammok | 4:61198ffab8ca | 111 | * @endcode |
benrammok | 4:61198ffab8ca | 112 | */ |
benrammok | 0:e576e892f0ca | 113 | |
benrammok | 0:e576e892f0ca | 114 | void animate(int[][8], int, int); |
benrammok | 0:e576e892f0ca | 115 | |
benrammok | 2:88581826b53c | 116 | /** |
benrammok | 4:61198ffab8ca | 117 | * Demonstrates two animation examples by using the animate function |
benrammok | 4:61198ffab8ca | 118 | */ |
benrammok | 0:e576e892f0ca | 119 | |
benrammok | 0:e576e892f0ca | 120 | void animationExample(void); |
benrammok | 0:e576e892f0ca | 121 | |
benrammok | 2:88581826b53c | 122 | /** |
benrammok | 5:0bb6aa206b86 | 123 | * Writes the desired state to the output on the shift register |
benrammok | 4:61198ffab8ca | 124 | * @param char output, state |
benrammok | 4:61198ffab8ca | 125 | */ |
benrammok | 0:e576e892f0ca | 126 | |
benrammok | 0:e576e892f0ca | 127 | void writeBitAtPos(unsigned char, bool); |
benrammok | 0:e576e892f0ca | 128 | |
benrammok | 2:88581826b53c | 129 | /** |
benrammok | 4:61198ffab8ca | 130 | * Writes the corresponding array item to the output on the shift register |
benrammok | 4:61198ffab8ca | 131 | * @param char array writes to the output from a state array |
benrammok | 4:61198ffab8ca | 132 | */ |
benrammok | 0:e576e892f0ca | 133 | |
benrammok | 0:e576e892f0ca | 134 | void writeArray(char[8]); |
benrammok | 0:e576e892f0ca | 135 | |
benrammok | 0:e576e892f0ca | 136 | protected: |
benrammok | 0:e576e892f0ca | 137 | void updateRegister(void); |
benrammok | 0:e576e892f0ca | 138 | void updateOutput(void); |
benrammok | 3:a0df8989ffa2 | 139 | void clearStateArray(void); |
benrammok | 0:e576e892f0ca | 140 | DigitalOut DSERIAL, LATCH, RCLK, SRCLK, RESET; |
benrammok | 0:e576e892f0ca | 141 | |
benrammok | 0:e576e892f0ca | 142 | }; |
benrammok | 0:e576e892f0ca | 143 | |
benrammok | 0:e576e892f0ca | 144 | #endif |