Posts

Showing posts from November, 2015

TIMERS AND COUNTERS:TIMERS

Image
T IMERS Timer0 will be covered first, and in more detail, as a prototypical timer, and discussion and examples for the use of Timer1 and Timer2 will be provided. The use of timers internal to microprocessors is a bit more complicated than what we have been doing so far because there is a considerable amount of setup required before the timer can be used, and the options for setting the timers up are extensive. We will cover the timers one at a time in an introductory manner. However, you should be aware that there is an entire book available from Microchip Technologies that cov- ers nothing but timers, so the coverage here will be rudimentary. Note The Microchip Technologies timer manual is called The PICmicro Mid- Range MCU Family Reference Book (DS33023), available from Microchip Technology Inc. To understand timers, you must understand how to turn them on and off and how to read and set the various bits and bytes that relate to them. Essentially, in the typical timer applicat...

CONTROLLING THE OUTPUT AND READING THE INPUT:GENERATING OUTPUTS

Image
GENERATING OUTPUTS It will be easier if we learn to control the outputs first because we can do this from programs that we write without the need for any additional hardware or input signal. We will start with the simple control of LEDs and proceed to the control of the two- line LCD that is provided on the LAB-X1, and then move on to using the speaker and an R/C hobby servo. Let us start with the standard turning an LED on and off program. We will use one of the LEDs in the ten-LED bar graph that is provided on the LAB-X1. On the LAB-X1 we have control of only the rightmost eight LEDs on the bar graph. The leftmost LED is the power-on indicator and the one next to it comes on if we were using a common cathode arrangement (as opposed to the common anode arrangement as it is currently configured). The circuitry we are interested in is shown in Figure 5.1. All other circuitry of the LAB-X1 is still in place, but we have suppressed it, as shown in the figure, so we won’t be distracted ...

CONTROLLING THE OUTPUT AND READING THE INPUT:GENERATING OUTPUTS

Image
GENERATING OUTPUTS It will be easier if we learn to control the outputs first because we can do this from programs that we write without the need for any additional hardware or input signal. We will start with the simple control of LEDs and proceed to the control of the two- line LCD that is provided on the LAB-X1, and then move on to using the speaker and an R/C hobby servo. Let us start with the standard turning an LED on and off program. We will use one of the LEDs in the ten-LED bar graph that is provided on the LAB-X1. On the LAB-X1 we have control of only the rightmost eight LEDs on the bar graph. The leftmost LED is the power-on indicator and the one next to it comes on if we were using a common cathode arrangement (as opposed to the common anode arrangement as it is currently configured). The circuitry we are interested in is shown in Figure 5.1. All other circuitry of the LAB-X1 is still in place, but we have suppressed it, as shown in the figure, so we won’t be distracted ...

TIMERS AND COUNTERS:COUNTERS

Image
COUNTERS Of the three timers in the 16F877A, only Timer0 (an 8-bit timer) and Timer1 (the 16-bit timer) can be used as counters. Timer2 does not have a counter input line. Generally speaking this makes Timer0 suitable for use with small counts and rapid interrupts and Timer1 suitable for larger counts. HOW DOES A COUNTER WORK? The operation of a counter is similar to the operation of a timer except that instead of getting its count from an internal clock or oscillator, the counter gets its signals from an outside source. This means we have to do the following to use a counter: N Decide which counter (timer) to use. N Tell the counter where the signal is coming from. N Tell it whether to count on a rising or falling edge of the signal. N Decide what target count we are looking for, if applicable. N Tell the counter where to start counting (because the interrupt will occur when the counter gets full). N Decide whether we will need to scale the count by setting the scale...

TIMERS AND COUNTERS:Exercises for Timers and Exercises for Counters

Exercises for Timers 1. Write a program to generate a one minute timer clock with a 0.1 second display and then do the following: N Check its accuracy with the time site on the Internet. N Make adjustments to make it accurate to within one second per hour and then per day. Can this be done? Which timer works best? Which timer is the easiest to use for such a task? What are the problems that you identified? 2. Write the preceding program for each of the other two timers. Exercises for Counters 1. Design and make a tachometer for small model aircraft engines. Make the range between 5 rev per second to 50,000 rev per minute displayed on the LCD in real time. 2. Design and build a thermometer based on the changes in frequency exhibited by a 555 timer circuit being controlled by a thermistor. Calibrate the thermometer with a lookup table. If you are not familiar with the use of lookup tables, research on the Internet so you can understand how to use them. They are very useful...

TIMERS AND COUNTERS:TIMER OPERATION CONfiRMATION

TIMER OPERATION CONfiRMATION To make sure a timer is working, set up a program in which the interrupt routine incre- ments a variable and the main loop displays it. If you see the variable incrementing, the interrupt routine is working. The speed of the incrementing will give you some idea of the rate at which the interrupts are occurring and will confirm that the interrupts are occurring as fast as you have programmed them to. Other timer related registers can also be looked at to see what is going on in them by displaying them on the LCD. Caution Writin g to the LCD is time consuming and will slow down the system.

TIMERS AND COUNTERS:PRE-SCALERS AND POST-SCALERS

PRE-SCALERS AND POST-SCALERS Pre-scalers and post-scalers can be confusing for the beginner. Here is a simple explanation. A pre-scaler is applied to the system clock and affects the timer by slowing down the system clock as it applies to the timer. Normally the timer is fed by a fourth of the basic clock frequency, which is called Fosc/4. In a system running a 4 MHz, the timer sees a clock running at 1 MHz. If the pre-scaler is set for I:8, the clock will be slowed down by another eight times, and the timer will see a clock at 125 kHz. See Figure 6.2 (for Timer1) in the data sheet to see how this applies to Timer1. A post-scaler is applied after the timer count exceeds its maximum value, generating an overflow condition. The post-scaler setting determines how may overflows will go by before an interrupt is triggered. If the post-scaler is set for 1:16, the timer will overflow 16 times before an interrupt flag is set. The upper diagram on page 55 (for Timer2) of the data sheet shows ...

TIMERS AND COUNTERS:THE WATCHDOG TIMER

THE WATCHDOG TIMER A Watchdog Timer is a timer that sets an interrupt that tells us that for some reason the program has hung up or otherwise gone awry. As such it is expected that in a prop- erly written and compiled program the Watchdog Timer will never set an interrupt. This is accomplished by resetting the Watchdog Timer every so often in the program. The compiler inserts these instructions automatically into the program if the Watchdog Timer option is selected. However, setting the option does not guarantee that a pro- gram cannot or will not hang up. Software errors and infinite loops that reset the timer within themselves can still cause hang ups. The Watchdog Timer is scalable. It shares its scaler with Timer0 on an exclusive basis. Either it uses the scaler, or Timer0 uses it. They cannot both use it at the same time. See the discussion under Timer0 in the data sheet for more information (Chapter 5). Since PICBASIC PRO assumes that the Watchdog Timer will be run with a 1:...

CONTROLLING THE OUTPUT AND READING THE INPUT:THE LCD DISPLAY

Image
THE LCD DISPLAY This section describes the use of and interactions with existing hardware connections as they come with the LAB-X1 module. Other wiring schemes can be used with ease as defined in the compiler manual. The LCD is controlled from PORTD, and all eight bits of this port are connected to the LCD. You therefore have the choice of using only the four high bits as a 4-bit data path for the LCD or using all eight bits. The entire port is also connected to eight of the LEDs on the 10-light LED bar graph. (The two leftmost LEDs in the bar graph are used to indicate that the power to the LAB-X1 is on.) The four high bits, bits D4 to D7, cannot be used for any other purpose if the LCD is being used. The software does not release these four bits automatically after using them to transfer information to the LCD, but you do have the option of saving the value of PORTD before using the LCD and then restoring this value after the LCD has been written to. The complication, of course, i...

TIMERS AND COUNTERS

If you have no knowledge about timers, you should read this chapter a few times. However, there is some repetition in the other chapters to allow each part of the book to stand as an independent resource. Most users will find that using the timers and the counters is the hardest part of learning how to use PIC microcontrollers. With this in mind, we will proceed in a step-by-step manner and build up the programs in pieces that are easier to digest. Once you get comfortable with their setup procedures, you will find that timers and counters are not so intimidating. We will cover timers and counters separately. Counters are essentially timers that get their clock input from an outside source. There are two counters in the 16F877A, and they are associated with Timer0 and Timer1. Timer2 cannot be used as a counter because there is no input line (internal or external) for this particular counter. Note The clock frequency utilized by the timers is a fourth of the oscillator fre- quency. ...

CONTROLLING THE OUTPUT AND READING THE INPUT:EXERCISES

EXERCISES Answers to these problems are not provided. Since this is really all about input and output, a comprehensive set of exercises that focus specifically on input and output have been provided. We need to be completely comfortable with these I/O functions before we start on running motors, so you are encouraged to expand on these exercises on your own. These and similar techniques will be used to control and respond to all the ancillary devices that we will use with our motors. LED EXERCISES: CONTROLLING TH E LIGHT EMITTING DIODES (LEDS) We will learn more about controlling the output from the LAB-X1 by writing a series of increasingly complicated programs that will control the ten-segment LED IC pro- vided on the LAB-X1. In these exercises we are controlling the LEDs, but the control strategies developed will apply to any kind of “on/off” devices that we will connect to the LAB-X1 or to any other device that we may design. 1. One at a time, light the eight LEDs on t...