UPC EETAC Bachelor's Degree in Telecommunications Systems and in Network Engineering EEL


P12: peripheral TMR0. Adapting dedicated processors

Low Power

Resources in lectures and labs: L12, Lab11 Project objectives

Highlighted project: 6-bit Johnson sequencer with TMR0 or TMR2 (phase #3)

1. Specifications

Phase #3: Enhance project Johnson_sequencer_mod12_LCD phase #2 using TMR0 peripheral to replace external CLK functionality.

  • Let us running the sequencer by means of interrupts from internal timer peripheral TMR0.

  •  The sequencer has to advance at a rate of 10 counts per second, thus the timer will generate an interrupt every 100 ms. This means that SQ_out has a period of 1.2 s (frequency of 8.33 Hz).

  • Add TMR_wave output to show on the oscilloscope the waveform generated by TMR0 toggling logic value every TMR interrupt (var_CLK_flag signal).

Sequencer LCD

Fig 1. Project symbol and I/O signals (Visio).


Phase #4: As a final improvement, replace TMR0 by TMR2 and compare solutions and discuss performance. Why TMR2 is better for precision timing applications?

Other similar projects: In order to study how TMR0 replaces functionality supplied by external circuits, let us invent a basic timer that like other examples is organised in several consecutive design phases: 

- Fixed-time (18.5 s) timer. This circuit is covered in Lab11.

Phase #1: Timer using exernal CLK (50 Hz, 20 ms time-base).

NOTE: Be aware that a timer circuit is a dedicated processor application conceived in P8  with this specific architecture: datapath + FSM + CLK_Generator. Thus, here in Chapter we will adapt it to C language.

Phase #2:  Timer_LCD  that uses a library of LCD functions to print ASCII messages.

Phase #3: Timer_LCD_TMR0 using internal TMR0 1 μs time-base to replace external time-base.   

Phase #4: Timer_LCD_TMR2 using internal TMR2 1 μs time-base to replace TMR0 for better accuracy.


- Serial_transmitter_LCD_TMR0, a new future development enhancing P11: TMR0 replaces the external baud generator.


2. Planning

Hardware circuit updated is represented in Fig. 2.


Fig 2. Circuit sketch.


Software. We pretend to generate var_CLK_flag using internal TMR0. Therefore, the project has the same conception and only minor changes are necessary. In set_CLK_source state TMR0 will be selected and set as the source for sequence timing. In stop_CLK TMR will be switch off. 


Fig. 3 shows how the hardware-software diagram is modified.


Fig 3. Software-hardware diagram when using TMR0.


 In this application TMR0 must be a timer derived from the internal FOSC/4 system time-base. Thus, T0CS = 0.

TMR0 configuration

Fig 4. Functional structure and configuration bits in TMR0.

To obtain a var_CLK_flag signal every TP = 100 ms, a convenient set of parametres may be: N1 = 32, N2 = 125,  N3 = 25 (char variable). It means a hardware interrupt TMR0IF every 4 ms.


Fig 5. Algorithm in ISR() to generate var_TMR0_flag from TMR0 interrupts.

Project locations for Phase #3 are:



3. Development

Circuit captured in Proteus is Johnson_sequencer_mod12_LCD_TMR.pdsprj. Source file  Johnson_sequencer_mod12_LCD_TMR.c and LCD libraries must be compiled in the same MPLABX project for the target PIC18F4520 microcontroller.


Fig. 5. Circuit captured in Proteus. External CLK is deleted and the functionality replaced by TMR0.


4. Testing 



Fig. 6. Circuit running and watching variables and oscilloscope waveforms.

Measure the period of the output TMR_wave.

Why using the calculated values the time elapsed between TMR0 interrupts is 104.52 us instead of 100 ms ?

What is the meaning of software overhead? How to solve it? Try to replace TMR0 by TMR2 and repead measurements.

5. Report


6. Prototyping

Download the application to a training board an verify that it works as expected and the same as in the simulator.


Other similar projects on sequential circuits  

The same example Timer XLCD using extended libraries for the LCD. Find what eXtended libraries are required for commanding the TMR0 replacing low-level code in  previous examples.



"Education is really aimed at helping students get to the point where they can learn on their own. . . "

Noam Chomsky