Intro To Digital Timers:

Program Download:

Main C File
timer_main.c

The Software
           The software for this tutorial sets up 4 different timers and each timer is unique from the others. Some have different prescalars and others have different bit widths (8-Bit/16-Bit).
           Since the software for this project won't fit all on one page I'll explain these three elements and how they're implemented. If you have any lasting questions about the software just go to the forums and ask! The program is heavily commented so it should be readable.

------------« Begin Code »------------
void main(void)
{

TRISB = 0x00;
PORTB = 0x00;

OpenTimer0(TIMER_INT_OFF & T0_16BIT
          & T0_SOURCE_INT & T0_PS_1_1);
OpenTimer1(TIMER_INT_OFF & T1_16BIT_RW
          & T1_SOURCE_INT & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF);
OpenTimer2(TIMER_INT_OFF & T2_PS_1_16
          & T2_POST_1_1);
OpenTimer3(TIMER_INT_OFF & T3_8BIT_RW
          & T3_SOURCE_INT & T3_PS_1_1 & T3_OSC1EN_OFF
          & T3_SYNC_EXT_OFF);

WriteTimer0(0x0000);
WriteTimer1(0x0000);
WriteTimer2(0x00);
WriteTimer3(0x00);

while(1){
     if(ReadTimer0() >= 0xFFF0){
          PORTBbits.RB1 = ~PORTBbits.RB1;
          WriteTimer0(0x0000);
     }
     if(ReadTimer1() >= 0xFFF0){
          PORTBbits.RB3 = ~PORTBbits.RB3;
          WriteTimer1(0x0000);
     }
     if(ReadTimer2() >= 0xF0){
          PORTBbits.RB5 = ~PORTBbits.RB5;
          WriteTimer2(0x00);
     }
     if(ReadTimer3() >= 0xF0){
          PORTBbits.RB7 = ~PORTBbits.RB7;
          WriteTimer3(0x00);
     }
}
}
------------« End Code »------------
Timer0 & Timer1
           So it was actually a lie to say all timers would be unqiue. Both Timer0 & Timer1 are set to 16-Bit width and have a prescalar of 1 to 1. This means every instruction cycle is counted and anytime a value equal to or larger than 0xFFF0 is read the counter resets & changes the LED state.

Timer2 & Timer3
           These timers are both in 8-bit mode. One has a prescalar which will make it count faster the other has a standard 1 to 1 prescalar. In any case, with a 20 MHz clock that does an instruction in 1/5,000,000 of a second 256 instructions (8-Bit) happens rather fast and is not noticeable. While timers 2 & 3 may be bad at blinking leds they are very useful for satisfying faster & higher frequency requirements.



;