Here is the code to multiplex 4 digit 7 segment on Atmega8 using Timer 2 Interrupt. This code witll display digits array on seven segments. In our case we used 3024 to display on 7 segments.
#define segA 5
#define segB 6
#define segC 7
#define segD 8
#define segE 9
#define segF 10
#define segG 11
char disps[]={A0,A1,A2,A3};
char digits[]={4,2,0,3};
void digitMap(int val){
int i=0;
for(i=5;i<12;i++){
digitalWrite(i,LOW);
}
switch(val){
case 0:
digitalWrite(segA,HIGH);
digitalWrite(segB,HIGH);
digitalWrite(segC,HIGH);
digitalWrite(segD,HIGH);
digitalWrite(segE,HIGH);
digitalWrite(segF,HIGH);
break;
case 1:
digitalWrite(segB,HIGH);
digitalWrite(segC,HIGH);
break;
case 2:
digitalWrite(segA,HIGH);
digitalWrite(segB,HIGH);
digitalWrite(segD,HIGH);
digitalWrite(segE,HIGH);
digitalWrite(segG,HIGH);
break;
case 3:
digitalWrite(segA,HIGH);
digitalWrite(segB,HIGH);
digitalWrite(segC,HIGH);
digitalWrite(segD,HIGH);
digitalWrite(segG,HIGH);
break;
case 4:
digitalWrite(segB,HIGH);
digitalWrite(segC,HIGH);
digitalWrite(segF,HIGH);
digitalWrite(segG,HIGH);
break;
default:
digitalWrite(segG,HIGH);
break;
}
}
void setup(){
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(13,OUTPUT);
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(A3,OUTPUT);
digitalWrite(13,HIGH);
OCR2 = 125;
TCCR2 |= (1 << WGM21);
TIMSK |= (1 << OCIE2);
TCCR2 |= (1 << CS22);
sei();
}
void offAllDisps(){
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
}
void loop(){
/*int i=0;
for(i=0;i<4;i++)
{
digitMap(digits[i]);
digitalWrite(disps[i],HIGH);
delay(1);
offAllDisps();
delay(3);
}
*/
}
volatile char disp_di = 0, ms_count=0;
ISR (TIMER2_COMP_vect)
{
ms_count++;
if(ms_count>3)ms_count=0;
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
if(ms_count<2){
digitMap(digits[disp_di]);
digitalWrite(disps[disp_di],HIGH);
disp_di++;
if(disp_di>3)disp_di=0;
}
}
Code language: PHP (php)