needed for 1620 test code

Fork of ShiftOut by Ollie Milton

Committer:
el11es
Date:
Tue Sep 27 08:30:42 2016 +0000
Revision:
5:f7749e47a29b
Parent:
4:7333dc6fca5c
nothing changed;

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 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 }
el11es 5:f7749e47a29b 43
el11es 5:f7749e47a29b 44 void set_latch (int val){
el11es 5:f7749e47a29b 45 *latchout = val;
el11es 5:f7749e47a29b 46 }
ollie8 0:a4f4842715fd 47
ollie8 0:a4f4842715fd 48 private :
ollie8 0:a4f4842715fd 49 DigitalOut *clkout;
ollie8 0:a4f4842715fd 50 DigitalOut *dataout;
ollie8 0:a4f4842715fd 51 DigitalOut *latchout;
ollie8 0:a4f4842715fd 52 int8_t registerCount;
ollie8 0:a4f4842715fd 53 };
ollie8 0:a4f4842715fd 54
ollie8 0:a4f4842715fd 55 #endif