Sunteți pe pagina 1din 7

Supplying the microcontroller

Generally speaking, the correct voltage supply is of utmost importance for the proper functioning of the
microcontroller system. For a proper function of any microcontroller, it is necessary to provide a stable
source of supply, a sure reset when you turn it on and an oscillator. According to technical specifications by
the manufacturer of PIC microcontroller, supply voltage should move between 2.0V to 6.0V in all versions.
The simplest solution to the source of supply is using the voltage stabilizer LM7805 which gives stable +5V
on its output. One such source is shown in the picture below.

In order to function properly, or in order to have stable 5V at the output (pin 3), input voltage on pin 1 of
LM7805 should be between 7V through 24V. Depending on current consumption of device there are several
versions of voltage stabilizer LM7805. For current consumption of up to 1A, version TO-220 is used. If the
total consumption is 50mA, 78L05 (stabilizer version in small TO - 92 packaging for current of up to
100mA) is used.

Dr. Oday A.L.A Ridha 40


I/O PORTS
Almost any embedded system needs to transfer digital data between its CPU and the outside world. This transfer falls into a
number of categories, which can be summarized as:
• Direct user interface, including switches, keypads, light-emitting diodes (LEDs) and displays
• Input measurement information, from external sensors, possibly being acquired through an analog-to-digital converter
• Output control information, for example to motors or other actuators
• Bulk data transfer to or from other systems or subsystems, moving in serial or parallel form, for example sending serial data to
an external memory. With this plethora of data coming and going, it is likely we will need to have a variety of digital inputs
and/or outputs. These are divided broadly into serial and parallel. In serial data transfer, the information is transferred one bit at a
time. Only a single interconnection is used to carry the data itself, although other lines are usually included for synchronization
and control. In parallel data transfer, a set (for example, eight) of interconnections is used. Each of these can carry 1 bit, and each
works in parallel with the others. Data can thus be transferred in groups of bits, for example in bytes. Parallel input/output (I/O) is
the workhorse for all the basic data interchange of a microcontroller, including interfacing with switches, LED, displays and so
on. A group of parallel I/O interconnections, appearing on the pins of the microcontroller, is called a parallel port. The general
I/O port is shown in the figure.

The PIC16F8X has two ports, PORTA and PORTB. Some port pins are multiplexed with an alternate function for other features
on the device.

a) PORTA and TRISA Registers

PORTA is a 5-bit wide latch. RA4 is a Schmitt Trigger input and an open drain
output. All other RA port pins have TTL input levels and full CMOS output
drivers. All pins have data direction bits (TRIS registers) which can configure
these pins as output or input. Setting a TRISA bit (=1) will make the
corresponding PORTA pin an input, i.e., put the corresponding output driver in
a hi-impedance mode. Clearing a TRISA bit (=0) will make the corresponding
PORTA pin an output, i.e., put the contents of the output latch on the selected
pin. Reading the PORTA register reads the status of the pins whereas writing to
it will write to the port latch. All write operations are read-modify-write
operations. So a write to a port implies that the port pins are first read, then this
value is modified and written to the port data latch. The RA4 pin is multiplexed
with the TMR0 clock input.

Dr. Oday A.L.A Ridha 41


EXAMPLE: INITIALIZING PORTA
CLRF PORTA ; Initialize PORTA by
; setting output
; data latches
BSF STATUS, RP0 ; Select Bank 1
MOVLW 0x0F ; Value used to
; initialize data
; direction
MOVWF TRISA ; Set RA<3:0> as inputs
; RA4 as outputs
; TRISA<7:5> are always
; read as '0'.

Dr. Oday A.L.A Ridha 42


b) PORTB and TRISB Registers

PORTB is an 8-bit wide bi-directional port. The corresponding data direction register is TRISB. A '1' on any bit in the TRISB
register puts the corresponding output driver in a hi-impedance mode. A '0' on any bit in the TRISB register puts the contents of
the output latch on the selected pin(s). Each of the PORTB pins have a weak internal pull-up. A single control bit can turn on all
the pull-ups. This is done by clearing the RBPU (OPTION_REG<7>) bit. The weak pull-up is automatically turned off when the
port pin is configured as an output. The pull-ups are disabled on a Power-on Reset. Four of PORTB’s pins, RB7:RB4, have an
interrupt on change feature. Only pins configured as inputs can cause this interrupt to occur (i.e., any RB7:RB4 pin configured as
an output is excluded from the interrupt on change comparison). The pins value in input mode are compared with the old value
latched on the last read of PORTB. The “mismatch” outputs of the pins are OR’ed together to generate the RB port change
interrupt.

This interrupt can wake the device from SLEEP. The user, in the interrupt service routine, can clear the interrupt in the following
manner:
a) Read (or write) PORTB. This will end the mismatch condition.
b) Clear flag bit RBIF.
A mismatch condition will continue to set the RBIF bit. Reading PORTB will end the mismatch condition, and allow the RBIF bit
to be cleared. This interrupt on mismatch feature, together with software configurable pull-ups on these four pins allow easy
interface to a key pad and make it possible for wake-up on key-depression. The interrupt on change feature is recommended for
wake-up on key depression operation and operations where PORTB is only used for the interrupt on change feature. Polling of
PORTB is not recommended while using the interrupt on change feature.

Dr. Oday A.L.A Ridha 43


EXAMPLE: INITIALIZING PORTB
CLRF PORTB ; Initialize PORTB by
; setting output
; data latches
BSF STATUS, RP0 ; Select Bank 1
MOVLW 0xCF ; Value used to
; initialize data
; direction
MOVWF TRISB ; Set RB<3:0> as inputs
; RB<5:4> as outputs
; RB<7:6> as inputs

Light-Emitting Diodes - LEDs


LEDs are surely one of the most commonly used elements in electronics. LED is an abbreviation for 'Light Emitting
Diode'. When choosing a LED, several parameters should be looked at: diameter, which is usually 3 or 5 mm (millimeters),
working current which is usually about 10mA (It can be as low as 2mA for LEDs with high efficiency - high light output), and
color of course, which can be red or green though there are also orange, blue, yellow....LEDs must be connected around the
correct way, in order to emit light and the current-limiting resistor must be the correct
value so that the LED is not damaged or burn out (overheated). The positive of the supply
is taken to the anode, and the cathode goes to the negative or ground of the project
(circuit). In order to identify each lead, the cathode is the shorter lead and the LED "bulb"
usually has a cut or "flat" on the cathode side. Diodes will emit light only if current is
flowing from anode to cathode. Otherwise, its PN junction is reverse biased and current
won't flow. In order to connect a LED correctly, a resistor must be added in series that to
limit the amount of current through the diode, so that it does not burn out. The value of the
resistor is determined by the amount of current you want to flow through the LED.
Maximum current flow trough LED was defined by manufacturer. High-efficiency LEDs
can produce a very good output with a current as low as 2mA.
To determine the value of the dropper-resistor, we need to know the value of the supply voltage. From this we subtract
the characteristic voltage drop of a LED. This value will range from 1.2v to 1.6v depending on the color of the LED. The answer
is the value of Ur. Using this value and the current we want to flow through the LED (0.002A to 0.01A) we can work out the
value of the resistor from the Ohm’s Law R=Ur/I.
LEDs are connected to a microcontroller in two ways. One is to turn them on with logic zero, and other to turn them on
with logic one. The first is called NEGATIVE logic and the other is called POSITIVE logic. The above diagram shows how they
are connected for POSITIVE logic. Since POSITIVE logic provides a voltage of +5V to the diode and dropper resistor, it will
emit light each time a pin of port B is provided with a logic 1 (1 = HIGH output). NEGATIVE logic requires the LED to be
turned around the other way and the anodes connected together to the positive supply. When a LOW output from the
microcontroller is delivered to the cathode and resistor, the LED will illuminate.

EXAMPLE (homework): For the schematic diagram shown, write an assembly program to make all LED’s blinking
as follows: for 1 second, all LED’s are ON and then, for 2 seconds, all LED’s are OFF.

Dr. Oday A.L.A Ridha 44


Keyboards or Switches

Keyboards are mechanical devices used to execute a break or make connection between two points.
They come in different sizes and with different purposes. Key function is simple. When we press a key, two
contacts are joined together and connection is made. Still, it isn't all that simple. The problem lies in the
nature of voltage as an electrical dimension, and in the imperfection of mechanical contacts. That is to say,
before contact is made or cut off, there is a short time period when vibration (oscillation) can occur as a
result of unevenness of mechanical contacts, or as a result of the different speed in pressing a key (this
depends on person who presses the key). The term given to this phenomena is called SWITCH
(CONTACT) DEBOUNCE. If this is overlooked when program is written, an error can occur, or the
program can produce more than one output pulse for a single key press. In order to avoid this, we can
introduce a small delay when we detect the closing of a contact. This will ensure that the press of a key is
interpreted as a single pulse. The debounce delay is produced in software and the 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 key press debounce.

EXAMPLE (homework): For the schematic diagram shown, write an assembly program that works as follows:
1) If switch T1 is pressed, the LED lights for 1 second then stops lighting.
2) If switch T2 is pressed, the LED lights for 2 seconds then stops lighting.
3) If both switches (T1 and T2) are pressed simultaneously, the LED continuously blinking (.5 second ON and .5
second OFF).
4) Otherwise the LED is OFF.

Dr. Oday A.L.A Ridha 45


Dr. Oday A.L.A Ridha 46

S-ar putea să vă placă și