ShiftReg library (74HC595, NUJ3711)
ShiftReg.cpp@1:3741bc0fbba8, 2015-10-24 (annotated)
- Committer:
- kysiki
- Date:
- Sat Oct 24 17:44:30 2015 +0000
- Revision:
- 1:3741bc0fbba8
- Parent:
- 0:5f1670338bef
8bit, 16bit, 32bit???
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 | 1:3741bc0fbba8 | 3 | |
kysiki | 1:3741bc0fbba8 | 4 | template <class T_bit> |
kysiki | 1:3741bc0fbba8 | 5 | ShiftReg<T_bit>::ShiftReg(PinName dataPin, PinName stbPin, |
kysiki | 0:5f1670338bef | 6 | PinName clkPin, PinName clrPin) |
kysiki | 0:5f1670338bef | 7 | : |
kysiki | 0:5f1670338bef | 8 | DATA(dataPin), |
kysiki | 0:5f1670338bef | 9 | STB(stbPin), |
kysiki | 0:5f1670338bef | 10 | CLK(clkPin), |
kysiki | 0:5f1670338bef | 11 | CLR(clrPin) |
kysiki | 0:5f1670338bef | 12 | { |
kysiki | 0:5f1670338bef | 13 | DATA = 1; |
kysiki | 0:5f1670338bef | 14 | STB = 1; |
kysiki | 0:5f1670338bef | 15 | CLK = 1; |
kysiki | 0:5f1670338bef | 16 | CLR = 1; |
kysiki | 0:5f1670338bef | 17 | } |
kysiki | 1:3741bc0fbba8 | 18 | |
kysiki | 1:3741bc0fbba8 | 19 | template <class T_bit> |
kysiki | 1:3741bc0fbba8 | 20 | void ShiftReg<T_bit>::send_data(T_bit data, uint8_t size) { |
kysiki | 1:3741bc0fbba8 | 21 | shiftOut(data, size); |
kysiki | 0:5f1670338bef | 22 | wait_us(1); |
kysiki | 0:5f1670338bef | 23 | #ifdef HC595 |
kysiki | 0:5f1670338bef | 24 | STB = 0; |
kysiki | 0:5f1670338bef | 25 | wait_us(1); |
kysiki | 0:5f1670338bef | 26 | STB = 1; |
kysiki | 0:5f1670338bef | 27 | #elif NJU3711 |
kysiki | 0:5f1670338bef | 28 | STB = 1; |
kysiki | 0:5f1670338bef | 29 | wait_us(1); |
kysiki | 0:5f1670338bef | 30 | STB = 0; |
kysiki | 0:5f1670338bef | 31 | #else |
kysiki | 0:5f1670338bef | 32 | printf("not support\n"); |
kysiki | 0:5f1670338bef | 33 | #endif |
kysiki | 0:5f1670338bef | 34 | wait_us(1); |
kysiki | 0:5f1670338bef | 35 | } |
kysiki | 0:5f1670338bef | 36 | |
kysiki | 1:3741bc0fbba8 | 37 | template <class T_bit> |
kysiki | 1:3741bc0fbba8 | 38 | void ShiftReg<T_bit>::shiftOut(T_bit data, uint8_t size) { |
kysiki | 1:3741bc0fbba8 | 39 | for(int i = 0; i < size; i++) { |
kysiki | 0:5f1670338bef | 40 | DATA = (data & (1 << i)); |
kysiki | 0:5f1670338bef | 41 | CLK = 0; |
kysiki | 0:5f1670338bef | 42 | CLK = 1; |
kysiki | 0:5f1670338bef | 43 | } |
kysiki | 1:3741bc0fbba8 | 44 | } |
kysiki | 1:3741bc0fbba8 | 45 | template class ShiftReg<uint8_t>; |
kysiki | 1:3741bc0fbba8 | 46 | template class ShiftReg<uint16_t>; |
kysiki | 1:3741bc0fbba8 | 47 | template class ShiftReg<uint32_t>; |