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

Committer:
ollie8
Date:
2015-07-14
Revision:
3:31000094ef1e
Parent:
2:78bb6ead1c28
Child:
4:7333dc6fca5c

File content as of revision 3:31000094ef1e:

#ifndef SHIFTOUT_H
#define SHIFTOUT_H

#include <mbed.h>

/** A simple serial driver for a shift register that uses only three digital out pins leaving SPI and i2c free. 
 * ShiftOut can be configured for any size shift register but defaults to eight bits.
 */
class ShiftOut {

    public :
    
        /** Constructs a new ShiftOut with the given three pins.\n
         * clk - the pin to use for the shift register clock.\n
         * data - the pin to use for the shift register data line.\n
         * latch - the pin to use for the shift register latch.\n
         * registerCount - the number of registers in the shift register, defaults to eight.\n
         */
        ShiftOut(PinName clk, PinName data, PinName latch, int8_t registerCount = 0x08) {
            clkout = new DigitalOut(clk);
            dataout = new DigitalOut(data);
            latchout = new DigitalOut(latch);
            this->registerCount = registerCount;
        }
        
        ~ShiftOut() {
            delete clkout;
            delete dataout;
            delete latchout;
        }
        
        /** Writes the given integer to the shift register
         */
        void write(int data) {
            *latchout = 0;
            for (int i = registerCount - 1; i >=  0; i--) {
                *clkout = 0;
                *dataout = (data & (1 << i)) != 0;
                *clkout = 1;
            }
            *latchout = 1;
        }
         
    private :
        DigitalOut *clkout;
        DigitalOut *dataout;
        DigitalOut *latchout;
        int8_t registerCount;     
};

#endif