-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreading-TM1638-buttons-sample-code-2.txt
More file actions
68 lines (58 loc) · 3.34 KB
/
Copy pathreading-TM1638-buttons-sample-code-2.txt
File metadata and controls
68 lines (58 loc) · 3.34 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
//demonstration of button reads on the TM1638 LED & KEY board
const int strobePin = 8;
const int clockPin = 9;
const int dataPin = 10;
int keys1;
int keys2;
int keys3;
int keys4;
int LEDarray;
void setup()
{
pinMode(strobePin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
digitalWrite(strobePin, LOW); //set the strobe low so it'll accept instruction
shiftOut(dataPin, clockPin, LSBFIRST, 0x40); // set up sequential mode write
digitalWrite(strobePin, HIGH); //end of instruction, strobe must go high
digitalWrite(strobePin, LOW); //start of data for sequential write, strobe must go low
shiftOut(dataPin, clockPin, LSBFIRST, 0xC0); // sets up the first address
for (int displayAddress = 1; displayAddress < 17; displayAddress++) {
shiftOut(dataPin, clockPin, LSBFIRST, 0x00); // blanks out the display
}
digitalWrite(strobePin, HIGH); //end of writing to displays, so we set the strobe high again.
digitalWrite(strobePin, LOW); //set the strobe low so it'll accept instruction
shiftOut(dataPin, clockPin, LSBFIRST, 0x8F); // activate the display and set brightness to max
digitalWrite(strobePin, HIGH);
}
void loop()
{
digitalWrite(strobePin, LOW); //set the strobe low so it'll accept instruction
shiftOut(dataPin, clockPin, LSBFIRST, 0x42); // send the "read buttons" instruction
pinMode(dataPin, INPUT_PULLUP); //Instruction received, board changes DIO to OUTPUT, we now need to set the pin as INPUT
keys1 = shiftIn(dataPin, clockPin, LSBFIRST); // read in first byte of data
keys2 = shiftIn(dataPin, clockPin, LSBFIRST); // read in first byte of data
keys3 = shiftIn(dataPin, clockPin, LSBFIRST); // read in first byte of data
keys4 = shiftIn(dataPin, clockPin, LSBFIRST); // read in first byte of data
digitalWrite(strobePin, HIGH); // all data is received, and just like any other instruction, the strobe line must go high when finished
pinMode(dataPin, OUTPUT); //having sent all data, the TM1638 board now sets the DIO line as INPUT, we switch the arduino back to OUTPUT
bitWrite(LEDarray, 0, bitRead(keys1,0));
bitWrite(LEDarray, 1, bitRead(keys2,0));
bitWrite(LEDarray, 2, bitRead(keys3,0));
bitWrite(LEDarray, 3, bitRead(keys4,0));
bitWrite(LEDarray, 4, bitRead(keys1,4));
bitWrite(LEDarray, 5, bitRead(keys2,4));
bitWrite(LEDarray, 6, bitRead(keys3,4));
bitWrite(LEDarray, 7, bitRead(keys4,4));
//we'll now write to the 8 discrete LED's, checking individual bits in the LEDarray variable
digitalWrite(strobePin, LOW); //set strobe low for an instruction
shiftOut(dataPin, clockPin, LSBFIRST, 0x40); //set up sequential addressing
digitalWrite(strobePin, HIGH); //end of instruction, strobe must go high
digitalWrite(strobePin, LOW); //start of data, strobe must go low
shiftOut(dataPin, clockPin, LSBFIRST, 0xC0); // sets up the first address as the first discrete LED, 0001
for (int whichBit = 0; whichBit < 8; whichBit++) { //we'll read the 8 bits from the LEDarray variable
shiftOut(dataPin, clockPin, LSBFIRST, 0x00); // next display in the sequence will be a 7 segment, so we'll clear it.
shiftOut(dataPin, clockPin, LSBFIRST, bitRead(LEDarray, whichBit)); // writes to the discrete LED we're at;
}
digitalWrite(strobePin, HIGH); //we're done here, set the strobe high again.
}