Ok, with my success with Shift Registers and LEDs, it was time to step it up a notch.
I purchased a 7 segment LED (cost about $6). Decided to treat myself to a nice blue one.
Each of the segments on these is effectively a separate LED and with what I learned about shift registers Ive decided to try and create numbers on the segment LEDs.
I'll make a simple project to make the single digit could from 0 to 9 and repeat, with an adjustable software based timer to modify the speed. My knowledge in binary to decimal conversion was required here, as I somewhat randomly wired each segment and then had to deduce which was which from a circuit point of view (a bit backwards - but it worked). Robbie can count ! ... See below
See the photo below.
Initially I used a great big piece of messy code, but decided to switch to an array (which saved alot of valuable memory space on the micro controller.
Here is the code I wrote. To make it run.
//This code uses the 7 segment LED in combination with
//the ShiftRegiter IC Chip - this using only 3 outputs !
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
int mydelay = 2000; //Timer
int ledconfig ;
int ConfigArray[] = {18 ,185 ,179 ,210 ,227 ,235 ,114 ,251 ,243 ,123}; //1,2,3,4,5,6,7,8,9,0
void setup() {
Serial.begin(9600); // In case debugging is needed
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop()
{
for (int ctr = 0; ctr < 10; ctr++) {
ledconfig = ConfigArray[ctr];
//Serial.println(ledconfig);
updateled();
}
}
void updateled()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, ledconfig);
digitalWrite(latchPin, HIGH);
delay(mydelay);
}
//Below is the BIT Math if needed to switch binary bits up and down (HIGH/LOW)
// 0 & 0 == 0
// 0 & 1 == 0
// 1 & 0 == 0
// 1 & 1 == 1
//int c = numberToDisplay & B00111100;
// 0 | 0 == 0
// 0 | 1 == 1
// 1 | 0 == 1
// 1 | 1 == 1
// int c = numberToDisplay | B01111100;
// 0 ^ 0 == 0
// 0 ^ 1 == 1
// 1 ^ 0 == 1
// 1 ^ 1 == 0
// int c = numberToDisplay ^ B10000010;
// int c = ~numberToDisplay; // the ~ (NOT) will switch all bits to opposite state
// int c = numberToDisplay << 3; // move all bits 3 places to the left
// int c = numberToDisplay >> 3; // move all bits 3 places to the left
//Based on my wiring of the control pins (ll those other then 3 & 8), below is the binary for each segment.
//
//B00100000;//A position segment on my 7 desgment LED (based on the way i wired it!) DEC 32
//B00010000;//B position segment on my 7 desgment LED (based on the way i wired it!) DEC 16
//B00000010;//C position segment on my 7 desgment LED (based on the way i wired it!) DEC 2
//B00000001;//D position segment on my 7 desgment LED (based on the way i wired it!) DEC 1
//B00001000;//E position segment on my 7 desgment LED (based on the way i wired it!) DEC 8
//B01000000;//F position segment on my 7 desgment LED (based on the way i wired it!) DEC 64
//B10000000;//G position segment on my 7 desgment LED (based on the way i wired it!) DEC 128
//B00000100;//Dot position segment on my 7 desgment LED (based on the way i wired it!) DEC 4

No comments:
Post a Comment