Last active
April 30, 2018 12:48
-
-
Save abdul-rehman-2050/41bc569e674cefe7df0e6cbfc06ac876 to your computer and use it in GitHub Desktop.
This is small Arduino code to drive non blocking TDM for Seven Segment multipexing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========================================================================================== | |
#define NUM_OF_DIGITS 3 | |
#define NUM_OF_SEGMENTS 7 | |
//-------------------------- | |
long preMicros=0,preMillis=0; | |
unsigned char digitBuffer[NUM_OF_DIGITS]={0}; | |
unsigned char segCommons[]={A5,A4,A3}; | |
unsigned char segData[]={6,7,8,9,10,11,12}; | |
unsigned char segFont[]={ | |
0b00111111, //0 | |
0b00000110, //1 | |
0b01011011, //2 | |
0b01001111, //3 | |
0b01100110, //4 | |
0b01101101, //5 | |
0b01111101, //6 | |
0b00000111, //7 | |
0b01111111, //8 | |
0b01101111 //9 | |
}; | |
//---------------------------------------------------------------------------------- | |
void outputSingleDigit(unsigned char digit){ | |
int i=0; | |
for(i=0;i<NUM_OF_SEGMENTS;i++){ | |
digitalWrite(segData[i],bitRead(digit,i)); | |
} | |
} | |
//---------------------------------------------------------------------------------- | |
void offAllSegments(){ | |
int i=0; | |
for(i=0;i<NUM_OF_DIGITS;i++){ | |
digitalWrite(segCommons[i],HIGH); | |
} | |
} | |
void sevenSegmentsTDM(){ | |
static unsigned char current_segment_to_serve=0; | |
static unsigned char turn_number=0; | |
const unsigned char MAX_TURNS = 6; | |
//-------------------------------------------------------------- | |
// Serve Next Seven Segment | |
//------------------------------------------------------------- | |
if((turn_number%2)==0){ | |
offAllSegments(); | |
}else if(turn_number>0){ | |
outputSingleDigit(segFont[digitBuffer[current_segment_to_serve]]); | |
digitalWrite(segCommons[current_segment_to_serve],LOW); | |
current_segment_to_serve++; | |
if(current_segment_to_serve>=NUM_OF_DIGITS){ | |
current_segment_to_serve=0; | |
} | |
} | |
//------------------------------------------------------------- | |
//-------------------------------- | |
//finally update the turn number | |
//-------------------------------- | |
turn_number++; | |
if(turn_number>MAX_TURNS){ | |
turn_number=1; | |
} | |
//--------------------------------- | |
}//function ends here | |
//================================================================================== | |
void setup() { | |
char i=0; | |
for(i=0;i<NUM_OF_SEGMENTS;i++){ | |
pinMode(segData[i],OUTPUT); | |
} | |
for(i=0;i<NUM_OF_DIGITS;i++){ | |
pinMode(segCommons[i],OUTPUT); | |
} | |
} | |
int cnt=0; | |
void loop() { | |
//------------------------------------------- | |
if((millis()-preMillis) > 250){ | |
//2 seconds passed | |
preMillis=millis(); | |
//------------------- | |
cnt++; | |
if(cnt>999)cnt=0; | |
digitBuffer[0]=cnt%10; | |
digitBuffer[1]=(cnt/10)%10; | |
digitBuffer[2]=(cnt/100)%10; | |
} | |
//-------------------------------------------- | |
if((micros()-preMicros)>2500){ | |
preMicros=micros(); | |
sevenSegmentsTDM(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment