Maker Pro
Maker Pro

PIC18f45k22 timer 1 not working properly

electronika.design

Feb 25, 2016
9
Joined
Feb 25, 2016
Messages
9
I am using PIC18f45k22's timer 1 to display D.PIC for 1 second on my 4 digit 7-segment led and after 1 second led should remain off for next 1 second.
This should continue. I am getting what i wrote above but the problem is that when D.PIC is displayed, it is continuously flashing. I want it to remain constant. I have posted my code below.I am using 8Mhz Freq.

unsigned short i=1,cnt,digit_no=0,DISPLAY[4]={0,0,0,0};

void interrupt() {
if (TMR1IF_bit) {
cnt++;

if(cnt<=8){
switch (digit_no){
case 0:{
PORTB=0;
PORTD= DISPLAY[0];
PORTB = 0b00000001;
digit_no=1;
break;
}
case 1:{
PORTB=0;
PORTD = DISPLAY[1];
PORTB = 0b00000010;
digit_no=2;
break;
}
case 2: {
PORTB=0;
PORTD = DISPLAY[2];
PORTB = 0b00000100;
digit_no=3;
break;
}
case 3:{
PORTB=0;
PORTD = DISPLAY[3];
PORTB = 0b00001000;
digit_no=0;
break;
}
}
}
else if((cnt>8)&&(cnt<16)){
PORTB=0;
}
else if(cnt>16)
cnt=0;
TMR1IF_bit = 0;
TMR1H = 0x00;
TMR1L = 0x00;
}
}

void main() {

LATB = 0; // Initialize PORTB
TRISB = 0; // PORTB is output
LATD = 0; // Initialize PORTD
TRISD = 0; // PORTD is output
T1CON = 0x21; // Timer1 settings //1:4 Prescaler
TMR1IF_bit = 0; // clear TMR1IF
TMR1H = 0x0; // Initialize Timer1 register
TMR1L = 0x00;
TMR1IE_bit = 1; // enable Timer1 interrup
cnt = 0; // initialize cnt
INTCON = 0xC0; // Set GIE, PEIE

do {
DISPLAY[0] = 57; // C
DISPLAY[1] = 6; // I
DISPLAY[2] = 115; // P
DISPLAY[3] = 249; // D.
} while (1);
}
 

Amar Dhore

Dec 2, 2015
129
Joined
Dec 2, 2015
Messages
129
Top