-
Notifications
You must be signed in to change notification settings - Fork 0
/
shiftRegisterFast.h
108 lines (89 loc) · 2.37 KB
/
shiftRegisterFast.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* shiftRegisterFast.h
*
* Created on: 06.07.2014
* Author: user
*/
#ifndef SHIFTREGISTERFAST_H_
#define SHIFTREGISTERFAST_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "portManipulations.h"
// namespace to quickly write data to one shift register
//
// in order to use these functions, you need to define the three pins that should be use to shift out the data
// example:
// #define SHIFTREGISTER_SER D,5
// #define SHIFTREGISTER_RCK D,6
// #define SHIFTREGISTER_SRCK D,7
// you have to place those defines BEFORE you include this header file!
//
// shifting out a byte takes ~25us
#ifdef SHIFTREGISTER_SER
#ifdef SHIFTREGISTER_RCK
#ifdef SHIFTREGISTER_SRCK
namespace shiftRegFast {
enum BitOrder {LSB_FIRST,MSB_FIRST};
// initialize pins
// call this function before shifting out data!
inline void setup() {
//set pins to output mode
bit_dir_outp(SHIFTREGISTER_SER);
bit_dir_outp(SHIFTREGISTER_SRCK);
bit_dir_outp(SHIFTREGISTER_RCK);
//initialize clocks to low
bit_clear(SHIFTREGISTER_SRCK);
bit_clear(SHIFTREGISTER_RCK);
}
// pulse the serial clock to shift storage registers and place vaule from SER to first storage register
inline void serClkIn() {
bit_set(SHIFTREGISTER_SRCK);
bit_clear(SHIFTREGISTER_SRCK);
}
// pulse the latch pin to copy data from storage register to output
inline void enableOutput() {
bit_set(SHIFTREGISTER_RCK);
bit_clear(SHIFTREGISTER_RCK);
}
// set the data pin to the given value
inline void setDataBit(bool value) {
if (value) bit_set(SHIFTREGISTER_SER);
else bit_clear(SHIFTREGISTER_SER);
}
// send out a byte
inline void write_8bit(uint8_t byte, shiftRegFast::BitOrder order = LSB_FIRST) {
if (order == LSB_FIRST) {
for (uint8_t index = 0; index < 8; index++) {
setDataBit(byte & _BV(index));
serClkIn();
}
} else {
for (int8_t index = 7; index >=0; index--) {
setDataBit(byte & _BV(index));
serClkIn();
}
}
}
// send out an integer
inline void write_16bit(uint16_t integer, shiftRegFast::BitOrder order=LSB_FIRST) {
if (order == LSB_FIRST) {
for (uint8_t index = 0; index < 16; index++) {
setDataBit(integer & _BV(index));
serClkIn();
}
} else {
for (int8_t index = 15; index>=0; index--) {
setDataBit(integer & _BV(index));
serClkIn();
}
}
}
}
#endif
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif /* SHIFTREGISTERFAST_H_ */