ShiftReg library (74HC595, NUJ3711)
ShiftReg.cpp@0:5f1670338bef, 2015-10-24 (annotated)
- Committer:
- kysiki
- Date:
- Sat Oct 24 16:30:11 2015 +0000
- Revision:
- 0:5f1670338bef
- Child:
- 1:3741bc0fbba8
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kysiki | 0:5f1670338bef | 1 | #include "ShiftReg.h" |
kysiki | 0:5f1670338bef | 2 | #include "mbed.h" |
kysiki | 0:5f1670338bef | 3 | |
kysiki | 0:5f1670338bef | 4 | ShiftReg::ShiftReg(PinName dataPin, PinName stbPin, |
kysiki | 0:5f1670338bef | 5 | PinName clkPin, PinName clrPin) |
kysiki | 0:5f1670338bef | 6 | : |
kysiki | 0:5f1670338bef | 7 | DATA(dataPin), |
kysiki | 0:5f1670338bef | 8 | STB(stbPin), |
kysiki | 0:5f1670338bef | 9 | CLK(clkPin), |
kysiki | 0:5f1670338bef | 10 | CLR(clrPin) |
kysiki | 0:5f1670338bef | 11 | { |
kysiki | 0:5f1670338bef | 12 | DATA = 1; |
kysiki | 0:5f1670338bef | 13 | STB = 1; |
kysiki | 0:5f1670338bef | 14 | CLK = 1; |
kysiki | 0:5f1670338bef | 15 | CLR = 1; |
kysiki | 0:5f1670338bef | 16 | } |
kysiki | 0:5f1670338bef | 17 | |
kysiki | 0:5f1670338bef | 18 | void ShiftReg::send_data(uint8_t data) { |
kysiki | 0:5f1670338bef | 19 | shiftOut(data); |
kysiki | 0:5f1670338bef | 20 | wait_us(1); |
kysiki | 0:5f1670338bef | 21 | #ifdef HC595 |
kysiki | 0:5f1670338bef | 22 | STB = 0; |
kysiki | 0:5f1670338bef | 23 | wait_us(1); |
kysiki | 0:5f1670338bef | 24 | STB = 1; |
kysiki | 0:5f1670338bef | 25 | #elif NJU3711 |
kysiki | 0:5f1670338bef | 26 | STB = 1; |
kysiki | 0:5f1670338bef | 27 | wait_us(1); |
kysiki | 0:5f1670338bef | 28 | STB = 0; |
kysiki | 0:5f1670338bef | 29 | #else |
kysiki | 0:5f1670338bef | 30 | printf("not support\n"); |
kysiki | 0:5f1670338bef | 31 | #endif |
kysiki | 0:5f1670338bef | 32 | wait_us(1); |
kysiki | 0:5f1670338bef | 33 | } |
kysiki | 0:5f1670338bef | 34 | |
kysiki | 0:5f1670338bef | 35 | void ShiftReg::shiftOut(uint8_t data) { |
kysiki | 0:5f1670338bef | 36 | for(int i = 0; i < 8; i++) { |
kysiki | 0:5f1670338bef | 37 | DATA = (data & (1 << i)); |
kysiki | 0:5f1670338bef | 38 | CLK = 0; |
kysiki | 0:5f1670338bef | 39 | CLK = 1; |
kysiki | 0:5f1670338bef | 40 | } |
kysiki | 0:5f1670338bef | 41 | } |