-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShiftduino.cpp
More file actions
105 lines (74 loc) · 2.07 KB
/
Copy pathShiftduino.cpp
File metadata and controls
105 lines (74 loc) · 2.07 KB
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
/*
Library to control the 74HC595
More info at http://bildr.org/?s=74hc595
*/
#include "Arduino.h"
#include "Shiftduino.h"
int _dataPin; //pin 14 on the 75HC595
int _latchPin; //pin 12 on the 75HC595
int _clockPin; //pin 11 on the 75HC595
int _numOfRegisterPins; //number of c.i. in cascade
#define maxRegisterPins 64
boolean _registers[maxRegisterPins];
Shiftduino::Shiftduino(uint8_t dataPin, uint8_t clockPin, uint8_t latchPin, uint8_t numOfRegisters){
_dataPin = dataPin;
pinMode(_dataPin, OUTPUT);
_clockPin = clockPin;
pinMode(_clockPin, OUTPUT);
_latchPin = latchPin;
pinMode(_latchPin, OUTPUT);
_numOfRegisterPins = numOfRegisters * 8;
//reset all register pins
clear();
writeValues();
}
//set all register pins to LOW
void Shiftduino::clear(){
for(int i = _numOfRegisterPins - 1; i >= 0; i--){
_registers[i] = LOW;
}
//set pins
writeValues();
}
//set an individual pin HIGH or LOW
void Shiftduino::setPin(int index, int value){
_registers[index] = value;
//set pins
writeValues();
}
//set an individual pin HIGH or LOW, specifiying the register number
void Shiftduino::setPin(int registerNum, int index, int value){
int finalIndex = (registerNum * 8) + index;
_registers[finalIndex] = value;
//set pins
writeValues();
}
//set all pins HIGH or LOW
void Shiftduino::setPins(boolean values[]){
for(int i = _numOfRegisterPins - 1; i >= 0; i--){
_registers[i] = values[i];
}
//set pins
writeValues();
}
//set all pins HIGH or LOW
void Shiftduino::setPins(uint64_t values){
for(int i = 0; i < _numOfRegisterPins; i++){
_registers[i] = values & 0x00000001;
values >>= 1;
}
//set pins
writeValues();
}
//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void Shiftduino::writeValues(){
digitalWrite(_latchPin, LOW);
for(int i = _numOfRegisterPins - 1; i >= 0; i--){
digitalWrite(_clockPin, LOW);
int value = _registers[i];
digitalWrite(_dataPin, value);
digitalWrite(_clockPin, HIGH);
}
digitalWrite(_latchPin, HIGH);
}