Recent Changes - Search:

Introduction to Microcontroller Programming

About PICmicro Chips

Clocking Your PICmicro Devices

E-Blocks

Flowcode Step By Step

PICmicro Projects

Labs

Getting the Timing Right

<^< Using the Quad Display | Course Index | Software Macro >^>

The problem we face is that we can only light one 7-segment display at a time. What do we do?

The answer is both straightforward and complex at the same time. The straightforward bit is that if we keep displaying the digits again and again we will see all four light up as they are going on and off so fast they appear to be on all the time to our eyes. This technique is known as 'multiplexing'.

By adding a loop we can make all four 7-segment displays light up.

The complexity comes when we need to calculate the values to be displayed, or to run another part of the program. If the time taken between updating the different 7-segment displays is too great then the display will stop, or become disjointed. So how do we get around that?

What we do is to take the display icons out of the main program and into a separate routine that is executed frequently elsewhere. We do this using the Timer interrupt. The Timer interrupt gets triggered by the clock at regular intervals. It then interrupts the program that the PICmicro is running, and executes a separate macro. Once the interrupt macro is finished then the program goes back to whatever it was doing.

Add an Interrupt icon to the top of the flowchart, and double-click on it to bring up the property page for it. Select 'Timer 0' Overflow Interrupt and then click on the Properties button. Make the settings you can see here:

The key figure here is the one at the bottom of the dialogue box marked 'Interrupt frequency:'. The Interrupt frequency is the number of times per second that the main program is interrupted and the interrupt macro executed. The Interrupt frequency value doesn't matter too much for this example but aim for a relatively low figure for now - say 150 times per second.

The 'Prescaler' is a digital counter inside the PICmicro device. It takes in fundamental clock signal and counts for a number of cycles. The interrupt is triggered when the prescaler counter reaches the pre-programmed figure. Try changing the prescaler value and clock speed to see how they interact to give the interrupt frequency. The interrupt dialogue window calculates this for you.

Close the prescaler dialogue window and in the Interrupt window click on the 'Create New Macro...' button. Enter "REFRESH" in the text box marked 'Name of new macro' click ok. Every time there is an interrupt the 'REFRESH' macro will run. Now click on the 'OK & Edit Macro' button this will allow us to put some code in it. The 'REFRESH' macro will now be open and ready to use.

There is one more trick to teach you, before we finish this part of the course. We could just show '1234' or some other predetermined number. But then we wouldn't be able to change the display without rewriting the program. So instead let's use a bunch of variables, Digit1, Digit2, Digit3 and Digit4. Now if we want to change the values we can change the variables in the main program, and the next time the interrupt is triggered then the new values will appear on the 7-segment display.

To make use of the timer interrupt trick we simply need to create a program that puts the values into the variables. Here you can see the interrupt routine and the main program:

In this example we just add the values to the variables and do nothing in the main loop, but if we did have a program chugging away updating the variables when needed, they would be displayed automatically by the timer interrupt next time it gets triggered.

This may seem a long-winded and complex way of doing things, but actually it is a very elegant way of making sure that routines get carried out at regular intervals and it can really help simplify your programs.

<^< Using the Quad Display | Course index | Software Macro >^>

Print - Search - Login

Page last modified on May 03, 2013, at 03:56 PM