CP316: Microprocessor Systems and Interfacing 
   Timers and Counters 
 
Objectives
Microcontroller timers can typically operate as a timer or a counter. 
  A timer outputs
  a signal after a specified interval of time. 
  A counter counts the number
  of pulses (by counting rising or falling edges) for a particular input signal.
  The timing signal will be monitored using an oscilloscope driven from Port
  C.
  - using C2 on the top header strip to allow the timing signal to be measured
    on an oscilloscope
- implementing timers and counters 
Equipment
Procedure
  - Modify one of your LED programs from the last lab (new project) to turn
    the left LED on/off every second, i.e. on for one second, 
    off for one second.
    Structure the program so that you have one subroutine that measures 1/100
    of a second based on timer0. You should have another subroutine
    that calls the .01 second routine the appropriate number of times to get
    the required timing. 
 
 
- Check GROUND and VDD pins on the header to make absolutely
      sure you understand the header pin numbering. Verify your timing for
      the previous program using the oscilloscope.
 
 Demonstration - demonstrate and explain the 
    operation of your program.
 
 
- If you wanted to change the timing of the above program to turn the left
    LED on/off every two seconds, there are three different ways to change the
    timing.
  
    - You could change the counting routine 
     that calls the timing routine.
- You could change the timing routine to run twice as long.
- You could reconfigure the timer to run twice as slow 
 Implement the three techniques. Note the changes that have to be made to
    the base program in your lab notebook. 
    Verify the timing for each implementation
    using the oscilloscope. 
     Comment on the respective accuracy of each technique.
 
 Demonstration - demonstrate the operation of all 
     three techniques.
 
 
- With respect to the previous question, if you simultaneously maxed out
    all three parameters and assuming that you did not change the structure of
    your program by adding additional loops, 
    at what interval would the LED flash?
    Clearly show the maximum for each of the parameters and how it contributes
    to your calculation.
 
 Demonstration - explain the your calculation.
 
 
- Using the PIC18F452 datasheet or your text, review the block diagrams for
    the four timers.
    
      - Which of the timers allow input from a pin for use as a counter?
- For each of these counters, which pin is used for input and where is
        it located on the 
         
        PIC18F452
        Block Diagram ? 
 NOTE: references in the text or in the Timer3 section of datasheet to
        pin T13CKI should be replaced with T1CKI.
- For each of these input pins, what is the associated port pin (from
        block diagram)?
- How are these port pins used in the QwikFlash system 
       (from schematic)?
- Can we use any of these port pins as input to a counter?
 
 
 Demonstration - explain your investigation.
 
 
- Modify your program from question 1 of this lab (new project) to turn the
    left LED on/off every 1.5 seconds based on timer1. 
    Verify your timing
    using the oscilloscope.
 
 Demonstration - demonstrate and explain the operation 
     of your program.
 
 
- Although RC0, RC3, and RC5 are connected as inputs to the DAC, the DAC
    does not drive any of these pins. 
    These pins can be used by expansion
    circuitry and, at worst, the only consequence would be 
    erratic output voltages
    appearing on the DAC outputs. 
  
    - Use the DAC datasheet, 
      MAX522,
      to determine the function of the 
      CS
       pin on the DAC. 
- Use the DAC datasheet, to determine the electrical specifications for
      the
      CS
      pin on the DAC. 
- Use the QwikFlash board test program (the QFPV.asm project from last
      week's lab) and an oscilloscope to verify your results.
 
 
 
 
- Setup the signal generator to produce a 0-5V square wave and verify the
    signal using the scope. 
  
 Demonstration - explain your investigation of the DAC, demonstrate
  your 0-5V signal, and demonstrate location of pin1 on the header.
 
 You may not proceed to next task until the 
   lab supervisor gives his OK.
 
 
 
- Write a program to demonstrate the use of timer1 as a counter. Use the
    information from the previous steps to provide a suitable 
    input signal without
    harming the DAC. 
 Demonstration - demonstrate and explain the 
    operation of your program.
 
 
- Make a macro to initiallize timer 1 and add it to your include 
   file. How much flexability can you put into the macro compared to 
   doing all of the configuration in your main program? Adjust your main 
    program accordingly.
    
 Demonstration - demonstrate the revised code in 
    operation.
 
              Wilfrid Laurier University
            
              © 2019 Wilfrid Laurier University