Файл: Introduction to PIC Microcontrollers (Complete Guide to PIC).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 1214
Скачиваний: 0
Chapter 6 - Samples
length of the delay depends on the key, and the purpose of the key. The problem can be partially solved by adding a capacitor across the key, but a well-designed program is a much-better answer. The program can be adjusted until false detection is completely eliminated.
In some case a simple delay will be adequate but if you want the program to be attending to a number of things at the same time, a simple delay will mean the processor is "doing-nothing" for a long period of time and may miss other inputs or be taken away from outputting to a display.
The solution is to have a program that looks for the press of a key and also the release of a key. The macro below can be used for keypress debounce.
The above macro has several arguments that need to be explained:
TESTER macro HiLo, Port, Bit, Delay, Address
HiLo can be '0' or '1' which represents rising or falling edge where service subprogram will be executed when you press a key.
Port is a microcontroller's port to which a key is connected. In the case of a PIC16F84 microcontroller, it can be PORTA or PORTB.
Bit is port's line to which the key is connected.
Delay is a number from 0 to 255, used to assign the time needed for key debounce detection - contact oscillation - to stop. It is calculated as TIME = Delay x 1ms.
Address is the address where the micro goes after a key is detected. The sub-routine at the address carries out the required instruction for the keypress.
http://www.mikroelektronika.co.yu/english/books/6_04Poglavlje.htm (2 of 4) [30/12/2001 16:54:22]
Chapter 6 - Samples
Example 1: TESTER 0, PORTA, 3, .100, Tester1_below
Key-1 is connected to RA0 (the first output of port A) with a delay of 100 microseconds and a reaction to logic zero. Subprogram that processes key is found at address of label Tester1_above.
Example2: TESTER 0, PORTA, 2, .200, Tester2_below
Key-2 is connected to RA1 (the second output of port A) with 200 mS delay and a reaction to logic one. Subprogram that processes key is found at address of label Tester2_below.
The next example shows the use of macros in a program. TESTER.ASM turns LED on and off. The LED is connected to the seventh output of port B. Key-1 is used to turn LED on. Key-2 turns LED off.
http://www.mikroelektronika.co.yu/english/books/6_04Poglavlje.htm (3 of 4) [30/12/2001 16:54:22]
Chapter 6 - Samples
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/books/6_04Poglavlje.htm (4 of 4) [30/12/2001 16:54:22]
Chapter 6 - Samples
Optocouplers
Optocouplers combine a LED and photo-transistor in the same housing. The purpose of an optocoupler is to separate two parts of a circuit.
This is done for a number of reasons:
●Interference. One part of a circuit may be in a location where it picks up a lot of interference (such as from electric motors, welding equipment, petrol motors etc.) If the output of this circuit goes through an optocoupler to another circuit, only the intended signals will pass through the optocoupler. The interference signals will not have enough "strength" to activate the LED in the optocoupler and thus they are eliminated. To protect a section of the device. Typical examples are industrial units with lots of interferences which affect signals in the wires. If these interferences affect the function of control section, errors will occur and the unit will stop working.
●Simultaneous separation and intensification of a signal. A signal as low as 3v is able to activate an optocoupler and the output of the optocoupler can be connected to an input line of a microcontroller. The microcontroller requires an input swing of 5v and in this case the 3v signal is amplified to 5v. It can also be used to amplify the current of a signal. See below for use on the output line of a microcontroller for current amplification.
●High Voltage Separation. Optocouplers have inherent high voltage separation qualities. Since the LED is completely separate from the photo-transistor, optocouplers can exhibit voltage isolation of 3kv or higher.
Optocouplers can be used as input or output device. They can have additional functions such as Schmitt triggering (the output of a Schmitt trigger is either 0 or 1 - it changes slow rising and falling waveforms into definite low or high values). Optocouplers are packaged as a single unit or in groups of two or more in one housing. They are also called PHOTO INTERRUPTERS where a spoked wheel is inserted in a slot between the LED and phototransistor and each time the light is interrupted, the transistor produces a pulse.
Each optocoupler needs two supplies in order to function. They can be used with one supply, but the voltage isolation feature is lost.
Optocoupler on an input line
The way it works is simple: when a signal arrives, the LED within the optocoupler is turned on, and it shines on the base of a photo-transistor within the same housing. When the transistor is activated, the voltage between collector and emitter falls to 0.5V or less and the microcontroller sees this as a logic zero on its RA4 pin.
The example below is a counter, used for counting products on production line, determining motor
http://www.mikroelektronika.co.yu/english/books/6_05Poglavlje.htm (1 of 4) [30/12/2001 16:54:24]
Chapter 6 - Samples
speed, counting the number of revolutions of an axle etc.
Let the sensor be a micro-switch. Each time the switch is closed, the LED is illuminated. The LED 'transfers' the signal to the phototransistor and the operation of the photo-transistor delivers a LOW to input RA4 of a microcontroller. A program in the microcontroller will be needed to prevent false counting and an indicator connected to any of the outputs of the microcontroller will shows the current state of the counter.
Input line optocoupler example
http://www.mikroelektronika.co.yu/english/books/6_05Poglavlje.htm (2 of 4) [30/12/2001 16:54:24]
Chapter 6 - Samples
Optocoupler on an output line
An Optocoupler can be used to separate the output signal of a microcontroller from an output device. This may be needed for high voltage separation or current amplification. The output of some microcontrollers is limited to 25mA. The optocoupler will take the low-current signal from the microcontroller and drive a LED or relay, as shown below:
http://www.mikroelektronika.co.yu/english/books/6_05Poglavlje.htm (3 of 4) [30/12/2001 16:54:24]
Chapter 6 - Samples
Output line optocoupler example
The program for this example is simple. By delivering a logic '1' to the fourth pin of port A, the LED will be turned on and the transistor will be activated in the optocoupler. Any device connected to the output of the optocoupler will be activated. The transistor current-limit is about 250mA.
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/books/6_05Poglavlje.htm (4 of 4) [30/12/2001 16:54:24]
Chapter 6 - Samples
The Relay
The relay is an electromechanical device, which transforms an electrical signal into mechanical movement. It consists of a coil of insulated wire on a metal core, and a metal armature with one or more contacts.
When a supply voltage is delivered to the coil, current will flow and a magnetic field is produced that moves the armature to close one set of contacts and open another set. When power is removed from the relay, the magnetic flux in the coil collapses and produces a fairly high voltage in the opposite direction. This voltage can damage the driver transistor and thus a reverse-biased diode is connected across the coil to "short-out" the spike when it occurs.
http://www.mikroelektronika.co.yu/english/books/6_06Poglavlje.htm (1 of 4) [30/12/2001 16:54:26]
Chapter 6 - Samples
Connecting a relay to the microcontroller via a transistor
Many microcontrollers cannot drive a relay directly and so a driver transistor is required. A HIGH on the base of the transistor turns the transistor ON and this activates the relay. The relay can be connected to any electrical device via the contacts.
The 10k resistor on the base of the transistor limits the current from the microcontroller to that required by the transistor. The 10k between base and the negative rail prevents noise on the base from activating the relay. Thus only a clear signal from the microcontroller will activate the relay.
Connecting the optocoupler and relay to a microcontroller
A relay can also be activated via an optocoupler which at the same time strengthens the current from the output of the microcontroller and provides a high degree of isolation. High current optocouplers usually contain a 'darlington' output transistor to provide high output current.
Connecting via an optocoupler is recommended especially for microcontroller applications, where motors are activated as the commutator noise from the motor can get back to the microcontroller
http://www.mikroelektronika.co.yu/english/books/6_06Poglavlje.htm (2 of 4) [30/12/2001 16:54:26]
Chapter 6 - Samples
via the supply lines. The optocoupler drives a relay and the relay activates the motor.
The figure below shows the program needed to activate the relay, and includes some of the already discussed macros.
http://www.mikroelektronika.co.yu/english/books/6_06Poglavlje.htm (3 of 4) [30/12/2001 16:54:26]
Chapter 6 - Samples
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/books/6_06Poglavlje.htm (4 of 4) [30/12/2001 16:54:26]
Chapter 6 - Samples
Generating a sound
A Piezo diaphragm can be added to an output line of a microcontroller to deliver a whole range of tones, beeps and signals.
It is important to know there are two main types of piezo sound-emitting devices. One has active components inside the casing and only requires a DC supply for the "sounder" to emit a tone or beep. Generally the tones or beeps emitted by these "sounders" or "beepers" cannot be changed - they are fixed by the internal circuitry. This is not the type we are discussing in this article.
The other type consists of a piezo diaphragm and requires a signal to be delivered to it for it to function. Depending on the frequency of the waveform, the output can be a tone, tune, alarm or even voice messages.
In order for them to work we must deliver a cycle consisting of a HIGH and LOW. It is the change from HIGH to LOW or LOW to HIGH that causes the diaphragm to "dish" (move) to produce the characteristic "tinny" sound. The waveform can be a smooth change from one value to the other (called a sinewave) or a fast change (called a SQUARE WAVE). A computer is ideal for producing a square wave. The square wave delivery produces a slightly harsher output.
Connecting a piezo diaphragm is very simple. One pin is connected to the negative rail and the other to an output of a microcontroller, as shown in the diagram below. This will deliver a 5v waveform to the piezo diaphragm. To produce a higher output, the waveform must be increased and this requires a driver transistor and inductor.
http://www.mikroelektronika.co.yu/english/books/6_07Poglavlje.htm (1 of 5) [30/12/2001 16:54:29]
Chapter 6 - Samples
Connecting a piezo diaphragm to a microcontroller
As with a key, you can employ a macro that will deliver a BEEP ROUTINE into a program when needed.
BEEP macro has two arguments:
freq: frequency of the sound. The higher number produces higher frequency duration: sound duration. The higher the number, the longer the sound.
Example 1: BEEP 0xFF, 0x02
The output of the piezo diaphragm has the highest frequency and duration at 2 cycles per 65.3mS which gives 130.6 mS
Example2: BEEP 0x90, 0x05
The output of the piezo diaphragm has a frequency of 0x90 and duration of 5 cycles per 65.3mS. It is best to determine these macro arguments through experimentation and select the sound that best suits the application.
The following is the BEEP Macro listing:
http://www.mikroelektronika.co.yu/english/books/6_07Poglavlje.htm (2 of 5) [30/12/2001 16:54:29]
Chapter 6 - Samples
The following example shows the use of a macro in a program. The program produces two
http://www.mikroelektronika.co.yu/english/books/6_07Poglavlje.htm (3 of 5) [30/12/2001 16:54:29]
Chapter 6 - Samples
melodies which are obtained by pressing T1 or T2. Some of the previously discussed macros are included in the program.
http://www.mikroelektronika.co.yu/english/books/6_07Poglavlje.htm (4 of 5) [30/12/2001 16:54:29]
Chapter 6 - Samples
© Copyright 1999. mikroElektronika. All Rights Reserved. For any comments contact webmaster.
http://www.mikroelektronika.co.yu/english/books/6_07Poglavlje.htm (5 of 5) [30/12/2001 16:54:29]