Sie sind auf Seite 1von 51

CHAPTER 5: HARDWARE

INTERFACING
By: Zunainah Binti Hamid

5.1 KNOW EXTERNAL DEVICES


THAT CAN BE INTERFACED
WITH PIC MICROCONTROLLER

Learning Outcomes:
1. Identify the minimum hardware
connection so that the PIC microcontroller
can operate.
2. Identify the bits of configuration register to
select features that are appropriate to the
hardware used.
3. Describe the features of external devices
that can be interfaced with PIC
microcontroller.
4. Identify I/O pin of PIC used for interfacing.

INTRODUCTION TO HARDWARE
INTERFACING
In PIC18, depending on the device selected and
features enabled, there are up to five ports
available.
All pins of the I/O ports are multiplexed with one
or more alternate functions from the peripheral
features on the device.
In general, when a peripheral is enables, that
pin may not be used as a general purpose I/O
pin
Refer datasheet on I/O multiplexed features
pg136

Available Features in PIC18 for


external devices interfacing
1. Capture/Compare/PWM Modules
2. Enhanced Universal
Synchronous/Asynchronous Receiver
Transmitter (EUSART) Module
3. Analog-to-Digital Converter (ADC) Module
4. Master Synchronous Serial Port (MSSP1
and MSSP2) Module
5. Digital-to-Analog Converter (DAC) Module
6. Etc.

Available Features in PIC18 for


external devices interfacing
1. Capture/Compare/PWM Modules
2. Enhanced Universal
Synchronous/Asynchronous Receiver
Transmitter (EUSART) Module
3. Analog-to-Digital Converter (ADC) Module
4. Master Synchronous Serial Port (MSSP1
and MSSP2) Module
5. Digital-to-Analog Converter (DAC) Module
6. Etc.
COVER IN THIS CHAPTER

5.2 UNDERSTAND THE


INTERNAL FEATURES OF THE
PIC MICROCONTROLLER

Learning Outcomes:
1. Explain the digital input/output in
PIC
2. Give examples of simple digital I/O
circuit
3. Give examples of C Program to read
and write digital signal at I/O pins.
4. Explain the Analog-to-Digital
Converter (ADC) module in the PIC.

Learning Outcomes:
5. Give examples of C program to read
analogue signal from external
devices.
6. Explain the Pulse Width
Modulation (PWM) feature in the PIC
7. Give examples of C program to
create PWM output

Learning Outcomes:
8. Explain the Universal Synchronous
Receiver/Transmitter (USART) setting in
the PIC according to the external
devices features
9. Give examples of C program to
configure USART setting in the PIC
according to the external devices
features.

ADC MODULE
Analog to Digital Converter (ADC) is a
device that converts an analog quantity
(continuous voltage) to discrete digital
values.
Very useful when processing on physical
quantities, which are normally analog in
nature (speed, pressure, temperature etc.).
Most of the PIC Microcontrollers have built
in ADC Module with 8-input channel.

ADC MODULE
In PIC18, ADC allows conversion of
analog input signal to a 10-bit binary
signal.
Analog input are multiplexed into a
single sample and hold circuit. The
connected to converter circuit
(Successive Approximation
Conversion).
Then, a 10-bit binary result stores
into the ADC registers (ADRESL &

BASIC ADC Converter

EXAMPLE OF ANALOG
DEVICES
1.
2.
3.
4.
5.

Light Sensors
Temperature Sensors
Touch Screens
Microphones for audio recording
And many more.

ADC Block Diagram

ADC REGISTERS
PIC ADC has the following registers :
ADCON0 ADC Control Register 0
ADCON1 ADC Control Register 1
ADCON2 ADC Control Register 2
ADRESH ADC Result High Register
ADRESL ADC Result Low Register

ADCON : A/D CONTROL REGISTER


To control the microcontroller
operation such as:
Power of ADC Circuit
Start converting
Channel selection
ADC Voltage reference selection
ADC result format presentation
(left/right justify)

ADCON0 REGISTER

ADCON0 REGISTER
The CHS <4:0> bits are used to select the
ADC input channel .
Example, by setting all these bits to
0011 means channel 3 or AN3 port was
selected
ADCON0=0b00001101 // ADC channel AN3

ADON = ON(1)/OFF(0) ADC circuit/module


GO/DONE = 1 to start the conversion
ADCON0bits.GO=1;
while (ADCON0bits.GO); // Wait conversion done

ADCON1 REGISTER

ADCON1 REGISTER
To tell ADC peripheral either
internal/external voltage references.
0 = internal reference voltage
ADCON1=0b00000000; // Use Internal
Voltage Reference (Vdd and Vss)

ADCON2 REGISTER

ADCON2 Register
To set 10-bit ADC result
representation in ADRESL and
ADRESH)
To select ADC acquisition time
To set ADC conversion clock
ADFM = 1 for right justify, 0 for
LEFT justify

EXAMPLE OF C
PROGRAM TO
READ ANALOG
SIGNAL

EXAMPLE OF C
PROGRAM TO
READ ANALOG
SIGNAL

ADC Module on Port A

Refer attachment: Using the ADC and


display on LCD

Pulse Width Modulation


(PWM)
The Capture/Compare/PWM Module is
a peripheral which allows the user to
time and control different events
Also to generate PWM signals of
varying frequency and duty cycle.
In PIC18, there have 3 Enhanced CCP
modules (ECCP1-ECCP3) and 2
standard CCP modules(CCP4-CCP5)

Pulse Width Modulation


(PWM)
PWM is a scheme that provides power to a
load by switching quickly between fully on
and fully off states.
PWM signal resembles a square ware
signal. High portion considered as ON
States (Pulse Width) while LOW portion
considered as OFF state
When more power supplies to the load its
will lengthens the pulse width and also
increase the number of steps applied.

PWM RESOURCES

PWM APPLICATIONS
1. Motor Speed Control
2. Servo Control
3. Varying the brightness of an LED

PWM CALCULATION
PWM Period = Total amount of ON
& OFF time
PWM Resolution = The max
number of steps that can be present
in a single PWM Period.
Duty Cycle = The proportion of the
ON time to the OFF time and
expressed in %.

CCP PWM Output Signal

PWM OPERATION
In PWM, the period, duty cycle, and
resolution are controlled by the
following registers:
i.
ii.
iii.
iv.

PRx
TxCON
CCPRxL
CCPxCON

CCPxCON REGISTER

Setup PWM Operation

PWM Timer Resource


The PWM standard mode makes use
of one of the 8-bit Timer2/4/6 timer
resources to specify the PWM Period

PWM Period
PWM Period is specified by the PRx
register .
The PWM Period can be calculated
using :

PWM DUTY CYCLE

PWM PULSE WIDTH

PWM DUTY CYCLE RATIO

PWM RESOLUTION

Example of using PWM in C


(1)
This example generates a 1 kHz PWM
signal on CCP1/RC2
Given clock frequency = 1 MHz. So
the T = 4 s.
PWM Period = PWM period =
(PR2+1) * 4*Tosc* prescaler value .
(Given PR2= 249)
So PWM Period = 1 ms

Example of using PWM in C


(2)
PWM pulse width = CCPR1L * Timer 2
presacler value * Tcy
Initially, CCPR1L = 125, the Timer 2
prescaler = 1 and Tcy = 4us. Initially
therefore,
PWM period = 125 * 1 * 4us = 0.5ms

C Code

EUSART
In Lab 6

5.3 APPLY THE INTERNAL FEATURES


OF THE PIC MICROCONTROLLER TO
INTERFACE WITH EXTERNAL DEVICES

5.3.1 Apply Digital I/O to interface


with LCD and Keypad 4X4
In Lab 4

5.3.2 Use ADC Module to interface


with an analogue sensor
Doing project

5.3.3 Produce the PWM to control the


current and voltage flow into the DC Load

In Lab 5

Das könnte Ihnen auch gefallen