Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

DEC40053 EMBEDDED SYSTEM APPLICATIONS

TOPIC 5
HARDWARE INTERFACING
Part 1:
Basic hardware connection
LEARNING OUTCOMES 2

At the end of this chapter, student should be able


to:
1 Identify the minimum hardware connection so
that the PIC microcontroller can operate.
2 Identify I/O pin of PIC used for interfacing.

3 Explain the digital input/output in PIC

4 Give examples of digital I/O circuit.

5 Give examples of C program to read and write


digital signal at I/O pins.
Minimum hardware connection of 3

embedded system
To make this microcontroller function, you need to give a DC power
supply, a reset circuit and a quartz crystal (system clock) from external
source. In order to make it work as embedded system we need to add
suitable inputs and outputs circuitry.

Figure 5.1: Microcontroller connection of embedded system.


PIC18F4550 Pin Diagram 4

2. Port B

1. Port A

5. Port E
4. Port D

3. Port C

Figure 1
Basic connection of PIC 5

microcontroller
In order to enable the microcontroller to operate properly it is necessary
to connect to power supply, reset circuitry and clock.

2. Reset Signal

1. Power supply

3. Clock Signal

Figure 5.2: Basic connection of


PIC microcontroller
Clock Signal 6

 Clock signal is used to execute microcontroller instruction.


 Example of external clock sources are :
1. Crystal oscillator
2. Resonator.
3. RC circuit
 Clock signal is supply through external OSC pin of
microcontroller.

Crystal Oscillator

 However modern microcontroller comes with both external and


internal ( built-in) oscillator.
Reset Circuit 7

 Reset circuit consist of push button and resistor, connecting the


reset pin (MCLR) used to restart the system.

Figure 5.3: Reset Circuit of PIC microcontroller


Digital Input Output in PIC
8

• Digital I/O state should be in logic “LOW” or logic “HIGH” in any


given time.
• The following diagram show the relationship between the logic
state and the value of voltage for input and output of TTL device.

Digital input and output voltage levels


Input & Output Devices
9

• In embedded system, a microcontroller must be


interfaced with input and output (I/O) devices to make it
useful.

INPUT OUTPUT
DEVICES DEVICES

Example of input and output devices


Example of I/O Devices 10
Digital Input
11

 Digital inputs have two states: off and on. If voltage is


flowing, the circuit is on. If it’s not flowing, the circuit is off.
 To make a digital circuit, you need a circuit, and a movable
conductor which can either complete the circuit, or not.
 Switches can be used as operator controls. They can sense
if a door is open or closed. A limit switch can detect if a
part of a machine has reached a certain position.
 One of the complicating issues with mechanical switches is
they can bounce (oscillate on and off) when touched and
when released. The contact bounce varies from switch to
switch and from time to time, but usually bouncing is a
transient event lasting less than 5ms.
 We can eliminate the effect of bounce if we design
software that waits at least 10 ms between times we read
the switch values.
 Besides switches, digital sensors also provide digital signal
to microcontroller which serve the same function as
switches.
Active LOW & Active HIGH switch 12

Active Active
LOW HIGH
The resistor R1(name as pull-up The 'Active HIGH' example works in
resistor) 'pulls' the PIC input high the opposite way,
(logic '1').
R2 is a 'pull down' resistor, holding
When the switch is pressed, the PIC input at logic '0', pressing
it pulls the voltage down to the switch connects the PIC input to
zero, changing the input to the 5V rail, forcing it to logic '1'.
logic '0'.  
 

I/O pin I/O pin


Digital output 13

 The simplest control you can use over an electrical device is digital output. In
this case, you would either turn something off, or on.
 Example of output devices are LED, buzzer, bulb, speaker, motor,
solenoid .etc. Below is example of output devices used in embedded system
applications.
 The simple digital output is LED.
 Connecting the LED without a resistor is likely to damage both the LED and
the PIC. To calculate the resistor needed for a simple LED circuit, simply take
the voltage drop away from the source voltage then apply Ohm's Law.

• where:
VS is the source voltage, measured in volts (V),
VLED is the voltage drop across the LED, measured in volts (V),
ILED is the current through the LED*, measured in Amperes (Amps/A), and
R is the resistance, measured in Ohms (Ω).
* The current through the circuit is constant so I LED is also the current through the resistor.
• Example: LED has a forward voltage drop of 2.1V and at 20mA output.
R = 5V – 2.1V  = 145 Ω (use the higher resistor value)
LED circuit 14

LED can be connected to microcontroller pin in two way:


 
Sink
Source mode
mode

If you direct the output to HIGH, you When you set the output pin to
put 5V over the LED and resistor so LOW, you are sinking current to
current flows from the I/O pin to the I/O pin, meaning flowing
ground through the LED and resistor. current from 5V to the I/O pin
In this case, you are sourcing current through the LED and resistor.
from the output, meaning flowing
current from the pin through the LED,

When you set the output pin to


 If you direct the output to LOW, the
HIGH, the potential on I/O pin is
potential on both sides of the LED
higher, so no current flows through
and resistor is zero so no current
the LED and resistor.
flows.
Example 1 15

Sketch a complete circuit for a control system that used


PIC18F4550 microcontroller interface with a switch (active
LOW) connected to RC1 and a LED (active LOW) connected
to RC3.

Solution:
Activity 1 16

A control system use PIC18f4550 microcontroller


connected to a switch (active High) and a LED (sink mode).
The switch is connected to pin RB0 and the LED is
connected to pin RB5.Draw a schematic diagram for
control system.
Buzzer 17

 Same as the LED, buzzer is a simple output component that can be used as
sound indicator when there is an emergency.
 The buzzer will buzz continuously when power is provided (5V) and will shut
down when the power is being cut off (0V). In other word, this is an active
high configuration.
 Refer to Figure 4.5 for the connection of buzzer with PIC Microcontroller
Relay 18

 Relay is an electrical device that is used as a switch in electrical


system with high current or voltage.
 In direct connection, a microcontroller is unable to provide or
resist large current and voltage.
 However, using Relay, microcontroller is able to control
connection with high current and voltage.

Relay

Connection between MCU and high current and


voltage devices using relay

• However, In order to prevent the appearance of high voltage self-induction,


caused by a sudden stop of the current flow through the coil, an inverted
polarized diode is connected in parallel to the coil.
• The purpose of this diode is to 'cut off' the voltage peak.
1919

LCD – Liquid Crystal Dislpay


 A 16x2 LCD means it can  It will be controlled in 2 mode:
display 16 characters per line  8 pins (8-bit interface)
and there are 2 such lines.

 Each character is displayed in  4 pins (4-bit interface)


5x7 pixel matrix.

 It can be used to display:


 numerical information
 text message
 special symbol.
2020

LCD Register
 This LCD has two registers:
The choice between the two registers is made by the register selector (RS) signal as detailed the following table

1. Command register
stores the command instructions to do a predefined task like
initializing it, clearing its screen, setting the cursor position,
controlling display etc.

2. Data register
stores the data to be displayed on the LCD. The data is the ASCII
value of the character to be displayed on the LCD.
2121

LCD Pins

RS = 0 (for sending command)


RS = 1 ( for sending character/data)
R/W=0 (write data to LCD)
R/W=1 (read data from LCD)
VEE - Connected to variable resistor
to control brightness
DB0 – DB7 (data I/O pins)
2222

LCD Commands
LCD Commands Bit 2323

Bit Bit Function Bit = Low (0) Bit = High (1)


Name

RS Command or Character Command Character


ID Set cursor move direction Decrement cursor Increment cursor
position position
S Specifies to shift the No display shift Display shift
display
D Sets On/Off of all display Display off Display on
C Cursor On/Off Cursor off Cursor on
B Cursor blinks Cursor blink off Cursor blink on
SC Sets cursor move or Move cursor Shift Display
display
shift

RL Shift direction Shift left Shift right


DL Sets interface data length 4‐bit interface 8‐bit interface
N Number of display line 1 line 2 lines
F Character font 5x7 dots 5x10 dots
2424

Cursor Positioning

Address
• The LCD contains a certain amount of memory which is assigned
to the display represented with the following "memory map“:
• Each position has their own address.
2525

Sending Command to LCD


Example 1:
Write instructions to send command to LCD to
clear display.

Solution:

LCD_RS =0; //RS=0

LCD_Data= 0b00000001; //clear display

LCD_E=1; //E=1
delay_ms (2); //delay 2ms
LCD_E=0; //E=0
delay_ms (2); //delay 2ms
2626

Sending Character to LCD


Example 2:
Write instructions to send character ‘P’ to
LCD display

Solution:

LCD_RS =1; //RS=1

LCD_Data= ‘P’; //send ASCII ‘P”


//to LCD data line

LCD_E=1; //E=1
delay_ms (2); //delay 2ms
LCD_E=0; //E=0
delay_ms (2); //delay 2ms
Example 3
2727

Display character ‘A’ at first line


(home position)

Solution:

LCD_RS=0;
LCD_DATA=0x80|0x00; //move cursor to address 0x00
LCD_E = 1; // set E clock to HIGH
delay_ms(2); // delay 2ms
LCD_E = 0; // falling of E clock
delay_ms(2); // delay 2ms

LCD_RS=1; //Select RS=1 to send character


LCD_DATA=‘A’; //send ASCII ‘A’ to LCD display
LCD_E = 1; // set E clock to HIGH
delay_ms(2); // delay 2ms
LCD_E = 0; // falling of E clock
delay_ms(2); // delay 2ms
Example 4
2828

Display character ‘A’ at location 45

LCD_RS=0;
LCD_DATA=0x80||0x45; //move cursor to 0x45
LCD_E = 1; // set E clock to HIGH
delay_ms(2); // delay 2ms
LCD_E = 0; // falling of E clock
delay_ms(2); // delay 2ms

LCD_RS=1;
LCD_DATA=‘A’;
LCD_E = 1; // set E clock to HIGH
delay_ms(2); // delay 2ms
LCD_E = 0; // falling of E clock
delay_ms(2); // delay 2ms
2929

Initialize LCD 8-bit mode


// Coding for LCD initializition
STEP: LCD_BACKLIGHT=1;
LCD_E = 1; // set E clock to HIGH
delay_ms(10); // delay 10ms

1. Send command 0x38 to LCD LCD_RS = 0; // set RS to LOW for sending command

- initialize LCD into 8 bit mode, 2 LCD_DATA = 0x38;


LCD_E = 1;
// initialize LCD into 8 bit mode
// set E clock to HIGH
line display, 5x7 dots font size. delay_ms(2); // delay 2ms
LCD_E = 0; // falling of E clock
2. Send command 0x06 to LCD delay_ms(2); // delay 2ms

- configure LCD into increment LCD_DATA = 0x06;


display.
// increment cursor after each character

cursor after each character LCD_E = 1; // set E clock to HIGH


delay_ms(2); // delay 2ms
display. LCD_E = 0; // falling of E clock
3. Send command 0x0F to LCD
delay_ms(2); // delay 2ms

LCD_DATA = 0x0F; // display on, cursor on and


- configure LCD into display on, //cursor blink
LCD_E = 1; // set E clock to HIGH
cursor on and cursor blink. delay_ms(2); // delay 2ms
4. Send command 0x01 to LCD LCD_E = 0;
delay_ms(2);
// falling of E clock
// delay 2ms
- to clear the LCD and bring LCD_DATA = 0x01; // clear display
LCD_E = 1; // set E clock to HIGH
cursor to home. delay_ms(2); // delay 2ms
LCD_E = 0; // falling of E clock
delay_ms(2); // delay 2ms
3030

LCD Function
Initialize LCD display
E_clock
void lcd_initialize(void)
{
LCD_E=1; void E_clock (void)
delay_ms(10); {
LCD_RS=0; LCD_E=1;
delay_ms(2);
LCD_DATA=0x38;
LCD_E=0;
E_clock();
delay_ms(2);
LCD_DATA=0x06;
}
E_clock();
LCD_DATA=0x0F;
E_clock();
LCD_DATA=0x01;
E_clock();
}
3131

LCD Function

Display character Display string

void put_char (char data) void put_string (const char *s) 


{ {         
//set lcd to display mode     while (s && *s)
LCD_RS=1;
//lcd data port = data lcd_send_char (*s++);
LCD_DATA = data; }
E_clock();
}
3232

Example 5
Write a program to display ‘ABC’ #include <xc.h>
in first line on LCD by sending void put_char (char data);
character one by one
void main(void)
{
TRISA=0;
TRISD=0;
lcd_initialize();
lcd_goto(0x80); //force cursor to 1st line
put_char (‘A');
put_char (‘B');
put_char (‘C');

while (1);
}

void put_char (char data)


{
LCD_RS=1; //set lcd to display mode
LCD_DATA = data; //display data on LCD
E_clock();
}
3333

Example 6
Write a program to display #include <xc.h>
‘POLISAS’ in column 6 of first #include “lcd.h”
line on LCD by sending all
characters as a string
void main(void)
{
TRISA=0;
TRISD=0;

lcd_initialize();
//force cursor to column 6 of 1st line
lcd_goto(0x85);
put_string (“POLISAS”);

while (1);
}
3434

Example 7
unsigned int num=123;
Write a program to display the
numbers stored in the variable. void main (void)
{
TRISA=0; TRISD=0b00000000;
ADCON1=0x0F;

lcd_initialize();
lcd_goto(0x80); //force cursor to 1st line
put_string (“NUM = ”);

put_char (0x30+(num/100)%10); //hundreds


put_char (0x30+(num/10)%10); //tens
put_char (0x30+(num/1)%10); //ones

while(1);
}
Reference 35

https://en.wikipedia.org/wiki/Microcontroller
http://rowebots.com/en/services/embedded-hardware-design-and-developme
nt
http://www.embedded.com/design/
http://www.tigoe.com/pcomp/code/controllers/input-output/controlling-high-
current-circuits/
http://www.mikroe.com/

You might also like