Вы находитесь на странице: 1из 10

Interfacing Peripheral Devices

with
PIC18F4520 Microcontroller.

AHMAD NASHRIQ ABDUL MUTALIB


A35890744
GROUP 9
ECE 480 – SPRING 2008
EXECUTIVE SUMMARY
This application note will discuss how to interface many peripheral
devices at once using PIC18f4520 microcontroller. It will explain a
basic example of using logic gates to control the peripheral devices.
This application note also will show the steps to set up the
microcontroller and an example of coding in C language for the
microcontroller.

INTRODUCTION
Peripheral device is any device attached to microcontroller in order
to expand its functionality. The examples of peripheral devices are
LCD, sensors, heater and blower. In designing a device or system,
we might be using many peripheral devices that need to be
controlled by the microcontroller on the same time. To connect the
peripheral devices to the microcontroller, we have to attach each
peripheral device to the pins of the microcontroller. Sometime, we
might be running out of pins since the pins of the microcontroller
are limited. Thus, it is important to have an efficient way to control
many peripheral devices at once.

OBJECTIVE
The purpose of this application notes is to show the reader how to
implement interface logic that can be used by the microcontroller to
handle many peripheral devices. This application notes also will
provide example of program codes for the microcontroller in C
language. The reader can easily change the example in order to get
a new design. The interface logic will be built using logic gates,
inverter and buffer while the PIC18F4520 will be used as the
microcontroller.
DESIGN BACKGROUND

Figure 1

i) Microcontroller
For this project, the PIC 18F4520 will be used as the
microcontroller. To program the microcontroller, we will use the
MPLAB IDE v7.41. We also need the Winford R-11 header, proto-
board, resistors, switch, and 40 Mhz clock for this project.

ii) Interface Logic


To handle multiple peripheral devices at once, we need interface
logic as shown in the Figure 1. There are many kind of interface
logic that can be used such as logic gates, decoder, and
comparator. This application notes will focus on implementing the
interface logic using logic gates. For that purpose, we need
1) DM74LS11 Triple 3-Input AND Gate
http://www.egr.msu.edu/eceshop/Parts_Inventory/datasheets
/74ls12.pdf
2) SN5404 Hex Inverter
http://www.egr.msu.edu/eceshop/Parts_Inventory/datasheets
/74ls04.pdf
3) SN7407N Hex Buffer
http://www.egr.msu.edu/eceshop/Parts_Inventory/datasheets
/sn7407.pdf

ii) Peripheral Device.


To simplify the design, LED will be used as the peripheral device. It
makes this project easier to debug when we are using the LED . We
can make sure the state of the peripheral device by using this LED
since it will light up when it is in high state (Logic 1) and turn off
when it is in low state (Logic 0). All of the LED will be connected in
series with current limiter resistor.

PROCEDURE

1) Set up the microcontroller.

a) Build circuit as shown in the Figure 2 on a proto-board.

Figure 2

b) Connect the Winford R-11 header to the PIC as shown in the


http://www.egr.msu.edu/classes/ece480/goodman/ForMiniprojects/
Lab3.pdf.

2) Set up the logic gates and peripheral device (LED)

a) Connect the buffer to the DM74LS11 as shown in Figure 2. This


buffer will provide a binary combination for the design. Since we will
have eight peripheral devices, we need three inputs to each gate
that connect the LED.

b) Port D2 of PIC will be used input and Port C will be used as


outputs. Build the circuit shown in Figure 2. Do not connect
anything on the last gates of the third DM74LS11 chips.
Port on PIC Pin Connect to
RC4 23 All "A" Pin in DM74LS11except the last one
RC5 24 All "B" Pin in DM74LS11except the last one
RC6 25 All "C" Pin in DM74LS11except the last one
Table 1

Figure 3

c) If the voltage coming out of PINC4, C5 or C6 are weak, connect a


buffer (SN7407N) to amplify the signal before connecting the wire
to the DM74LS11.
3) Programming the Microcontroller

a) Start MPLAB. It is located in Start > All Programs > Microchip >
MPLAB IDE v7.40 > MPLAB IDE .Under Projects, start the Project
Wizard. Select Next.
b) In this project, we are using the PIC18F4520. Make sure this
device is selected, and hit Next.
c) Make sure the Active Toolsuite is set to Microchip C18 Toolsuite.
Select Next.
d) This is the project name. Type the project name.
e) The window will prompt you to add an existing file. You need to
select the file located in C:\ >Program Files > Microchip >
MCC18 > lkr > 18f4520.lkr. Your screen should look like the
figure 4.

f) Go to File > New. Copy and paste this block of code into the new
window. This program has one input. When the switch is pushed,
LED 2 will be on for some instruction cycles so that we can notice
the change. Then, it will turn off and LED 3 will turn on. This loop
will continue for 6 times (6 LED will turn on and off). If the switch is
released, LED 1 and LED 8 will turn on and off.
#include <p18cxxx.h> // This is the library file that must be included
#include <delays.h> // Delay library
#pragma config WDT=OFF // Turn off the Wach Dog Timer
#define input PORTDbits.RD2 //Define the input,
#define A PORTCbits.RC4
#define B PORTCbits.RC5
#define C PORTCbits.RC6
#define mydelay Delay10KTCYx(500)

void main()
{
TRISD=0b00000100; //Set only PinRD2 as input, all other pin from Port D as output
TRISC=0; //All pins on Port C are output
while(1)
{
if (input==0) //if the switch in on
{
A = 0; B =0; C= 1; // LED 7 will turn on
mydelay; //provide delay so we can see the
// LED turning on and off.
A = 0; B =1; C= 0; // LED 6 will turn on
mydelay;
A = 0; B =1; C= 1; // LED 5 will turn on
mydelay;
A = 1; B =0; C= 0; // LED 4 will turn on
mydelay;
A = 1; B =0; C= 1; // LED 3 will turn on
mydelay;
A = 1; B =1; C= 0; // LED 2 will turn on
mydelay;
}
else //when the switch is not pushed, LED 1 and 8 will blink
{
A = 1; B =1; C= 1; // LED 1 will turn on
mydelay;
A = 0; B =0; C= 0; // LED 8 will turn on
mydelay;
}
}
}

g) Go to File > Save. Select the check box at the bottom labeled
Add File To Project. Select Save.

h) Connect the power supply (5V) to the circuit. Turn on the power
supply.

i) To run the program, follow this sequence: BUILD ALL >


PROGRAM > RUN
ANALYSIS

We are applying voltage from 3 sources that can be controlled by


the microcontroller. The pins are Pin 23, Pin 24 and Pin 25. We can
program the microcontroller to either output a high (about 5 V) or
low (about 0 V) to these pins. Since we have 3 pins, we have 8 bits
of data that can be use as output (2^3 = 8).The combinations of
data are listed on Table 2.

A B C
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Table 2

All three outputs out of the pins are connected to AND gates. The
AND gate will only produced a high voltage when all of the three
inputs coming in are high. By using inverter, we can control the
signal coming out of the pin and set the bit to match the condition.

Figure 4
As we can see from Figure 4, only one AND gate will produce a high
voltage with any combination of inputs A , B and C. Therefore, we
can control which peripheral device that we want to turn on by
programming the microcontroller and setting Pin A, B, and C to the
proper combination of high and low states. We also can use
karnaugh-map to change the location of the buffer. By doing that,
we can set up new combination of on and off state of the LED.
CONCLUSION

By using the AND gates as interface logic form the microcontroller


to the peripheral device (PD), we can control the PD easily. All we
need to do is connect the microcontroller to the gates and to the
PD. By doing this, we can minimize the number of pins of the
microcontroller. Without having the interface logic, we need to
connect the PD to eight pins of microcontroller if we have eight PD.
That is not an efficient method since the numbers of pins are
limited.

The example shown in this application note is just a simple example


on how to use this AND gates as an interface logic. Even though the
concept of using this AND gates is easy, the wiring of the circuit
might be complicated since we are running eight wires from the
same pin. Thus, it’s important to put a buffer to amplify the signal.

The advantage of using logic gates as interface logic is it is easy to


understand the concept. But this method has a scaling issue. The
more inputs we have the more gates are needed and that might
create problem when we wire the circuit.

Using logic gates as the interface logic also has disadvantage. When
we have too many PD, the wiring will be come messy and
complicated. So, it is recommended to use decoder or comparator if
these problems occur.
REFERENCE

1) http://www.egr.msu.edu/classes/ece480/goodman/ForMinipro
jects/Lab3.pdf
2) http://www.egr.msu.edu/eceshop/Parts_Inventory/datasheets
/74ls12.pdf
3) http://www.egr.msu.edu/eceshop/Parts_Inventory/datasheet
Z/sn7407.pdf
4) http://www.egr.msu.edu/eceshop/Parts_Inventory/datasheets
/sn7407.pdf
5) http://ulcape.org/wiki/Tutorial-PIC_Programming_Basics

Вам также может понравиться