needed for 1620 test code

Fork of ShiftOut by Ollie Milton

Committer:
ollie8
Date:
Tue Jul 14 07:28:28 2015 +0000
Revision:
3:31000094ef1e
Parent:
2:78bb6ead1c28
Child:
4:7333dc6fca5c
documentation update

Who changed what in which revision?

UserRevisionLine numberNew 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 3:31000094ef1e 13 /** Constructs a new ShiftOut with the given three pins.\n
ollie8 3:31000094ef1e 14 * clk - the pin to use for the shift register clock.\n
ollie8 3:31000094ef1e 15 * data - the pin to use for the shift register data line.\n
ollie8 3:31000094ef1e 16 * latch - the pin to use for the shift register latch.\n
ollie8 3:31000094ef1e 17 * registerCount - the number of registers in the shift register, defaults to eight.\n
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