Simple library for controlling a shift register using only three pins on the mbed. Defaults to controlling an 8 bit shift register.
Dependents: Lab07_RTOS_display Lab07_RTOS_queue
ShiftOut.h@4:7333dc6fca5c, 2015-07-14 (annotated)
- Committer:
- ollie8
- Date:
- Tue Jul 14 07:33:26 2015 +0000
- Revision:
- 4:7333dc6fca5c
- Parent:
- 3:31000094ef1e
Documentation update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ollie8 | 0:a4f4842715fd | 1 | #ifndef SHIFTOUT_H |
ollie8 | 0:a4f4842715fd | 2 | #define SHIFTOUT_H |
ollie8 | 0:a4f4842715fd | 3 | |
ollie8 | 0:a4f4842715fd | 4 | #include <mbed.h> |
ollie8 | 0:a4f4842715fd | 5 | |
ollie8 | 2:78bb6ead1c28 | 6 | /** A simple serial driver for a shift register that uses only three digital out pins leaving SPI and i2c free. |
ollie8 | 3:31000094ef1e | 7 | * ShiftOut can be configured for any size shift register but defaults to eight bits. |
ollie8 | 2:78bb6ead1c28 | 8 | */ |
ollie8 | 0:a4f4842715fd | 9 | class ShiftOut { |
ollie8 | 0:a4f4842715fd | 10 | |
ollie8 | 0:a4f4842715fd | 11 | public : |
ollie8 | 0:a4f4842715fd | 12 | |
ollie8 | 4:7333dc6fca5c | 13 | /** Constructs a new ShiftOut with the given three pins. |
ollie8 | 4:7333dc6fca5c | 14 | * @param clk - the pin to use for the shift register clock. |
ollie8 | 4:7333dc6fca5c | 15 | * @param data - the pin to use for the shift register data line. |
ollie8 | 4:7333dc6fca5c | 16 | * @param latch - the pin to use for the shift register latch. |
ollie8 | 4:7333dc6fca5c | 17 | * @param registerCount - the number of registers in the shift register, defaults to eight. |
ollie8 | 2:78bb6ead1c28 | 18 | */ |
ollie8 | 2:78bb6ead1c28 | 19 | ShiftOut(PinName clk, PinName data, PinName latch, int8_t registerCount = 0x08) { |
ollie8 | 0:a4f4842715fd | 20 | clkout = new DigitalOut(clk); |
ollie8 | 0:a4f4842715fd | 21 | dataout = new DigitalOut(data); |
ollie8 | 0:a4f4842715fd | 22 | latchout = new DigitalOut(latch); |
ollie8 | 0:a4f4842715fd | 23 | this->registerCount = registerCount; |
ollie8 | 0:a4f4842715fd | 24 | } |
ollie8 | 0:a4f4842715fd | 25 | |
ollie8 | 0:a4f4842715fd | 26 | ~ShiftOut() { |
ollie8 | 0:a4f4842715fd | 27 | delete clkout; |
ollie8 | 0:a4f4842715fd | 28 | delete dataout; |
ollie8 | 0:a4f4842715fd | 29 | delete latchout; |
ollie8 | 0:a4f4842715fd | 30 | } |
ollie8 | 0:a4f4842715fd | 31 | |
ollie8 | 2:78bb6ead1c28 | 32 | /** Writes the given integer to the shift register |
ollie8 | 2:78bb6ead1c28 | 33 | */ |
ollie8 | 1:4c2379445f72 | 34 | void write(int data) { |
ollie8 | 0:a4f4842715fd | 35 | *latchout = 0; |
ollie8 | 2:78bb6ead1c28 | 36 | for (int i = registerCount - 1; i >= 0; i--) { |
ollie8 | 0:a4f4842715fd | 37 | *clkout = 0; |
ollie8 | 0:a4f4842715fd | 38 | *dataout = (data & (1 << i)) != 0; |
ollie8 | 0:a4f4842715fd | 39 | *clkout = 1; |
ollie8 | 0:a4f4842715fd | 40 | } |
ollie8 | 0:a4f4842715fd | 41 | *latchout = 1; |
ollie8 | 0:a4f4842715fd | 42 | } |
ollie8 | 0:a4f4842715fd | 43 | |
ollie8 | 0:a4f4842715fd | 44 | private : |
ollie8 | 0:a4f4842715fd | 45 | DigitalOut *clkout; |
ollie8 | 0:a4f4842715fd | 46 | DigitalOut *dataout; |
ollie8 | 0:a4f4842715fd | 47 | DigitalOut *latchout; |
ollie8 | 0:a4f4842715fd | 48 | int8_t registerCount; |
ollie8 | 0:a4f4842715fd | 49 | }; |
ollie8 | 0:a4f4842715fd | 50 | |
ollie8 | 0:a4f4842715fd | 51 | #endif |