Lab 13 - Timer Interrupt

1. Introduction

The second interrupt function that we can use in Flowcode is the timer interrupt. Timer interrupts allow you to perform software tasks at precisely predetermined time intervals. This is a really useful feature that allows you to develop time critical applications and clocks.

2. Setting up the equipment

3. Hardware settings

EB006 Options

Setting

Jumper settings (EB006)

Jumper settings (HP488)

Power supply

External, 14V

J29: PSU

J29: PSU

PICmicro device

16F1937

Programming method

USB

J12,13,14: USB

J20: USB

Clocking method

XTAL

SW2: XTAL

S2: XTAL

R/C clock speed

Xtal frequency

19 660 800 Hz

LVP Jumper selection

I/O Port

J11,16,17: I/O Port

J15,16,18: I/O

Port A E-block

Port B E-block

Switch board

Port C E-block

LED board

Port D E-block

LCD board

Port E E-block

4. Flowcode and download settings

Build > Project Options... > General Options

Build > Project Options... > Configure

Options

Setting

Clock speed

19 660 800 Hz

Simulation speed

10

Options

Setting

Device

16F1937

RC/XT

XTAL

Watchdog timer

Off

5. Software learning objectives

Real time execution, exact timing, using timer interrupts.

6. Hardware learning objectives

Triggering the timer by the Xtal or by an external event.

7. Instructions

Construct the system shown from E-blocks.

In the course navigate to the ‘Flowcode step-by-step’ and review the section on Seven Segment Display (step 9). The descriptions of the Multiprogrammer board , the switch board, the LCD and the LED board are in the E-blocks section.

Look into the 'Help' menu and function in Flowcode to get the info you need.

The 16F1937 has multiple timers, but we will only work with two: TMR0 (Timer 0) and TMR1 (Timer 0)

TMR0 can be triggered by the XTAL frequency or by a transition on the T0CKI pin (RA4)

The 19.660.800 Hz Xtal used to clock the PIC is not just a random number. When we divide it by a typical binary number, we get precise timing results

The internal clock is the Xtal clock/4 (19.660.800Hz/4 = 4.915.200Hz)

The timer interrupt TMR0 has a prescaler that can be set from 1:2-1:256. Set it to 256.

In this case every 256 internal clock pulses, this 8-bit timer (TMR0) is incremented by 1.

(4.915.200Hz/256prescaler = 19.200Hz)

Every time this 8-bit timer has an overflow, it generates an interrupt. This happens every time this timer register reaches 256. (19.200Hz/256 = 75Hz)

Now the main program is stopped 75 times per second and the timer interrupt macro is executed exactly 75 times per second.

In stead of clocking this timer by the Xtal, it can also be clocked by an external event. This function could be used to measure motor speed etc.

TMR1 can be triggered by the XTAL frequency or by a transition on the T1CKI pin (RC0)

This timer works similar to TMR0, only the prescaler values differ.

TMR2 also exist but this in not discussed in this course.

8. Labs

Letter

Meaning

L

Lab x

B

Basic complexity

I

Intermediate complexity

E

Expert complexity

L13-B1

Make an exact second counter using 1sec delays. Display these seconds on the LCD. Don’t use any timer interrupt to do this. Download this program to the PIC. Start the clock by resetting the multiprogrammer and hold your own watch next to it to check if it keeps running in the same pace as your ‘correct’ watch.

L13-B2

Same exercise as above, but now you should use a timer interrupt to accomplish the task. Download this program to the PIC. Start the clock by resetting the multiprogrammer and hold your own watch next to it to check if it keeps running in the same pace as your ‘correct’ watch.

L13-B3

Here you are going to develop a basketball timer clock. In basketball, the team that has the ball needs to make a goal attempt before 30 seconds have elapsed. Your clock, displayed on the LCD, should be started when SW0 is pressed (use an external Port B interrupt to trigger the start function). If the 30 second limit is reached, all the LEDs should start flashing on and off (on a basketball court this would be a buzzer) so the referee knows that the ball goes to the other team. Download this program to the PIC.

L13-I4

Remember the reaction timer from lab 12? Take this program to the next level by changing the delay-based timer to a correct interrupt based timer. Download this program to the PIC and test it.

L13-I5

Develop a complete and very accurate hh:mm:ss clock that indicates exactly how long ago the PIC was reset. This clock should be displayed on the LCD. Download this program to the PIC and test it.

L13-E6

Variation to the program above. With SW0, you can stop the clock. SW1-2-3 are used to change the hh:mm:ss values to the actual time. SW0 is used again to start the clock back up again. Download this program to the PIC and test it.

L13-I7

Develop a counter that counts backwards from 01:00:00 to 00:00:00. When 00:00:00 is reached all LEDs should light up. Have fun with this one and surprise your teacher…. Download this program to the PIC and test it.

L13-E8

Develop a dimmer for all the LEDs that reacts to the measured light intensity. Imagine: an LDR measures the light intensity in a room. When the light intensity drops, a dimmer circuit sends more power to the lights in that room and vice versa. Use the LEDs to simulate the dimmed lights. This is done by programming a software PWM output to all these LEDs at port C. When the on-time rises, the LEDs intensify. Put this program in a timer interrupt macro. You have done something similar in [L3-I4]. The main loop should measure the analogue input of the LDR on the sensor board on port A. When the LDR detects less light, the LEDs need to shine brighter. The opposite should happens with the LEDs when the light, measured by the LDR intensifies. The period of the PWM signal stays a constant 20msec at all times and is accomplished by using a timer interrupt. Download this program to the PIC and test it. If you have a 2 channel oscilloscope, measure the analogue input of the LDR on channel A and the PWM output to one of the leds on channel B.

L13-I9

Draw up the full circuit diagram of the PIC with 8 LEDs and LDR, including the clocking circuit, the reset circuit, VDD and VSS connected to the PIC and the correct current limiting resistors connected between LEDs and PIC. If you were to make a practical circuit of this drawing, and connect power to it, it should behave exactly the same as the E-blocks system in front of you. Schematics in the E-blocks section and the full circuit diagrams in the datasheets of the E-blocks will come in handy when you do this task.