Electronic – Using interrupts to strobe an LED

cembeddedmicrocontrollermicroprocessorpic

Beginner here, just playing around with a devboard using a PIC micro. I am attempting to strobe an LED using an interrupt, I've created the function below within isr.c, INTOSC is running at 4MHz, TMR0 at 1MHz with a 1:128 prescaler. Variables second and mSecond are defined as 100 in my initialize function; ledOnTime and ledOffTime are initialized at 0 in the same function; GP2 and GP4 are the pins tied to my LEDs as you can see from my comments.

First off, I've adjusted the timing of my interrupt occurrence based on my scope readings, the math I did beforehand had it happening too frequently so I slowed it down to where my pulses matched what I was looking for. I did the math backwards, and it still doesn't really add up in my mind. Maybe someone can give me a clue as to why.

Another thing I can't figure out is why when I increase either one of the the variables (second or mSecond) it increases the time in which my LEDs stay off. What I'm trying to increase is the time they stay on. I would think based on the way I wrote it mSecond should do that, but from my results upon programming the micro it does not, and it has me baffled. Can someone please explain what's going on here:

if (TMR0IF){
    TMR0 += 162;                       // overflow 93 (at approx every 10 millisecs)
    TMR0IF = 0;                        // reset overflow flag

    if (ledOffTime == second){         // every second
        ledOffTime = 0;
        GP2 = 1;                       // turn lights on
        GP4 = 0;                       // bicolor led on
        if(ledOnTime == mSecond){      // every 100 millisecs
            ledOnTime = 0;             // reset on time count
            GP2 = 0;                   // turn lights off
            GP4 = 1;                   // bicolor led off
        }
        else{
            ledOnTime++;               // keep LEDs ON
        }
    }
    else{
        ledOffTime++;                  // keep LEDs OFF
    }
    system_ok = 1;
}

Found a solution, some variables and config registers changed:

  • TMR0 prescaler 1:64
  • SECOND = 440
  • MSECOND = 40

    if (TMR0IF){
    TMR0 += 217;                         // approx 2.5 msecs
    TMR0IF = 0;                          //reset overflow flag
    
    if (++ledOffTime >= SECOND){         // Every second 
        ledOffTime = 0;                  // Clear off time count
        ledsOn = true;                   // LEDs are on
        GP4 = 0;                         // Bicolor LED
        GP2 = 0;                         // Turn LEDs on
    }
    if(ledsOn == true){                  
        if(++ledOnTime > MSECOND){   
            ledOnTime = 0;              // Reset on time count
            ledsOn = false;             // LEDs are off
            GP4 = 1;                    // Bicolor LED
            GP2 = 1;                    // LEDs are off
        }
    }    
    

    }

Best Answer

Found a solution, some variables and config registers changed:

TMR0 prescaler 1:64 SECOND = 440 MSECOND = 40

if (TMR0IF){
TMR0 += 217;                         // approx 2.5 msecs
TMR0IF = 0;                          //reset overflow flag

    if (++ledOffTime >= SECOND){         // Every second 
        ledOffTime = 0;                  // Clear off time count
        ledsOn = true;                   // LEDs are on
        GP4 = 0;                         // Bicolor LED
        GP2 = 0;                         // Turn LEDs on
    }
    if(ledsOn == true){                  
        if(++ledOnTime > MSECOND){   
            ledOnTime = 0;              // Reset on time count
            ledsOn = false;             // LEDs are off
            GP4 = 1;                    // Bicolor LED
            GP2 = 1;                    // LEDs are off
        }
    }    
}
Related Topic