Assume that you have the following 4-digit cc-type 7-segment Display Unit (Fig-1) and you want to show 3457 over there.
Figure-1:
Given:
int y = 3457;
1. Perform %10 (modulus) operation on y to get remainder 7.
byte indexDP3 = y%10; // indexDP3 = 7
2. Divide y by 10 to get quotient 345.
y = y/10; //y = 345
Repeat the above process until quotient is 0.
3. Sketch to find 3 4 5 7 and save in an array.
int y = 3457;
byte indexDP[4];
int i = 3;
void setup()
{
Serial.begin(9600);
do
{
indexDP[i] = y%10; //indexDP[3] = 7, indexDP[2] = 5, ....
y = y/10;
i--;
}
while(y !=0);
for(int j = 0; j < 4; j++)
{
Serial.print(indexDP[j], HEX);
Serial.print(' ');
}
}
void loop()
{
}
Output:
3 4 5 7
4. Use the value of indexDP[0] (3 = 0x03) to consult the following array (which contains cc-codes vs digit) to get cc-code of 3 and show it on DP0 position of the display unit.
byte lupTable[] =
{
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, //0, 1, 2, 3, 4, 5, 6, 7
0x7F, 0x6F, 9x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 //8. 9, A, b, C, d, E, F
};
Codes:
byte ccDP0 = lupTable(indexDP[0]); //ccDP0 = 0x4F ;; cc-code of 3
PORTB = ccDP0; //lower 6-bit goes to Display unit for segments: f, ..., a
digitalWrite(6, bitRead(ccDP0, 6)); bit-6 of ccDP0 goes to segment g of display unit
digitalWrite(7, bitRead(ccDP0, 7)); bit-67 of ccDP0 goes to segment p of display unit
PORTC = 0b111110; //3 appears on DP0 position of display unit;
delay(10);
5. If you use SevSeg.h Library, the tasks of Step-3/4 will be done by the Library. You may study the example of this thread.