Laboratory |
Laboratory 11: peripherals: [P11] LCD, [P12] TMR0 + dedicated processor Timer. Phase #1: time-base with external CLK phase #2: LCD phase #3 : internal TMR0 |
[22/5] |
This is the group post lab assignment PLA10_11 to be submitted at Atenea before the due date. Study and execute in your computer this LAB11 and the previous LAB10 tutorials before attempting to solve this post lab assignment. |
3.7.2. Examples: Timer
3.7.2.1. Timer (design phase#1)
This laboratory project is for adapting a basic timer circuit organised as a dedicated processor in P8 (datapath & control unit) to a mC PIC18F46K22. Timer's datapath is conceived as a RAM variable driven by an external CLK (50 Hz) signal connected to an external interrupt (INT1) source. This external CLK is the time-base (TB) of the application defining a time resolution of 20 ms.
1. Specifications | Planning | Dev. & test | Prototype | report |
Design phase #1: Design a fixed timing period timer, for instance:
Timing period: TP = 18.5 s, as represented in Fig. 1.
A switch RT to select two modes of operation: re-triggerable and non-retriggerable (consider two design steps).
External 50 Hz CLK (INT1) time-base. TB = 20 ms.
Trigger control signal push-button.
LED connected to Timer_out to visualise the timing period duration.
C language, coding in FSM style, dedicated processor approach.
Microcontroller PIC18F46K22.
MPLABX IDE + XC8 compiler + Proteus
CSD_PICstick board for prototyping experimentation.
VB8012 compact instrument for measurement and product characterisation.
![]() |
Fig. 1. Timer symbol. |
We can easily imagine two design steps in this phase:
- Step #1. Basic non-retriggerable timer, like the classic chip 74LS121. These are other similar classic timer circuits.
- Step #2. Retriggerable (RT = '1') and non-retriggerable (RT= '0'). The classic chip 74LS122/123 is retriggerable. RT is a switch to be read connected to a given port pin, for example RA4.
![]() |
Fig. 2. a) Waveform in non-retriggerable mode of operation. b) Retriggerable mode of operation where it is possible to keep Timer_out high forever if the user clicks Trigger repeatedly before the timing period ends.This particular circuit application has another widely used name: watchdog timer. |
Specifications | 2. Planning | Dev. & test | Prototype | report |
Fig. 3 below shows a dedicated processor design. The FSM has to be able to attend trigger active edges to start operation and control the datapath, for instance an up binary counter.
![]() |
Fig. 3. Example dedicated processor to perform timer function. |
Study other materials, for instance these notes on how a programmable timer can be planned as in Chapter II using VHDL and FPGA/CPLD devices. Review as well the P8 highlighted programmable timer Timer_MMSS that is more complex but with a similar organisation. Thus, let us adapt such structures to a microcontroller.
Determine where to connect OSC, MCLR_L, Trigger, CLK, Timer_out and RT.
![]() |
Fig. 4. Hardware connections. Interrupt-driven Trigger signal is a pushbutton connected at RB0/INT0. Interrupt-driven CLK external oscillator connected to RB1/INT1 is used to generate a time-base TB = 20 ms. |
Draw the hardware-software diagram imagining which mC resources may be connected to count real time.
![]() |
Fig. 5. Hardware-software diagram. |
List the RAM variables required and their type. Which variables will be flags as we name them attached to interrupts?
For instance, a good question to see the adaptability of this architecture: what would be the datapath structure if we like to count for a long timing period of TP = 2.5 h?
![]() |
Fig. 6. RAM variables |
Draw the state diagram that may run the FSM in control of the application.
![]() |
Fig. 7. State diagram for the FSM. The system is either idle or timing. However, peripherals and operating devices will require setting up and stopping extra states to prepare them to work properly. |
Fig. 8 shows a flowchart to organise the software. The FSM will include the datapath in its main loop, running it only when timing.
![]() |
Fig. 8. General software organisation. ISR() will determine what is the interrupt source: trigger button or external CLK. Datapath function for counting CLK periods will not be used when IDLE. |
Program init_system() function, configuring ports and interrupts INT0 and INT1.
![]() |
Fig. 9. Data direction register (TRIS).
PORTB pin 0 is INT0, the Trigger push-button to start timing. PORTB pin 1 is INT1, the external oscillator CLK time-base TB. PORTB pin3 is the output Timer_out. PORTA pin 5 will the the switch RT to select between the two modes of operation. |
Program state_logic() so that all state transitions in Fig. 8 are implemented. Truth tables and flowcharts.
![]() ![]() |
Fig. 10. State logic and its equivalent behavioural flowchart (below) where C instructions are inferred with no difficulties. |
Program output_logic(). Truth tables and flowcharts. As seen in Fig. 6 hardware-software diagram, output logic has to generate Timer_out circuit's output and control signals for managing the datapath counter.
![]() ![]() |
Fig. 11. Output logic. Truth table and flowchart. |
Program datapath() as a P7 counter solved now in software.
![]() |
Fig. 12. Datapath is a counter described using RAM variables. Be aware that executing assembly instructions requires some μs, consequently introducing some systematic error if what we have in mind is measuring real-time. |
Write ISR() flowchart. so that the computer program can be sensitive to events such CLK rising edge and trigger button falling edges.
![]() |
Fig. 13. ISR() flowchart. |
Program read_inputs(), write_outputs(). Truth tables and flowcharts.
![]() |
Fig. 14. Read_inputs() captures RT value when executed. This value is saved in a convenient RAM variable var_RT. |
![]() |
Fig. 15. Write_outputs() this time handles only var_Timer_out. |
Target chip PIC18F4520. Project location:
C:\CSD\P12\Timer\(files)
Specifications | Planning | 3. Dev. & 4. test | Prototype | report |
The basic timer represented in Fig. 16. Timer.pdsprj hardware.
![]() |
Fig. 16. Microcontroller connections. |
Source file example Timer.c containing the translation of all planned flowchart .
The idea is, as usual, write and run a bit of code with both windows, the hardware Proteus environment and the software IDE running simultaneously for easy debugging. Use watch window and break points to control the program sequence and monitor RAM variables of interest for each function.
![]() |
Fig. 17. Basic timer running while watching RAM variables. |
Specifications | Planning | Dev. & Test | 5. Prototype | Report |
Example 1: Board CSD_PICstick . Target microcontroller: PIC18F46K22. Tools: MPLAB X + XC8 + Proteus + VB8012 compact instrumentation.
Modify timing period to 6.5 s. In this application the key point is the timing period precision. Is it 6.4998 s or 6.50001 s? How to measure timing precision? What can be done to improve it?
Specifications | Planning | Dev. & Test | Prototype | 6. Report |
Follow this rubric for writing reports.
Laboratory |
Laboratory 11: peripherals: [P11] LCD, [P12] TMR0 + dedicated processor Timer. Phase #1: time-base with external CLK phase #2: LCD phase #3 : internal TMR0 |
3.7.2.2. Timer_LCD (design phase#2)
Timer enhanced with LCD to show ASCII messages (and numerical data)on the LCD screen.
1. Specifications | Planning | Dev. & test | Prototype | report |
(Fixed-time timer. Phase #2: LCD peripheral)
Design a fixed-time timer (for instance 18.5 s) adding an LCD screen for representing information.
![]() |
![]() |
Fig. 1. The timer symbol and basic waveform in non-retriggerable mode of operation. |
Features:
- Same features as in design phase #1 Timer above.
- In a first step #1 information may be simply ASCII messages, in a second step #2, information may include dynamic data such time in seconds while down counting. The symbol is represented in Fig. 1.
Specifications | 2. Planning | Dev. & test | Prototype | report |
The LCD interface copying and adapting a previous example from P11.
![]() |
Fig. 2. The LCD interface used in PICDEM 2 PLUS board can be replicated in Proteus. |
Electronic circuit.
![]() |
Fig. 3. Electronic circuit sketch. |
Draw and discuss an state diagram and software organization. What states or signals have to be introduced to adapt the LCD? Hardware-software diagram
- Modify init_system() function.
- Modify state-logic() and output_logic(). Truth tables and flowcharts.
- Discuss read_inputs(), write_outputs() and ISR(). Truth tables and flowcharts.
Project location:
C:\CSD\P12\Timer_LCD\(files)
Specifications | Planning | 3. Dev. & 4. test | Prototype | report |
Study all the files in the basic Timer, design phase #1 above, this si the same application fully operational without LCD.
Include LCD display. This is an example Timer_LCD.pdsprj harware.
![]() |
Fig. 4. Circuit captured in Proteus. |
This is an example source file Timer_LCD.c to be compiled along with LCD libraries.
Fig. 5 shows the circuit running representing ASCII characters on the LCD screen.
![]() |
Fig. 5. Timer with LCD running. |
The idea is for easy debugging, write, compile and run a bit of code with both windows working interactively: hardware Proteus environment and software IDE. Use the watch window and break points to stop and control the program sequence and monitor RAM variables of interest for each function.
![]() |
Fig. 3. The timer running while watching the main variables of the software. |
Specifications | Planning | Dev. & Test | 5. Prototype | Report |
Example 2: Board CSD_PICstick . Target microcontroller: PIC18F46K22. Tools: MPLAB X + XC8 + Proteus + VB8012 compact instrumentation.
Specifications | Planning | Dev. & Test | Prototype | 6. Report |
Follow this rubric for writing reports.
Laboratory |
Laboratory 11: peripherals: [P11] LCD, [P12] TMR0 + dedicated processor Timer. Phase #1: time-base with external CLK phase #2: LCD phase #3: internal TMR0 |
3.8.3. Examples: Timer (continuation)
3.8.3.1. Timer_LCD_TMR0 (design phase#3)
Discuss how the functionality assigned to the counter and the time base circuits in the datapath can be covered by peripheral TMR0 embedded in the microcontroller. Replace the previous time-base (external CLK interrupt) by the peripheral TRM0.
- TMR0: hardware configuration bits and registers: CLK edge, CLK source, prescaler selection, TMR0 in 8-bit or 16 bit mode, hardware interrupt on overflow (terminal count) TMR0IF and variable var_TMR0_flag.
- Study the hardware-software diagram of the TMR0 timer/counter: prescaler (N1), counter TMR0 (N2), and (software) postscaler (N3) when required. How to increase counting capacity?
Questions:
- Measure simulator's "real-time" using break points and step mode.
- Watch variables of interest.
- Modify parameters to generate TP = 2.23 s.
- Modify parameters to generate TP = 5.00 s when XTAL OSC is 12 MHz.
1. Specifications | Planning | Dev. & test | Prototype | report |
Design phase #3: TMR0 time-base
Design a fixed-time timer (for instance 18.5 s) as represented in Fig. 1 replacing the internal RAM variable acting as a datapath counter by TMR0 time-base of 1 us (FOSC = 4 MHz).
![]() |
Fig. 1.Project entity is the same in phase #2 but suppressing external CLK. |
Common features:
- The same as in project phase #1 Timer and phase #2 Timer_LCD .
- TMR0 peripheral to generate 1 μs time-base.
- Optional: Add a buzzer that will sound for 2 s at 1 kHz once the timing period has ended.
Specifications | 2. Planning | Dev. & test | Prototype | report |
Study how the TMR0 works, its configuration bits and interrupt mechanism. How to add a post-scaler to generate long counts?
If the optional buzzer is added it may wired at RC2 (as in PICDEM2plus board). The schematic is simpler from phase #2 because CLK is removed.
![]() |
Fig. 2.Schematic. |
Follow this adaptation. The datapath capable of counting real-time is TMR0 peripheral configured as a timer; the control unit is a FSM running in the main loop. Draw the hardware-software diagram as in Fig. 3 and the FSM state diagram (Fig. 4) to generate the output and to command the datapath.
![]() |
Fig. 3. Hardware-software diagram representing in dotted green lines the circuit that has become the datapath for calculating real-time from an internal time-base derived from system OSC. |
![]() |
Fig. 4. State diagram for the FSM. |
Program init_system() function, configuring ports and interrupts and TMR0.
![]() |
Fig. 5. TRIS registers. |
Program state-logic() basically as in phase #2 or phase #1. Truth tables and flowcharts.
Program output_logic(). Truth table in Fig. 6.
![]() |
Fig. 6. Truth table for the output_logic() representing the instructions to the datapath and outputs. |
Program ISR() to acknowledge interrupts from TMR0 and INT0 solving as well the datapath operation of real-time counting.
![]() |
Fig. 7. ISR(). |
Program read_inputs(), write_outputs() as in phase #2 or phase #1.
Project location:
C:\CSD\P12\Timer_LCD_TMR0\(files)
Specifications | Planning | 3. Dev. & 4. test | Prototype | report |
This Timer_LCD_TMR0.pdsprj hardware including the LCD and the TMR0.
![]() |
Fig. 8. var_Timer_flag is derived from internal TMR0. It means that another external for another purpose can be used in pin RB1/INT1. |
This is the example software source code Timer_LCD_TMR0.c to be compiled along with LCD libraries.
The idea is, as usual, write and run a bit of code with both windows, the hardware Proteus environment and the software IDE running simultaneously for easy debugging. Use the watch window and break points to control the program sequence and monitor RAM variables of interest for each function.
![]() |
Fig. 9. Circuit running and watching variables of interest. |
NOTE: A new feature: Fig. 10 is the same circuit Timer_LCD_TMR0.c to represent dynamic data while counting. Real-time processing is solved in Timing state. |
![]() |
Fig. 10. Displaying "real-time" in one-tenth of a second resolution on the LCD. |
![]() |
Fig. 11. Picture of the same timer representing and updating time every tenth of a second. |
Specifications | Planning | Dev. & Test | 5. Prototype | Report |
Example 3: Board CSD_PICstick . Target microcontroller: PIC18F46K22. Tools: MPLAB X + XC8 + Proteus + VB8012 compact instrumentation.
How to represent as well a seconds count in the LCD?
Add the buzzer to sound the alarm when the timing period has finished. What kind of modifications are required?
|
TMR2 can be used instead of TMR0 as proposed in this design phase #4: Timer_LCD_TMR2.
Specifications | Planning | Dev. & Test | Prototype | 6. Report |
Follow this rubric for writing reports.