Hardware Timer

This project uses a hardware timer (see Timer1 Operation) to flash LED1 on and off. For more details on how to control the LED see Parallel I/O (PIO) Ports.

Project files: timer3.zip.

timer3.c


01: /* timer3.c
02:  *
03:  *  Use timer1 to blink an LED
04: */
05: 
06: #include <plib.h>
07: 
08: void wait();
09: 
10: #define LED1 BIT_0  // LED1 is connected to RD0
11: 
12: 
13: int main()
14: {
15:         int i;
16:         mPORTDSetPinsDigitalOut(LED1);   /* Make LED1 output */
17:         mPORTDClearBits(LED1);      /* Turn off LED1 on startup */
18:         while (1) {
19:                 mPORTDToggleBits(LED1);     /* Toggle LED1 */
20:                 wait();
21:         }
22:         return 0;
23: }
24: 
25: void wait()
26: {
27:         const int DLY = 36000;
28:         T1CON = 0x30; // turn timer off and set prescaller to 1:256
29:         TMR1 = 0;
30:         PR1 = 0xFFFF;
31:         T1CONSET = 0x8000; // start timer
32:         while (TMR1 < DLY) {
33:                 // just wait
34:         }
35:         T1CONCLR = 0x8000; // stop timer
36: }


Results

I timed my LED over ten cycles and got 18.37 seconds. According to my calculation that is equivalent to a PB clock rate of 10.03 MHz.

Exercise

Run the project and repeat the measurement of the cycle time. Calculate the PB clock rate (show your work).


Maintained by John Loomis, updated Sat Aug 02 16:35:37 2008