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

Lab 10

Laboratory

Laboratory 11: peripherals: [P11] LCD, [P12TMR0 + dedicated processor

 Timer. Phase #1: time-base with external CLK        phase #2: LCD        phase #3 : internal TMR0

LAB_AR

[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 as shown in Fig. 1.

Basic symbol

Fig. 1. Timer symbol.

We can easily imagine two design steps in this phase:

- Step #1. Basic non-retriggerable timer as for the the classic chip 74LS121. These are other similar classic timer circuits. Thus, we can draw the symbol in Fig. 1 without the RT input signal.

- Step #2. Retriggerable (RT = '1') and non-retriggerable (RT= '0') as for the classic chip 74LS122/123. This control signal RT is a switch to be polled (read or captured) connected to a given port pin, for example RA4.

Waveforms

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 for this timer. The FSM has to be able to attend trigger active edges to start operation and control the datapath, for instance a radix-2 up counter modulo 65536 (16-bit). In this way, for a TPD_CLK = 20 ms used as time base TB, we can generate a Timer_out signal that will last up to 21 min and 50 s.

Dedicated processor

Fig. 3. Example dedicated processor to perform the timer function as it could have been designed in Chapter 2 (L8.1).

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.

Therefore, let us adapt such structures to a microcontroller.

 


A) Planning hardware

Determine where to connect the crystal oscillator (OSC), the master clear active-low or reset push-button (MCLR_L) control signals, RT, Trigger_L, time base CLK, and the output Timer_out.

Circuit

Fig. 4. Example of hardware connections.

The interrupt-driven Trigger_L signal is a pushbutton connected at RB0/INT0.

The interrupt-driven CLK external oscillator connected to RB1/INT1 is used to generate a time-base TB = 20 ms from which we will count "real-time".

 


B) Planning software

Draw the hardware-software diagram imagining which mC resources may be connected to count real time.

Hardware-software

Fig. 5. Hardware-software diagram. The interrupt service routine ISR() will generate two flag variables to run the FSM.

List the RAM variables required and their type. Which variables will be flags as we name them attached to interrupts?

RAM variables

Fig. 6. RAM variables.

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?

Draw the state diagram to run the FSM in control of the application. The external CLK interrupt (var_CLK_flag) will be necessary (enabled) only when timing.

state  diagram

Fig. 7. Proposed state diagram for the FSM. The system is either idle or timing. However, peripherals and operational devices will require set up and stop extra states to prepare them to work properly. Most of the  mC peripheral devices may be kept disabled or even switched off when they are not required.

Fig. 8 shows our CSD flowchart to organise the software. The main loop will include the datapath() function running only when timing.

Software

Fig. 8. General software organisation. ISR() will determine which is the interrupt source: trigger button (INT0) or external CLK (INT1). Datapath function for counting CLK periods will not be used when IDLE.

Program init_system() function, configuring ports and interrupts INT0 and INT1.

TRIS Fig. 9. These are the data direction registers (TRIS) to configure the ports.

PORTB pin 0 is INT0, the Trigger_L push-button active-low to start timing

PORTB pin 1 is INT1, the external oscillator CLK time-base TB.

PORTA pin 4 is the the switch RT to select between the two modes of operation.

PORTE pin 0 is the the output Timer_out.

Unused port pins are configured by default as outputs.

Program state_logic() so that all state transitions in Fig. 7 are implemented. Draw the truth tables and flowcharts as we usually do applying plan B.

trutn table
state logic flow chart

Fig. 10. State logic truth table and its equivalent behavioural flowchart from which C instructions are inferred directly without 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.

Output logic truth table
flowchart

Fig. 11. Output logic. Truth table and flowchart.

Program datapath() as a P7 counter solved now in software.

datapath

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. 

ISR

Fig. 13.  ISR() flowchart.

Program read_inputs(), write_outputs(). Truth tables and flowcharts.  

read inputs

Fig. 14. Read_inputs() captures RT value when executed. This value is saved in a convenient RAM variable var_RT.

Writing outputs

Fig. 15. Write_outputs() copies var_Timer_out to the RE0 port bit.


Target chip PIC18F46K22. Project location:

C:\CSD\P12\Timer\(files)

 


Specifications Planning 3. Dev. & 4. test Prototype report

A) Developing hardware

The basic timer represented in Fig. 16. "Timer.pdsprj" hardware.

Timer circuit captured
PIC18F46K22

Fig. 16. Timer circuit captured in Proteus an the microcontroller connections to specific pins.


B) Developing software

Source file example "Timer.c" containing the translation of all planned flowchart. Add the "config.h" header to your project.

Run the microcontroller's IDE to develop and compile the C code to obtain executable files for the target PIC18F46K22.

 


C) Step-by-step testing

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.

Running and watching variables

Fig. 17. Basic timer running while watching RAM variables.

Questions:

 


Specifications Planning Dev. & Test 5. Prototype Report

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.

 



Lab10

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

Lab AR

top

3.7.2.2. Timer_LCD (design phase#2)

Timer enhanced with an LCD peripheral to print ASCII messages (text and numerical data) on the screen.

1. Specifications Planning Dev. & test Prototype report

Design phase #2: Design a fixed-time timer (for instance 18.5 s) adding an LCD screen for representing information. The symbol is represented in Fig. 1.

Symbol Waveforms

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.

 


Specifications 2. Planning Dev. & test Prototype report

A) Planning hardware

The LCD interface is copied from the example in P11. Only six wires are required: D(7..4) for the data bus, LCD_E and LCD_RS to send instructions or data.

LCD display

Fig. 2. The LCD interface used in our CSD_PICstick board can be replicated in Proteus.

In this way the hardware schematic is presented in Fig. 3.

Electronic circuit

Fig. 3. Electronic circuit sketch. 


B) Planning software

Draw the hardware-software diagram. Discuss how the state diagram is modified to adapt the LCD. What new signals are required? What is var_LCD_flag?

HArdware_software diagram

Fig. 4. Hardware-software diagram is the same as in design phase #1 adding the LCD interface connected to output_logic() function.

 Modify init_system() function.

Picture

Fig. 5. Adaptation in init_system().

 Modify state-logic() and output_logic(). Truth tables and flowcharts.  

Picture

Fig. 6. Adaptations in state-logic() and output_logic().

  


Project location:

C:\CSD\P12\Timer_LCD\(files)

 


Specifications Planning 3. Dev. & 4. test Prototype report

We will start copying the previous files from design phase #1 in the new folder location changing their names. We will compile and run the simulations to check that everything works before adding the LCD code introducing and changing their names.

A) Developing hardware

Include LCD display. This is an example "Timer_LCD.pdsprj" hardware.

Circuit in Proteus

Fig. 7. Circuit captured in Proteus.

 


B) Developing software

The XC8 compiler version is v3.0 or newer, C 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 an example source file containing the LCD adaptations "Timer_LCD.c". Generate the executable (*.hex) and debugging files (*.cof).

 


C) Step-by-step testing

Fig. 8 shows the circuit running representing ASCII characters on the LCD screen.

Circuit running

Fig. 8. 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.

Circuit running with watch window

Fig. 9. The timer running while watching the software main variables.

 


Specifications Planning Dev. & Test 5. Prototype Report

Example 1: Board CSD_PICstick . Target microcontroller: PIC18F46K22. Tools: MPLAB X + XC8 + Proteus + VB8012 compact instrumentation.  

For this first experiment we can connect the LCD through the 40-pin flat cable and the prototype board or only the six individual female-male wires.

 


Specifications Planning Dev. & Test Prototype 6. Report

Follow this rubric for writing reports.

 




Lab10

Laboratory

Laboratory 11: peripherals: [P11] LCD, [P12TMR0 + dedicated processor

 Timer. Phase #1: time-base with external CLK        phase #2: LCD        phase #3: internal TMR0

Lab AR

top

3.8.3. Examples: Timer (continuation)

3.8.3.1. Timer_LCD_TMR0  (design phase#3)

Discuss how the functionality assigned to the RAM variable counter and the time base circuits in the datapath can be replaced by the peripheral TMR0 embedded in the microcontroller.

 

1. Specifications Planning Dev. & test Prototype report

Design phase #3: Design a fixed-time timer (for instance TP = 18.5 s) as represented in Fig. 1 replacing the internal RAM variable acting as a datapath counter by the TMR0 running as a time base TB  4·TOSC = 500 ns. Let us consider the 8 MHz crystal oscillator on the CSD_PICstick.

Symbol
 

Fig. 1. Project entity is the same in phase #2 eliminating the external CLK and replacing it by the internal TMR0.

Common features:

- The same as in project phase #1 Timer and phase #2 Timer_LCD .

- TMR0 peripheral to generate from the TB = 500 ns a square output signal TMR0_wave with a period 2·TTMR0. (the waveform will toggle every TMR0 interrupt time).

- Optional: Add a buzzer that will sound for 2 s at 1 kHz once the timing period has ended.

 

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.

- Implement the circuit in the CSD_PICstick as a prototype and perform measurements to characterise the circuit's performance.


Specifications 2. Planning Dev. & test Prototype report

A) Planning hardware

The schematic is simpler from phase #2 because CLK is removed. We can use port pins connected to the CSD_PICstick available resources, as shown in Fig. 2. We have to consider the hardware belonging the the TMR0 peripheral as another project module.

Hardware
TMR0 hardware

Fig. 2.Hardware schematic for the top timer application and also the TMR0 peripheral hardware to configure. 


B) Planning software

Study how the TMR0 works. 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.

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.

hardware-software

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.

Explain the hardware-software diagram of the TMR0 timer.

Design equation using the prescaler (N1), counter TMR0 (N2), and (software) postscaler (N3) when required:

TP = 4 · TB · N1 · N2 · N3 = TTMR0·N3

How to increase counting capacity to any arbitrary large timing period? 

Fig. 4 shows how the state diagram may be adapted to generate timing periods using TMR0.

State diagram

Fig. 4. State diagram for the FSM.

Program init_system() function configuring ports, interrupts and the TMR0.

Symbol

Fig. 5. TRIS registers.

Discuss the state-logic() truth table and flowchart.   

Symbol

Fig. 6. state_logic().

Discuss the output-logic() truth table and flowchart.   

truth table

Fig. 7. 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.

Symbol

Fig. 8. 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

A) Developing hardware

This is the "Timer_LCD_TMR0.pdsprj" hardware including the LCD and the TMR0.

Chip diagram using the TMR0

Fig. 8. The variable var_Timer_flag is derived from the internal TMR0. Therefore, the external pin RB1/INT1 can be used for another purpose.

 


B) Developing software

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 example software source code "Timer_LCD_TMR0.c". Generate the executable (*.hex) and debugging files (*.cof).

 


C) Step-by-step testing

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.

Circuit running and watching variables

Fig. 9. Circuit running and watching variables of interest.

We can measure with the oscilloscope the circuit performance, for instance N3 = 125 generates a TP = 0.5 s, as shown in Fig. 10.

OScilloscope waveforms

Fig. 10. Waveforms printed from the Proteus oscilloscope. Interrupts Zooming the TMR0_wave activated while timing, we can observe how TMR0IF flags are generated every TTMR0 = 4 ms.

 


(update) NOTE: A new feature: Fig. 11 is the same circuit Timer_LCD_TMR0.c to represent dynamic data while counting. Real-time processing is solved in Timing state.

dynamic data

Fig. 11. Displaying "real-time" in one-tenth of a second resolution on the LCD.

circuit

Fig. 12. Picture of the same timer representing and updating time every tenth of a second.

 


Specifications Planning Dev. & Test 5. Prototype Report

Board CSD_PICstick . Target microcontroller: PIC18F46K22. Tools: MPLAB X + XC8 + Proteus + VB8012 compact instrumentation. 

Experiment #1: Run the application and visualise results using the board resources. This is for checking the installation of the tools and programming environment.

 


Experiment #2: Download and program the target chip (*.elf) to use the in-circuit debugger. Watch RAM variables in MPLABX IDE. Add breakpoints and follow the program execution as we did in Proteus.

 


Experiment #3: Drive and visualise digital signals using instruments.

Fig, 13 shows the connections to theVB8012 using the 40-pin header.

Header connections

Connections

Fig. 13. 40-pin header connections to the VB8012.  And yet another schematic symbol indicating signals of interest, header pin numbers and instrument probes in different colours.

 

Picture of the VB8012 connections

Fig. 14. Picture of the experiment (the LCD is not connected).  

Waveforms annotated

Fig. 15. Printing and annotating the captured logic analyser waveforms. Here we are showing a timing period TP = 1.32 s (N3 = 330)

 


Modify the system to add the buzzer to sound the alarm for 2 s when the timing period has finished. 

 



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.