KITT and the Cylon Eye are very popular among those tutorials on Shift Registers.
This tutorial should cover an Arduino vs AVR – This is going to suck for a tutorial. I wanted to go over some of the changes that I made to some code based off of the AVR.
Let’s start with the AVR – As the AVR is setup it has one individual wire going from each pin on Port B 0-7. You can see the bitshifting below.
AVR CODE:
#include /* Defines pins, ports, etc */
#include /* Functions to waste time */
#define DELAYTIME 15 /* milliseconds */
int main(void) {
uint8_t i = 0;
DDRB = 0b11111111; /* Data Direction Register B: all on */
while (1) {
toTheRight(&i);
toTheLeft(&i);
}
return (0);
}
void toTheRight(uint8_t *p) {
uint8_t i = *p;
while (i < 7) {
PORTB = (1 << i); /* illuminate only i'th pin */
_delay_ms(DELAYTIME); /* wait */
i++; /* move to the next LED */
}
*p = i;
}
void toTheLeft(uint8_t *p) {
uint8_t i = *p;
while (i > 0) {
PORTB = (1 << i); /* illuminate only i'th pin */
_delay_ms(DELAYTIME); /* wait */
i--; /* move to the previous LED */
}
*p = i;
}
Here’s the Arduino plugged in with the Shift Register (I don’t have the AVR code for this same thing, but it should be quite similar).
I pulled the bottom code from: http://www.instructables.com/id/8-LED-Chaser-with-74HC595-8-Bit-Shift-Register/?ALLSTEPS
You’ll notice the patterns that are on lines 5-19; I don’t like that. You’re defining something that you should be able to do the same as the AVR. Now, if I make a selection (say 5) and I want that specific display to show up, this is ok, but could still be done with the bitshifting. If you run this example you can remove lines 5-20, 37, 44, 56-64.
On line 38 we shift the bits to the left by one to produce the pattern on line 6-13. On line 45 we shift the bits again by one to the right to produce the pattern on lines 14-19. Then the updateShiftRegister method, lines 51-55, we open the latch on the shift register, then shiftOut our data to the register, then close the latch.
If you compare the AVR code above with this Arduino code at the bottom you should see a lot of similarities – since the AVR code doesn’t use a shift register you won’t see the latch open, out, and close.
1: //http://www.instructables.com/id/8-LED-Chaser-with-74HC595-8-Bit-Shift-Register/?ALLSTEPS
2: int clockPin = 3;//IC Pin 11, Yellow Jumper
3: int dataPin = 2;//IC Pin 14, Blue Jumper
4: int latchPin = 4;//IC Pin 12, Green Jumper
5: byte patterns[30] = {
6: B00000001, 100,
7: B00000010, 100,
8: B00000100, 100,
9: B00001000, 100,
10: B00010000, 100,
11: B00100000, 100,
12: B01000000, 100,
13: B10000000, 100,
14: B01000000, 100,
15: B00100000, 100,
16: B00010000, 100,
17: B00001000, 100,
18: B00000100, 100,
19: B00000010, 100
20: };
21: int index = 0;
22: int count = sizeof(patterns) / 2;
23: byte led = 0;
24: void setup() {
25: Serial.begin (9600);
26: pinMode(latchPin, OUTPUT);
27: pinMode(clockPin, OUTPUT);
28: pinMode(dataPin, OUTPUT);
29: pinMode(13, OUTPUT);
30: }
31: void loop() {
32: cylonEyeNew();
33: }
34: void cylonEyeNew() {
35: int i = 0;
36: while (i < 8) {
37: // bitSet(led, i);
38: led = (1 << i);
39: updateShiftRegister();
40: delay(100);
41: i++;
42: }
43: while (i > 0) {
44: // bitSet(led, i);
45: led = (1 << i);
46: updateShiftRegister();
47: delay(100);
48: i--;
49: }
50: }
51: void updateShiftRegister() {
52: digitalWrite(latchPin, LOW);
53: shiftOut(dataPin, clockPin, LSBFIRST, led);
54: digitalWrite(latchPin, HIGH);
55: }
56: void cylonEyeOriginal() {
57: digitalWrite(latchPin, LOW);
58: shiftOut(dataPin, clockPin, MSBFIRST, patterns[index * 2]);
59: digitalWrite(latchPin, HIGH);
60: delay(patterns[(index * 2) + 1]);
61: index++;
62: if (index >= count){
63: index = 0;
64: }
65: }
Without line numbers and without original instructables code:
int clockPin = 3;
int dataPin = 2;
int latchPin = 4;
int index = 0;
int count = sizeof(patterns) / 2;
byte led = 0;
void setup() {
Serial.begin (9600);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
cylonEyeNew();
}
void cylonEyeNew() {
int i = 0;
while (i < 8) {
led = (1 << i);
updateShiftRegister();
delay(100);
i++;
}
while (i > 0) {
led = (1 << i);
updateShiftRegister();
delay(100);
i--;
}
}
void updateShiftRegister() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, led);
digitalWrite(latchPin, HIGH);
}