Pages created and updated by Terry Sturtevant Date Posted: January 14, 2020

CP316: Microprocessor Systems and Interfacing

Timers and Counters

PostLab

  1. Make the necessary changes to use low-level instructions to toggle the output bit.
  2. Annotate the resulting oscilloscope sketch to show how to calculate the delay between the timer output and the other output.
  3. Paste this in your lab notebook.

Hand in a copy of the code and sketch at the next lab.

Creative Commons License

Information on this site which is produced by Terry Sturtevant is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Canada License.

Resources

To view pdf documents, you can download Adobe Acrobat Reader .
get Acrobat Reader
If you need to update a browser, you might try Firefox which is Get Firefox!
Since this page uses cascading style sheets for its layout, it will look best with a browser which supports the specifications as fully as possible.

If you are looking for an office package, with a word processor, spreadsheet, etc., you might try LibreOffice which is Get LibreOffice!

Go to the main page for the Department of Physics and Computer Science.

Valid XHTML 1.1

Valid CSS!

WCAG
2.0
(Level AA)

Wilfrid Laurier University

---------

Pages created and updated by Terry Sturtevant Date Posted: January 14, 2020

CP316: Microprocessor Systems and Interfacing

Objectives

Since creating square waves and pulses is common in circuits, hardware timers make this easy. Once set up, they can run indefinitely without software intervention.

Equipment

Procedure

Arduino timer "poll overflow flag" program

  1. Connect the Arduino board.
  2. Create a new sketch, and download the timer poll overflow flag sketch.
  3. Examine the code, and based on the timer configuration registers, identify which mode the timer is in; i.e. normal or CTC. Which is it?
    Also, determine what prescaler value is being used. Which is it?
  4. Connect the oscilloscope to output pins 9 and 13.
    (This is because each timer has certain pins which it can control. Timer 1 can control pins 9 and 10. None of the timers can directly control pin 13.)
  5. Run the program to see that the square wave appears on the scope.
  6. Change the value in OCR1A (by at least a factor of 2) and rerun the program to see if the frequency changes. Does it?
    Based on the mode, is this what you would expect? Explain.
  7. Without changing the mode, change the prescaler value. Record the output frequency observed. Does it make sense? Explain.
    Try a couple of other prescaler values to see that it operates as expected.
  8. Return to the original prescaler value, and rerun the program.
  9. Comment out the delay instructions in the program and rerun it so you can see the maximum frequency that can be produced.
    Sketch the output, and note the oscilloscope settings.
    arduino blink output
    Demonstration - summarize what you have learned about this timing mode and the prescaler.

Arduino timer "poll compare flag" program

  1. Create a new sketch, and download the timer poll compare flag sketch.
  2. Examine the code, and based on the timer configuration registers, identify which mode the timer is in; i.e. normal or CTC. Which is it?
    Also, determine what prescaler value is being used. Which is it?
  3. Connect the oscilloscope to output pins 9 and 13.
    (This is because each timer has certain pins which it can control. Timer 1 can control pins 9 and 10. None of the timers can directly control pin 13.)
  4. Run the program to see that the square wave appears on the scope.
    Determine the delay between the two output pins. What is the cause of the delay?
  5. Change the value in OCR1A (by at least a factor of 2) and rerun the program to see if the frequency changes. Does it?
    Based on the mode, is this what you would expect? Explain.
  6. Return to the original value in OCR1A.
  7. Comment out the delay instructions in the program and rerun it so you can see the maximum frequency that can be produced.
    Sketch the output, and note the oscilloscope settings.
    arduino blink output

  8. Without changing the mode, change the prescaler value. Record the output frequency observed. Does it make sense? Explain.
    Try a couple of other prescaler values to see that it operates as expected.
  9. Return to the original prescaler value, and rerun the program.
  10. Press the Run/Stop button on the oscilloscope to freeze the image, and note how consistent the pulses are. Repeat this 5 or 6 times to find the maximum variation between pulses that you observe.
    Sketch the output, and note the oscilloscope settings.
    Demonstration - demonstrate the screen showing the variation.
    Because there is no operating system, your program is the only task running.

Using low-level operations

  1. Look at the blinkfast sketch to see how access was made to Arduino pin 13.
  2. In this week's sketches, toggling a pin is done using the following instruction:
        digitalWrite(LED_PIN,!digitalRead(LED_PIN));
        
    Follow the example from the blinkfast sketch to replace this line of code with a low-level command. (Hint: The exclusive OR operator is '^'.)

    Determine the delay between the two output pins. How does it compare to the previous delay?
    Demonstration - demonstrate the modified program working.
  3. Disassemble both the original code and the modified version so that the timing can be compared.

Multiple timers; adapt for Timer 2

  1. Adapt the code to use Timer 2 instead.
    Demonstration - demonstrate the operation of Timer 2 using the modified code.
Creative Commons License

Information on this site which is produced by Terry Sturtevant is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Canada License.

Resources

To view pdf documents, you can download Adobe Acrobat Reader .
get Acrobat Reader
If you need to update a browser, you might try Firefox which is Get Firefox!
Since this page uses cascading style sheets for its layout, it will look best with a browser which supports the specifications as fully as possible.

If you are looking for an office package, with a word processor, spreadsheet, etc., you might try LibreOffice which is Get LibreOffice!

Go to the main page for the Department of Physics and Computer Science.

Valid XHTML 1.1

Valid CSS!

WCAG
2.0
(Level AA)

Wilfrid Laurier University