Skip to content

Instantly share code, notes, and snippets.

@yassinagx
Created February 22, 2016 15:34
Show Gist options
  • Save yassinagx/750d6acd6782f8f026f0 to your computer and use it in GitHub Desktop.
Save yassinagx/750d6acd6782f8f026f0 to your computer and use it in GitHub Desktop.
#include <EEPROM.h>
//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 11;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 13;
//Pin connected to Data in (DS) of 74HC595
const int dataPin = 8;
//7SegLED
const int ledArray = 8;
const int EEPROMAddress = 0;
byte byteData[ledArray] = {0};
char strData[ledArray] = {0};
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop() {
unsigned long counter = EEPROMReadlong(EEPROMAddress);
counter++;
EEPROMWritelong(EEPROMAddress, counter);
sprintf(strData, "%ld", EEPROMReadlong(EEPROMAddress));
setSegmentFormat(strData, byteData);
digitalWrite(latchPin, LOW);
for(int i=ledArray-1; i>=0; i--){
shiftOut(dataPin, clockPin, MSBFIRST, byteData[i]);
}
digitalWrite(latchPin, HIGH);
}
void setSegmentFormat(char* strData,byte *byteData){
int numofNull=0;
for(int i=0; i<ledArray; i++){
switch(strData[i]){
case '\0':
byteData[i]=0xff;
numofNull++;
break;
case '0':
byteData[i]=0xc0;
break;
case '1':
byteData[i]=0xf9;
break;
case '2':
byteData[i]=0xa4;
break;
case '3':
byteData[i]=0xb0;
break;
case '4':
byteData[i]=0x99;
break;
case '5':
byteData[i]=0x92;
break;
case '6':
byteData[i]=0x82;
break;
case '7':
byteData[i]=0xf8;
break;
case '8':
byteData[i]=0x80;
break;
case '9':
byteData[i]=0x90;
break;
case '-':
byteData[i]=0xbf;
break;
}
}
for(int i=0; i<numofNull; i++){
byte tmp[ledArray];
int j;
for(int i =0; i<ledArray; i++){
tmp[i]=byteData[i];
}
for(int i =0; i<ledArray; i++){
if(i==0){
j = ledArray-1;
}else{
j = i-1;
}
byteData[i] = tmp[j];
}
}
}
void EEPROMWritelong(int address, long value)
{
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.write(address, four);
EEPROM.write(address + 1, three);
EEPROM.write(address + 2, two);
EEPROM.write(address + 3, one);
}
long EEPROMReadlong(long address)
{
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment