A library interface to shift register such as NXP 74HCT595
Dependents: DiscoTech filter_implement System_Project_V6 LM35functionshield ... more
ShiftReg.h@0:a0e3fd47970f, 2012-11-09 (annotated)
- Committer:
- yoonghm
- Date:
- Fri Nov 09 01:03:30 2012 +0000
- Revision:
- 0:a0e3fd47970f
v1.0 Shift Register library for IC such as 74HCT595
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yoonghm | 0:a0e3fd47970f | 1 | /* mbed Shift Register Library, such as for NXP 74HC595 |
yoonghm | 0:a0e3fd47970f | 2 | * Copyright (c) 2012, YoongHM |
yoonghm | 0:a0e3fd47970f | 3 | * |
yoonghm | 0:a0e3fd47970f | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
yoonghm | 0:a0e3fd47970f | 5 | * of this software and associated documentation files (the "Software"), to deal |
yoonghm | 0:a0e3fd47970f | 6 | * in the Software without restriction, including without limitation the rights |
yoonghm | 0:a0e3fd47970f | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yoonghm | 0:a0e3fd47970f | 8 | * copies of the Software, and to permit persons to whom the Software is |
yoonghm | 0:a0e3fd47970f | 9 | * furnished to do so, subject to the following conditions: |
yoonghm | 0:a0e3fd47970f | 10 | * |
yoonghm | 0:a0e3fd47970f | 11 | * The above copyright notice and this permission notice shall be included in |
yoonghm | 0:a0e3fd47970f | 12 | * all copies or substantial portions of the Software. |
yoonghm | 0:a0e3fd47970f | 13 | * |
yoonghm | 0:a0e3fd47970f | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yoonghm | 0:a0e3fd47970f | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yoonghm | 0:a0e3fd47970f | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yoonghm | 0:a0e3fd47970f | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yoonghm | 0:a0e3fd47970f | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yoonghm | 0:a0e3fd47970f | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yoonghm | 0:a0e3fd47970f | 20 | * THE SOFTWARE. |
yoonghm | 0:a0e3fd47970f | 21 | */ |
yoonghm | 0:a0e3fd47970f | 22 | |
yoonghm | 0:a0e3fd47970f | 23 | #ifndef _SHIFTREG_H |
yoonghm | 0:a0e3fd47970f | 24 | #define _SHIFTREG_H |
yoonghm | 0:a0e3fd47970f | 25 | |
yoonghm | 0:a0e3fd47970f | 26 | #include "mbed.h" |
yoonghm | 0:a0e3fd47970f | 27 | |
yoonghm | 0:a0e3fd47970f | 28 | /** A interface to drive shifter register as such 74HCT595 |
yoonghm | 0:a0e3fd47970f | 29 | * |
yoonghm | 0:a0e3fd47970f | 30 | * @code |
yoonghm | 0:a0e3fd47970f | 31 | * #include "mbed.h" |
yoonghm | 0:a0e3fd47970f | 32 | * #include "ShiftReg.h" |
yoonghm | 0:a0e3fd47970f | 33 | * |
yoonghm | 0:a0e3fd47970f | 34 | * ShiftReg HC595(p21, p22, p23); |
yoonghm | 0:a0e3fd47970f | 35 | * |
yoonghm | 0:a0e3fd47970f | 36 | * int main() { |
yoonghm | 0:a0e3fd47970f | 37 | * // clear shift and store registers initially |
yoonghm | 0:a0e3fd47970f | 38 | * HC595.ShiftByte(0x00, ShiftReg::MSBFirst); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 39 | * |
yoonghm | 0:a0e3fd47970f | 40 | * while(1) { |
yoonghm | 0:a0e3fd47970f | 41 | * // Demostrate to shift in bit by bit |
yoonghm | 0:a0e3fd47970f | 42 | * HC595.ShiftBit(1); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 43 | * for (int i = 0; i < 8; i++) { |
yoonghm | 0:a0e3fd47970f | 44 | * HC595.ShiftBit(0); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 45 | * } |
yoonghm | 0:a0e3fd47970f | 46 | |
yoonghm | 0:a0e3fd47970f | 47 | * // Demostrate to shift in byte-by-byte |
yoonghm | 0:a0e3fd47970f | 48 | * // HC595.ShiftByte(0x80, ShiftReg::MSBFirst); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 49 | * HC595.ShiftByte(0x40, ShiftReg::MSBFirst); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 50 | * HC595.ShiftByte(0x20, ShiftReg::MSBFirst); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 51 | * HC595.ShiftByte(0x10, ShiftReg::MSBFirst); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 52 | * HC595.ShiftByte(0x08, ShiftReg::MSBFirst); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 53 | * HC595.ShiftByte(0x04, ShiftReg::MSBFirst); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 54 | * HC595.ShiftByte(0x02, ShiftReg::MSBFirst); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 55 | * HC595.ShiftByte(0x01, ShiftReg::MSBFirst); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 56 | * HC595.ShiftByte(0x00, ShiftReg::MSBFirst); HC595.Latch(); wait(0.2); |
yoonghm | 0:a0e3fd47970f | 57 | * } |
yoonghm | 0:a0e3fd47970f | 58 | * } |
yoonghm | 0:a0e3fd47970f | 59 | * @endcode |
yoonghm | 0:a0e3fd47970f | 60 | */ |
yoonghm | 0:a0e3fd47970f | 61 | |
yoonghm | 0:a0e3fd47970f | 62 | class ShiftReg |
yoonghm | 0:a0e3fd47970f | 63 | { |
yoonghm | 0:a0e3fd47970f | 64 | public: |
yoonghm | 0:a0e3fd47970f | 65 | /** Bit order out format */ |
yoonghm | 0:a0e3fd47970f | 66 | enum BitOrd { |
yoonghm | 0:a0e3fd47970f | 67 | MSBFirst = 0x80, /**< Most significant bit first */ |
yoonghm | 0:a0e3fd47970f | 68 | LSBFirst = 0x01 /**< Least significant bit first */ |
yoonghm | 0:a0e3fd47970f | 69 | }; |
yoonghm | 0:a0e3fd47970f | 70 | |
yoonghm | 0:a0e3fd47970f | 71 | /** Create a ShiftReg interface to shift register |
yoonghm | 0:a0e3fd47970f | 72 | * |
yoonghm | 0:a0e3fd47970f | 73 | * @param data Pin to serial input to shift register |
yoonghm | 0:a0e3fd47970f | 74 | * @param store Pin to store register |
yoonghm | 0:a0e3fd47970f | 75 | * @param clock Pin to shift into register |
yoonghm | 0:a0e3fd47970f | 76 | */ |
yoonghm | 0:a0e3fd47970f | 77 | ShiftReg |
yoonghm | 0:a0e3fd47970f | 78 | (PinName data |
yoonghm | 0:a0e3fd47970f | 79 | ,PinName store |
yoonghm | 0:a0e3fd47970f | 80 | ,PinName clock |
yoonghm | 0:a0e3fd47970f | 81 | ); |
yoonghm | 0:a0e3fd47970f | 82 | |
yoonghm | 0:a0e3fd47970f | 83 | /** Shift out 8-bit data via the serial pin |
yoonghm | 0:a0e3fd47970f | 84 | * |
yoonghm | 0:a0e3fd47970f | 85 | * @param data Data to be shifted out via the serial pin |
yoonghm | 0:a0e3fd47970f | 86 | * @param order Bit order to shift out data. Default is MSBFirst |
yoonghm | 0:a0e3fd47970f | 87 | */ |
yoonghm | 0:a0e3fd47970f | 88 | void |
yoonghm | 0:a0e3fd47970f | 89 | ShiftByte |
yoonghm | 0:a0e3fd47970f | 90 | (int8_t data |
yoonghm | 0:a0e3fd47970f | 91 | ,BitOrd ord = MSBFirst |
yoonghm | 0:a0e3fd47970f | 92 | ); |
yoonghm | 0:a0e3fd47970f | 93 | |
yoonghm | 0:a0e3fd47970f | 94 | /** Shift out 1-bit data via the serial pin |
yoonghm | 0:a0e3fd47970f | 95 | * |
yoonghm | 0:a0e3fd47970f | 96 | * @param data Data to be shifted out via the serial pin |
yoonghm | 0:a0e3fd47970f | 97 | */ |
yoonghm | 0:a0e3fd47970f | 98 | void |
yoonghm | 0:a0e3fd47970f | 99 | ShiftBit |
yoonghm | 0:a0e3fd47970f | 100 | (int8_t data = 0 |
yoonghm | 0:a0e3fd47970f | 101 | ); |
yoonghm | 0:a0e3fd47970f | 102 | |
yoonghm | 0:a0e3fd47970f | 103 | /** Latch data out |
yoonghm | 0:a0e3fd47970f | 104 | */ |
yoonghm | 0:a0e3fd47970f | 105 | void |
yoonghm | 0:a0e3fd47970f | 106 | Latch(); |
yoonghm | 0:a0e3fd47970f | 107 | |
yoonghm | 0:a0e3fd47970f | 108 | private: |
yoonghm | 0:a0e3fd47970f | 109 | DigitalOut _ds; // Serial in |
yoonghm | 0:a0e3fd47970f | 110 | DigitalOut _st; // store register or latch |
yoonghm | 0:a0e3fd47970f | 111 | DigitalOut _sh; // shift register |
yoonghm | 0:a0e3fd47970f | 112 | BitOrd _ord; // Bit order to shift out data |
yoonghm | 0:a0e3fd47970f | 113 | }; |
yoonghm | 0:a0e3fd47970f | 114 | |
yoonghm | 0:a0e3fd47970f | 115 | #endif // _SHIFTREG_H |