You are on page 1of 22

Some Data Types widely used by C18 and XC8

Data Type Size in Bits Data Range

unsigned char 8 bit 0 to 255


char 8 bit -128 to +127
unsigned int 16 bit 0 to 65536
int 16 bit -32768 to +32767
unsigned short 16 bit 0 to 65536
short 16 bit 32768 to +32767
unsigned short long 24 bit 0 to 16,777,215
unsigned long 32 bit 0 — 4,294,967,295
long 32 bit -2,147,483,648 to 2,147,483,647

Delay routine using for loop assume crystal frequency, XTAL=10 MHz

void Delay_ms(unsigned int value)


{
unsigned int x;
unsigned char y;
for(x=0;x<value;x++)
for(y=0;y<165;y++);
}

Delay routine using for loop assume crystal frequency , XTAL=20 MHz

void Delay_ms(unsigned int value)


{
unsigned int x,y;
for(x=0;x<value;x++)
for(y=0;y<310;y++);
}
Minimum Connections for PIC 18F4550
Introduction to MPLAB IDE
MPLAB IDE is a software program that runs on a PC to develop applications for
Microchip microcontrollers. It is called an Integrated Development Environment, or IDE,
because it provides a single integrated environment to develop code for embedded
microcontrollers
Description of an embedded Systems
An embedded system is typically a design making use of the power of a small
microcontroller, like the Microchip PICmicro MCU or dsPIC Digital Signal Controller
(DSCs). These microcontrollers combine a microprocessor unit (like the CPU in a
desktop PC) with some additional circuits called ìperipheralsî, plus some additional
circuits on the same chip to make a small control module requiring few other external
devices. This single device can then be embedded into other electronic and mechanical
devices for low-cost digital control.
Differences between an Embedded Controller and a PC
The main difference between an embedded controller and a PC is that the embedded
controller is dedicated to one specific task or set of tasks. A PC is designed to run many
different types of programs and to connect to many different external devices. An
embedded controller has a single program and, as a result, can be made cheaply to
include just enough computing power and hardware to perform that dedicated task. A PC
has a relatively expensive generalized central processing unit (CPU) at its heart with
many other external devices (memory, disk drives, video controllers, network interface
circuits, etc.). An embedded system has a low-cost microcontroller unit (MCU) for its
intelligence, with many peripheral circuits on the same chip, and with relatively few
external devices.
The Development Cycle
The process for writing an application is often described as a development cycle, since it
is rare that all the steps from design to implementation can be done flawlessly the first
time. More often code is written, tested and then modified in order to produce an
application that performs correctly. The Integrated Development Environment allows the
embedded systems design engineer to progress through this cycle without the distraction
of switching among an array of tools. By using MPLAB IDE, all the functions are
integrated, allowing the engineer to concentrate on completing the application without the
interruption of separate tools and different modes of operation.
MPLAB IDE is a wrapper that coordinates all the tools from a single graphical user
interface, usually automatically. For instance, once code is written, it can be converted to
executable instructions and downloaded into a microcontroller to see how it works. In
this process multiple tools are needed: an editor to write the code, a project manager to
organize files and settings, a compiler or assembler to convert the source code to machine
code and some sort of hardware or software that either connects to a target
microcontroller or simulates the operation of a microcontroller.
Figure: Development cycle

MPLAB C18 C Compiler


MPLAB C18 C Compiler is a cross-compiler that runs on a PC and produces code that
can be executed by the Microchip PIC18XXXX family of microcontrollers. Like an
assembler, the MPLAB C18 compiler translates human-understandable statements into
ones and zeros for the microcontroller to execute. Unlike an assembler, the compiler does
not do a one-to-one translation of machine mnemonics into machine code.
MPLAB C18 takes standard C statements, such as “if(x==y)” and “temp=0x27”, and
converts them into PIC18XXXX machine code. The compiler incorporates a good deal of
intelligence in this process. It can optimize code using routines that were employed on
one C function to be used by other C functions. The compiler can rearrange code,
eliminate code that will never be executed, share common code fragments among
multiple functions, and can identify data and registers that are used inefficiently,
optimizing their access.

The MPLAB XC8 C Compiler


It is a free-standing, optimizing ISO C90 compiler. It supports all 8-bit PIC
microcontrollers: PIC10, PIC12, PIC16 and PIC18 series devices, as well as the
PIC14000 device. The compiler is available for several popular operating systems,
including 32- and 64-bit Windows (excluding Windows Server), Linux and Mac OS X.
EXPERIMENT NO: 1 DATE:

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:

MPLAB IDE, C18, XC8 Compiler, PIC Development Board.

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

2. SFR Associated with I/O Port


Each port has three registers for its operation

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.

PORT register (reads the levels on the pins of the device)

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.

Figure. TRISB register

Figure. SFRs related to I/O

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)

A light-emitting diode (LED) is a two-lead semiconductor light source. It is a p–n


junction diode, which emits light when activated. If LEDs are connected with common cathode
(connect cathode of all LEDs to ground), then logic ‘1’ has to be written to make LED ON. And
If LEDs are connected with common anode (connect anode of all LEDs to VCC), then logic ‘0’
has to be written to make LED ON. Following diagram shows 8 LED connections to PIC
Microcontroller.

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:

Q.1] Convert following numbers in Binary

A] 0X8= B] 0X1D=
C] 0X6FC= D] 0X4C1A=

Q.2] Convert following numbers in Hex

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.5] Write a C statement to configure (single pin) RD0 pin as input.


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.8] Write a C statement to send ‘1’ on pin (single pin) RD0.


Ans.

Q.9] upon reset all ports of PIC are configured as ____________ (output, input)
Ans.

Q.10] Relay used in this experiment operates on____________V.


Ans.
EXPERIMENT NO: 2 DATE:

AIM: Interfacing LCD to Display Message.

PROBLEM STATEMENT:

Write a program to display message on 16x2 LCD in Embedded C without using any
standard library function.

OBJECTIVE:

a. To understand the working of Liquid Crystal Display (LCD).


b. To study the LCD interfacing modes and Timing diagram.
c. To study and use of the LCD commands to drive LCD.
d. To interface LCD in 8-bit mode to PIC Microcontroller.

LAB REQUIREMENT:

MPLAB IDE, C18, XC8 Compiler, PIC Development Board.


.

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.

LCD pin descriptions:

Vcc, Vss and Vee:


Vcc and Vss provide +5V and ground, respectively, Vee is used for controlling LCD
contrast.

Register Select (RS):


There are two very important registers inside the LCD. The RS pin is
used for their selection as follows.

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.

Data bus (D0 – D7):


The 8-bit data pins, D0-D7 are used to send the information to the LCD or read the
contents of the LCD’s internal registers. To display the numbers and letters, we send ASCII
codes to these pins while making RS=1.
There are also instruction command codes that can be sent to the LCD to clear the display
or blink the cursor.
We also use RS = 0 to check the busy flag bit to see if the LCD is ready to receive
information. The busy flag is D& and can be read w hen R/W = 1 and RS=0.When D7 =1, the
LCD is busy taking care of internal operations and will not accept any new information. When
D7 = 0, the LCD is ready to receive new information.

Pin Assignment of 16x2 LCD:


INTERFACING DIAGRAM:
CONNECTIONS:

1. Keep switch SW21 Between 1-2 position to use LCD.

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)

3. Display message on first line.


a. Write a command 0x80 to LCD. (call command subroutine/function)
b. Load ASCII character from the given string.
c. Write data to LCD (call data subroutine/function)
d.Give delay between each data i.e character send. (call delay subroutine/function)
e. Repeat b, c and d.
4. Display message on Second line.
a. Write a command 0xC0 to LCD. (call command subroutine/function).
b. Load ASCII character from the given string.
c. Write data to LCD (call data subroutine/function)
d.Give delay between each data i.e character send. (call delay subroutine/function)
e. Repeat b, c and d.
5. Endless Loop
Write a command or Write a data.
1. Send 8-bit command or data on data port.
2. Make RS pin low (for command operation), Make RS pin high (for data
operation).
3. Latch data (Enable latch & Disable latch).

OUTPUT:
Message’WELCOME”is displayed on LCD at specified location.

CONCLUSION:
Questions:

Q.1] Describe LCD pins


Ans. 16x2 LCD:
This is a 16 pin, single line connector, designed for connection to standard, text LCD
modules.

Pin Symbol I/O Description Board


Connections

1 VSS - GND
-
2 VCC - +5V
-
3 VEE - POT
-
4 RS I RC0

5 R/#W I GND

6 E I/O RC1

7 DB0 I/O RB0

8 DB1 I/O RB1

9 DB3 I/O RB2

10 DB4 I/O RB3

11 DB4 I/O RB4

12 DB5 I/O RB5

13 DB6 I/O RB6

14 DB7 I/O RB7

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?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Q.6]What is purpose of’E’ line?


______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
EXPT.NO: 3 DATE:
AIM: Interfacing 4x4 Matrix Keypad to PIC Microcontroller.

PROBLEM STATEMENT:

Write a program interface 4X4 keypad and displaying key pressed on LCD.

OBJECTIVE:

a. To understand the basic operation of Matrix Keypad.


b. To study keypad interfacing techniques and key debounce.
c. To interface 4x4 Matrix Keypad to PIC Microcontroller.

LAB REQUIREMENT:

MPLAB IDE, C18 Compiler, PIC Development Board.

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.

To resolve this problem, the processor in a keyboard "debounces” the keystrokes, by


aggregating them across time to produce one "confirmed” key strokes that (usually) corresponds
to what is typically a solid contact. Early membrane keyboards limited typing speed because they
had to do significant debouncing.

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.

The debounce is necessary because:

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,

a) If C0=0&R0=0 Then KEY=’0’ a) If C1=0&R0=0 Then KEY=’4’


b) If C0=0&R1=0 Then KEY=’1’ b) If C1=0&R1=0 Then KEY=’5’
c) If C0=0&R2=0 Then KEY=’2’ c) If C1=0&R2=0 Then KEY=’6’
d) If C0=0&R3=0 Then KEY=’3’ d) If C1=0&R3=0 Then KEY=’7’

a) If C2=0&R0=0 Then KEY=’8’ a) If C3=0&R0=0 Then KEY=’C’


b) If C2=0&R1=0 Then KEY=’9’ b) If C3=0&R1=0 Then KEY=’D’
c) If C2=0&R2=0 Then KEY=’A’ c) If C3=0&R2=0 Then KEY=’E’
d) If C2=0&R3=0 Then KEY=’B’ d) If C3=0&R3=0 Then KEY=’F’

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?
______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Q.2] what is purpose of grounding each column in keyboard interfacing?


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

Q.3] what is the input to PIC microcontroller row if no key is pressed?


______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

______________________________________________________________________________

You might also like