Professional Documents
Culture Documents
Delay routine using for loop assume crystal frequency, XTAL=10 MHz
Delay routine using for loop assume crystal frequency , XTAL=20 MHz
AIM: Interfacing Push buttons, LEDs, Relay & Buzzer to PIC 18f Microcontroller.
PROBLEM STATEMENT:
To write a program for interfacing button, LED, relay & buzzer as follow,
A. when button 1 is pressed relay and buzzer is turned ON and LED’s start chasing from
left to right
B. when button 2 is pressed relay and buzzer is turned OFF and Led start chasing from
right to left
OBJECTIVE:
a. To understand the PORT Structure of PIC Microcontroller.
b. To study the SFRs to control the PORT Pins.
c. To interface common peripherals like pushbuttons, LEDs, relay.
d. To understand the use of MPLAB IDE, XC8 and C18 Compiler.
e. To write a simple program in Embedded C.
LAB REQUIREMENT:
THEORY:
Depending on the device selected, there are up to five general purpose
I/O ports available on PIC18F Microcontroller devices. Some pins of the I/O
ports are multiplexed with an alternate function from the peripheral features on the
device.
In general, when a peripheral is enabled, that pin may not be used as a
generalpurpose I/O pin.
1. Some common Features of the I/O Ports
• Up to 70 bi-directional I/O pins
- Some multiplexed with peripheral functions
• High drive capability
- 25mA source/sink capability
• Direct, single cycle bit manipulation
• 4kV ESD protection diodes
- Based on human body model
• After reset:
- Digital I/O default to Input (Hi-Z)
- Analog capable pins default to analog
TRIS register (Data Direction register): To select PORT pin as input or output.
All port pins are input by default. Whenever a bit in the TRISx register is a 0, the
corresponding bit in PORTx is an output. If the bit in TRISx is a 1, the
corresponding bit in PORTx is an input.
LAT register (output latch): The data latch (LAT register) is useful for read
modify-write operations on the value that the I/O pins are driving.
3. Buzzer
Buzzer is an electrical device, which is similar to a bell that makes a buzzing noise
and is used for signaling. Typical uses of buzzers include alarm devices, timers and
confirmation of user input such as a mouse click or keystroke, following diagram shows
buzzer connections to PIC.
4. LED (Light emitting diode)
5. Relay
Relays are switches that open and close circuits electromechanically or electronically.
Relays control one electrical circuit by opening and closing contacts in another circuit. As relay
diagrams show, when a relay contact is normally open (NO), there is an open contact when the
relay is not energized. When a relay contact is Normally Closed (NC), there is a closed contact
when the relay is not energized. In either case, applying electrical current to the contacts will
change their state.
6. Switch
Pushbuttons or switches connect two points in a circuit when you press them. Following
diagram shows SW1 connections to PIC Microcontroller. When switch is pressed corresponding
controller pin reads logic ‘0’.
INTERFACING DIAGRAM:
CONNECTIONS:
1. Keep switch SW20 Between 1-2 position and SW21 Between 2-3 position, to
Connect LED’s to PORTB
2. Keep switch SW22 Between 2-3 position, to Connect switches to RD0,RD1
And buzzer to RD2.
3. Switch is not used, to Connect relay to RA4.
ALGORITHM:
1. Start
2. Initialize respective PORT pins as output using TRIS register, where LEDs, Relay
and Buzzer are connected.
3. Initialize respective PORT pins as input using TRIS register, where Switch1 and
Switch2 are connected.
4. Check if SW1 is pressed Relay and Buzzer is turned ON and LED’s start chasing
from left to right.
5. Check if SW2 is pressed Relay and Buzzer is turned OFF and LED’s start chasing
from right to left.
6. Otherwise turn off all the devices.
7. Repeat process for continuous scanning of the switch to be pressed.
8. Stop.
OUTPUT:
A. If we press SW1 then Four LED’s get rotating right. Relay connection from NC to
NO. And Buzzer Gets ON.
B. If we press SW2 then Four LED’s get rotating Left. Relay connection from NO to
NC. And Buzzer Gets OFF.
CONCLUSION:
QUESTIONS:
A] 0X8= B] 0X1D=
C] 0X6FC= D] 0X4C1A=
A] 0b1001= B] 0b11001001=
C] 0b1111001101= D] 0b111011110001=
Q.3] Find value of TRISB if pins RB0, RB4, RB5 configure as input & remaining pins as
output.
Ans.
Q.4] Find value of TRISC if pins RC1, RC4, RC5 configure as input & remaining pins as
output.
Ans.
Q.6] Find value of PORTB to send ‘1’ on RB0, RB4, RB5 pins & ‘0’ on remaining pins.
Ans.
Q.7] Find value of PORTA to send ‘1’ on RA2, RA4, RA5 pins & ‘0’ on remaining pins.
Ans.
Q.9] upon reset all ports of PIC are configured as ____________ (output, input)
Ans.
PROBLEM STATEMENT:
Write a program to display message on 16x2 LCD in Embedded C without using any
standard library function.
OBJECTIVE:
LAB REQUIREMENT:
THEORY:
LCD Interfacing:
In recent years the LCD is finding widespread use replacing LED’s. This is due to the
following reason:
1. The declining prices of LCD.
2. The ability to display numbers, characters, and graphics. This is in contrast to LEDs, which are
limited to numbers and few characters.
3. In corporation of a refreshing controller into the LCD, thereby reliving the CPU of the task of
refreshing the LCD. In contrast, the LED must be refreshed by the CPU to keep displaying the
data.
4. Ease of programming for characters and graphics.
a. RS = 0: the instruction command code register is selected, allowing the user to send a
command such as clear display, cursor at home.
b. RS = 1: the data register is selected, allowing the user to send the data to be displayed on
the LCD.
Read/write (R/W):
R/W input allows the user to write information to the LCD or read information from it.
R/W = 1 when reading, R/W = 0 when writing.
Enable (EN):
The enable pin is used by the LCD to latch information presented to its data pins. When
data is supplied to data pins, a high to low pulse must be applied to the pin in order for the LCD
to latch in the data present at the data pins. This pulse must be a minimum of 450ns wide.
ALGORITHM:
1. As LCD data bus is connected to PORTB and handshaking signal EN-RC0, RS- RC1,
configure these pins output writing 0x00 to the appropriated TRIS register.
2. Initialize LCD.
a. Write a commands {initialize 2 line and 5X7 matrix (0x38), Display ON cursor
OFF (0x0C), Increment &shift cursor right (0x06), Clear display screen (0x01)} to
LCD. (call command subroutine/function)
b. Give delay between each command. (call delay subroutine/function)
OUTPUT:
Message’WELCOME”is displayed on LCD at specified location.
CONCLUSION:
Questions:
1 VSS - GND
-
2 VCC - +5V
-
3 VEE - POT
-
4 RS I RC0
5 R/#W I GND
6 E I/O RC1
15 LED+ - +5V
-
16 LED- - GND
-
Q.2] Describe various LCD commands
Code/Command Description
in Hex
1
2
4
6
5
7
8
A
C
E
F
10
14
18
1C
80
C0
38
Q.3] To send the instruction code 01 to clear display, we must make RS=__________.
Q.4] To send the letter ‘A’ to be displayed on LCD, we must make RS=____________.
Q.5] How does LCD distinguish data from instruction codes when receiving information?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
PROBLEM STATEMENT:
Write a program interface 4X4 keypad and displaying key pressed on LCD.
OBJECTIVE:
LAB REQUIREMENT:
THEORY:
1. Matrix Keypad Operation:
Matrix Keypads are commonly used in calculators, telephones etc where a number of
input switches are required. Matrix keypad is made by arranging push button switches in row and
columns. In the straight forward way to connect a 4×4 keypad (16 switches) to a microcontroller
we need 16 inputs pins. Keypad is a widely used input device with lots of application in our
everyday life. From a simple telephone to keyboard of a computer, ATM, electronic lock, etc.,
keypad is used to take input from the user for further processing.
The keypad has 8 communication lines namely R1, R2, R3, R4,(ROWS) C1, C2, C3 and
C4. (COLUMNS) R1 to R4 represents the four rows and C1 to C4 represents the four columns.
When a particular key is pressed the corresponding row and column to which the terminals of the
key are connected gets shorted. For example if key 1 is pressed row R1 and column C1 gets
shorted and so on.
INTERFACING DIAGRAM:
2. Keyboard debounce:
When striking a keyboard key, the key oscillates against its contacts several times before
settling. When released, it bounces again until it reverts to its rest state. Although it happens on
such a small scale as to be invisible to the naked eye, it's sufficient for the computer to register
multiple key strokes inadvertently.
When any of the key is pressed i.e. zero on any of the key is detected, the controller waits
for 20mS for the key debounce and then scans the columns again.
a. Waiting for key debounce help to detect if the key press was not erroneous viz due to spike.
b. 20ms to 100ms delay also prevents the same key press from being interpreted as a multiple
key press.
CONNECTIONS:
1. Keep switch SW21 Between 1-2 position to use LCD and SW22 Between 1-2
position to use 4*4 keypad on board.
ALGORITHM:
1. Start.
2. Make respective rows input and columns ouput, using TRIS register.
3. Make all rows and columns ‘1’.
4. Now make one column ‘0’.
5. Scan all rows for zero.
6. If no keys are open (i.e. all rows equal to zeros.),go to step 5,else go to step 8.
7. If no keys are pressed (i.e. all rows equal to zeros.),go to step 5. else go to step 8.
8. Check for key debounce (call delay function).
9. If no keys are pressed (after debounce if all rows read zero), go to step 4,else go to
step 10.
10. Ground next column
11. Read the status of rows.
12. If key belongs to this column, go to step 13,else go to step 10.if while scanning
row=0,it means key pressed belongs to that column.
13. Find which key is pressed by identifying row which reads zero, this can be done as
follows,
14. Send KEY detected value to LCD for displaying it on the screen.
OUTPUT:
After pressing the key corresponding key pressed is displayed on LCD as below,
“Key pressed: A “
CONCLUSION:
Questions:
Q.1] what is Key debouncing?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________