Professional Documents
Culture Documents
with
PIC18F4520 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.
PROCEDURE
Figure 2
Figure 3
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.
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
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