A library interface to shift register such as NXP 74HCT595

Dependents:   DiscoTech filter_implement System_Project_V6 LM35functionshield ... more

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?

UserRevisionLine numberNew contents of line
yoonghm 0:a0e3fd47970f 1 #include "mbed.h"
yoonghm 0:a0e3fd47970f 2 #include "ShiftReg.h"
yoonghm 0:a0e3fd47970f 3
yoonghm 0:a0e3fd47970f 4 #define MY_DEBUG 0
yoonghm 0:a0e3fd47970f 5
yoonghm 0:a0e3fd47970f 6 ShiftReg::ShiftReg
yoonghm 0:a0e3fd47970f 7 (PinName data
yoonghm 0:a0e3fd47970f 8 ,PinName store
yoonghm 0:a0e3fd47970f 9 ,PinName clock
yoonghm 0:a0e3fd47970f 10 ): _ds(data), _st(store), _sh(clock)
yoonghm 0:a0e3fd47970f 11 {
yoonghm 0:a0e3fd47970f 12 }
yoonghm 0:a0e3fd47970f 13
yoonghm 0:a0e3fd47970f 14
yoonghm 0:a0e3fd47970f 15 void ShiftReg::ShiftByte
yoonghm 0:a0e3fd47970f 16 (int8_t data
yoonghm 0:a0e3fd47970f 17 ,BitOrd ord
yoonghm 0:a0e3fd47970f 18 )
yoonghm 0:a0e3fd47970f 19 {
yoonghm 0:a0e3fd47970f 20 uint8_t mask;
yoonghm 0:a0e3fd47970f 21
yoonghm 0:a0e3fd47970f 22 if (ord == MSBFirst) mask = 0x80;
yoonghm 0:a0e3fd47970f 23 else mask = 0x01;
yoonghm 0:a0e3fd47970f 24
yoonghm 0:a0e3fd47970f 25 for (int i = 0; i < 8; i++)
yoonghm 0:a0e3fd47970f 26 {
yoonghm 0:a0e3fd47970f 27 if (data & mask) _ds = 1;
yoonghm 0:a0e3fd47970f 28 else _ds = 0;
yoonghm 0:a0e3fd47970f 29
yoonghm 0:a0e3fd47970f 30 #if MY_DEBUG > 0
yoonghm 0:a0e3fd47970f 31 printf("%d ", _ds.read());
yoonghm 0:a0e3fd47970f 32 #endif /* MY_DEBUG */
yoonghm 0:a0e3fd47970f 33
yoonghm 0:a0e3fd47970f 34 if (ord == MSBFirst) mask = mask >> 1;
yoonghm 0:a0e3fd47970f 35 else mask = mask << 1;
yoonghm 0:a0e3fd47970f 36
yoonghm 0:a0e3fd47970f 37 _sh = 0;
yoonghm 0:a0e3fd47970f 38 _sh = 1;
yoonghm 0:a0e3fd47970f 39 }
yoonghm 0:a0e3fd47970f 40
yoonghm 0:a0e3fd47970f 41 #if MY_DEBUG > 0
yoonghm 0:a0e3fd47970f 42 printf("\n");
yoonghm 0:a0e3fd47970f 43 #endif /* MY_DEBUG */
yoonghm 0:a0e3fd47970f 44
yoonghm 0:a0e3fd47970f 45 }
yoonghm 0:a0e3fd47970f 46
yoonghm 0:a0e3fd47970f 47 void
yoonghm 0:a0e3fd47970f 48 ShiftReg::ShiftBit
yoonghm 0:a0e3fd47970f 49 (int8_t data
yoonghm 0:a0e3fd47970f 50 )
yoonghm 0:a0e3fd47970f 51 {
yoonghm 0:a0e3fd47970f 52 _ds = data;
yoonghm 0:a0e3fd47970f 53 _sh = 0;
yoonghm 0:a0e3fd47970f 54 _sh = 1;
yoonghm 0:a0e3fd47970f 55 }
yoonghm 0:a0e3fd47970f 56
yoonghm 0:a0e3fd47970f 57 void
yoonghm 0:a0e3fd47970f 58 ShiftReg::Latch
yoonghm 0:a0e3fd47970f 59 (
yoonghm 0:a0e3fd47970f 60 )
yoonghm 0:a0e3fd47970f 61 {
yoonghm 0:a0e3fd47970f 62 _st = 1;
yoonghm 0:a0e3fd47970f 63 _st = 0;
yoonghm 0:a0e3fd47970f 64 }