P12: peripheral TMR0. Adapting dedicated processors |
Resources in lectures and labs: | L12.1, Lab11, L12.2, L12.3 | Project | objectives |
Highlighted project: 4-bit serial transmitter with TMR0 (design phase #3)
or TMR2 (design phase #4)
1. Specifications | Planning | Dev. & test | Prototype | Report |
Serial transmitter design phase #3: Serial_transmitter_LCD_TMR0.
Let us enhance the previous Serial_transmitter_LCD (design phase #2) replacing the external baud generator by an internal timer peripheral such TMR0. In this way the application can reduce board space and component count and also reduce power consumption.
Let us generate the baud rate timing using interrupts from internal timer peripheral TMR0.
Modify the hardware and delete the external CLK input.
Program the application configuring the TMR0 to generate the same var_CLK_flag variable.
![]() |
Fig. 1. Project serial_transmitter_LCD_TMR0 symbol. |
Timing diagram representing bit duration Tbit and the idea of starting to send data immediately after having detected the start transmission (ST) rising edge.
![]() |
Fig. 2. Waveforms representing Din = "1001" and the bit duration time Tbit. |
Other design tutorials and assignments.
Specifications | 2. Planning | Dev. & test | Prototype | Report |
The circuit is represented in Fig. 3. It is the same as it was in Serial_transmitter_LCD without the external CLK oscillator for generating the bit rate.
![]() |
Fig. 3. Hardware circuit. |
Let us study the hardware of the TMR0 peripheral interpreting the datasheet diagram and configuration registers.
![]() |
Fig. 3. TMR0 hardware in 8-bit mode from the PIC18F4520 datasheet. Study the TMR0 registers and configuration bits. |
We intent to generate var_CLK_flag using internal TMR0. Therefore, the project has the same conception and only minor changes are necessary. When ST interrupt is detected, TMR0 will be switched on, configured and set as the bit time Tbit source for transmitting bits. In the last state END_message TMR0 will be switch off because it is not required.
![]() |
Fig. 5. Software-hardware diagram when using TMR0 to generate var_CLK_flag. |
Design step #1. One idea is to use TMR0 in 8-bit mode. Infer N1, N2 and N3 values required in this application for generating a timing period TP = 6.66666667 ms = Tbit equivalent to 150 b/s or the nearest value.
![]() |
Fig. 6. TMR0 in 8-bit mode for generating a timing period of TP = 6.666 ms. |
The list of RAM variables required are shown in Fig. 7.
![]() |
Fig. 6. RAM variables. Selected values allows generating 149.88 Hz (0.08% error). |
Design step #2. Is it possible to obtain more timing precision?. An alternative idea is to use the TMR0 in 16-bit mode, hence, for timing TP = 6.666 ms is not necessary to use neither the prescaler or the software postscaler, resulting in a simpler configuration because we can program var_TMR0_TC_N2 = 6666 obtaining even more precision.
![]() |
Fig. 7. TMR0 in 16-bit mode for generating a timing period of TP = 6.666 ms. In this case, the bit rate is 150.015 b/s, (error 0.01%). However, instruction execution time imposes a time overhead that do not allow to reach such precision. An alternative is to use the TMR2 to generate higher time precision due to an improved circuit with hardware parallel load. |
This is how the state diagram is modified for configuring and driving the TMR0. When timing period TP = 6.666 ms is reached, var_CLK_flag is set. The main idea now is to configure and start TMR0 when the ST edge is detected at the ISR() function., thus start bit will be send immediately after the ST active edge. Start_bit state will remain until TP is elapsed.
![]() |
Fig. 8. State diagram modifications. |
We will draw only the function sections where the TMR0 parameters are set.
![]() |
Fig. 8. ISR() function flowchart. |
Project location:
C:\CSD\P12\s_trans_LCD_TMR0\(files)
Specifications | Planning | 3. Dev. & 4. test | Prototype | Report |
This is the serial_transmitter_LCD_TMR0.pdsprj circuit captured in Proteus.
![]() |
Fig. 10. Captured circuit. |
The XC8 compiler version is v3.0 or higher. C language standard is C99. And these LCD library files lcd.c, lcd.h has to be included in the project. The file config.h contains all the microcontroller configuration bits.
This is the serial_transmitter_LCD_TMR0.c source code.
Fig. 11 shows Proteus running this application while at the same time watching RAM variables of interest.
![]() |
Fig. 11. Proteus running the application. |
Oscilloscope results can be printed using white background colour for saving ink and easy annotation.
![]() |
Fig. 12. Proteus oscilloscope waveforms when Din = "0101" |
Measure the transmission frequency accuracy.
To study in detail this project, you can modify the design to use the TMR0 in 16 bit mode.
Specifications | Planning | Dev. & Test | 5. Prototype | Report |
We aim to prototype the transmitter and receiver using two CSD_PICstick boards.
Specifications | Planning | Dev. & Test | Prototype | 6. Report |
Follow this rubric for writing reports.
End of the course option: Now, that you have written many reports in paper, in case of free time once the course ended, you can edit and publish an example project report using LibreOffice word processor and editing tools or similar commercial tools from Microsoft, Google, etc.