TOPIC 5 - Part 1 Interfacing
TOPIC 5 - Part 1 Interfacing
TOPIC 5
HARDWARE INTERFACING
Part 1:
Basic hardware connection
LEARNING OUTCOMES 2
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.
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
Crystal Oscillator
INPUT OUTPUT
DEVICES DEVICES
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'.
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
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,
Solution:
Activity 1 16
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
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
LCD Commands
LCD Commands Bit 2323
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
Solution:
LCD_E=1; //E=1
delay_ms (2); //delay 2ms
LCD_E=0; //E=0
delay_ms (2); //delay 2ms
2626
Solution:
LCD_E=1; //E=1
delay_ms (2); //delay 2ms
LCD_E=0; //E=0
delay_ms (2); //delay 2ms
Example 3
2727
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=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
1. Send command 0x38 to LCD LCD_RS = 0; // set RS to LOW for sending command
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
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);
}
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 = ”);
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/