ECS Lab 1: Timer Basics

Remarks : Timer modules in most MCU products are considerably more sophisticated than the core in Figure 2. They may add fancy mechanisms such as external input clock selection, special event trigger, etc. Consult the datasheet of the MCU of your choice.

Example 1: A fictitious MCU runs on 80 MHz clock and has a 16-bit timer. The available 4 choices of prescaler are 1:1, 1:4, 1:16, 1:256; i.e., the input clock frequency to the timer unit can be divided by 1, 4, 16, 256. An engineer wants to implement a process control with sampling period of 10 millisecond.

To initialize the timer, she has to select the prescaler and compute a value to load to period register. She first selects 1:1 prescaler. The period of one clock cycle is 1/80 = 0.0125 microseconds. The maximum value for 16-bit timer is 2^16 – 1 = 65535. So the longest period for this configuration is 0.0125 x 65535 = 819.2 microseconds. This period is shorter than the desired sampling period. So the prescaler has to be adjusted.

Suppose she changes the prescaler to 1:16. The period of prescaled clock cycle is 0.0125 x 16 = 0.2 microseconds. So the value to load to PR is 10000/0.2 = 50000.

Exercise: Compute the PR value if the prescaler 1:256 is selected. (Ans: 3125)

Now we are ready to fool around a bit with timers. All needed are an MCU with 3 LED’s and 3 switches. Microchip’s PIC32 Starter Kit is quite handy for this experiment. Feel free to adjust the code for your choice of MCUs/development tools.

Lab instruction: design a simple multi-tasking scheme with 3 naïve tasks: to blink 3 LED’s at different frequencies using the infinite main loop together with 2 timers. There are also 3 PB switches, one to stop/resume execution of each task. When switch 1 is pushed, the main LED stops blinking. Switch 2 and 3 correspond to LED’s attached to the two timers.

Listing 1 shows the C program for this lab. The program flow is not difficult to understand. Notice the ISR definition for timer 1 and 2 above the main( ) function. For this lab their jobs are only to blink LED’s. For your application, this is where you put your discrete-time controller(s).

The two timers are initialized in main( ). The registers name here are specific to PIC32 but the steps are common.

  1. Select the prescalers by setting bit 5,4 of register T1CON and bit 6,5,4 of T2CON.
  2. The periods are specified by loading values to PR1 and PR2.
  3. Set priorities for both timers .
  4. Enable timer interrupts.

See [2] for more details on PIC32 timers.

You can download MPLABX project from the link below and play with it. We leave it as an exercise for you to set some desired periods for the timers, say, T1 = 0.5 second and T2 = 1 second.

Hint: simply compute the right values to load into PR1 and PR2 (as in Example 1 above). Notice that the clock to PIC32 timers is pbClk (peripheral bus clock), not FCY. Since the global variable pbclk is there for you in the program, it is convenient to add some lines of code to compute values for PR1 and PR2 automatically.



Comments are closed.