-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTPIC6B595.cpp
65 lines (56 loc) · 1.08 KB
/
TPIC6B595.cpp
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
#include "config.h"
#include "TPIC6B595.h"
#define GATE_DELAY 20
#if USE_HARD_SPI
#include <SPI.h>
#endif
TPIC6B595::TPIC6B595(int dataPin, int clockPin, int latchPin, int clearPin)
{
_dataPin = dataPin;
_clockPin = clockPin;
_latchPin = latchPin;
_clearPin = clearPin;
}
void TPIC6B595::begin()
{
#if USE_HARD_SPI
SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV2);
SPI.setBitOrder(LSBFIRST);
#else
pinMode(_dataPin, OUTPUT);
pinMode(_clockPin,OUTPUT);
#endif
pinMode(_latchPin,OUTPUT);
pinMode(_clearPin,OUTPUT);
}
void TPIC6B595::write(byte bits)
{
#if USE_HARD_SPI
SPI.transfer(bits);
#else
shiftOut(_dataPin, _clockPin, LSBFIRST, bits);
#endif
pulsePin(_latchPin, HIGH);
}
void TPIC6B595::clear()
{
pulsePin(_clearPin, LOW);
pulsePin(_latchPin, HIGH);
}
void TPIC6B595::pulsePin(int pin, int active)
{
if (active == HIGH)
{
digitalWrite(pin, HIGH);
delayMicroseconds(GATE_DELAY);
digitalWrite(pin, LOW);
}
else
{
digitalWrite(pin, LOW);
delayMicroseconds(GATE_DELAY);
digitalWrite(pin, HIGH);
}
}