PIC18F45K22 Timer 1 not working properly...

Thread Starter

Paras77

Joined Mar 4, 2016
12
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.
C:
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);
}
Mod edit: added CODE tags
 
Last edited by a moderator:

AlbertHall

Joined Jun 4, 2014
12,345
During the display on time, each digit is displayed for 1/8 of a second and is then off for 3/8 second. This will be very noticeable.
Use a shorter period for timer 1 (perhaps load timer 1 with some value rather than than setting to zero), then modify the testing of cnt. Now each digit is still on for only 1/4 of the time but the cycling is much quicker so persistence of vision means that the eye sees all digits lit.
 

AlbertHall

Joined Jun 4, 2014
12,345
Your timer interrupt occurs every 1/8 of a second. During the first second, when the display is on, each timer interrupt displays the next digit and as there are four digits, each digit is on for 1/8 second and off for the 3/8 of a second while the other three digits are displayed in turn.
 

GopherT

Joined Nov 23, 2012
8,009
Put your code inside '['code] '['/code] tags so we can see the formatting.

Do NOT use the quote marks when you use code tags. I needed them or they would disappear as they try to display code.
 
Top