You are on page 1of 23

8/9/2016

ĐẠI HỌC QUỐC GIA TP.HỒ CHÍ MINH


TRƯỜNG ĐẠI HỌC BÁCH KHOA
KHOA ĐIỆN-ĐIỆN TỬ
BỘ MÔN KỸ THUẬT ĐIỆN TỬ

Embedded System Design


Chapter 7: Peripherals for embedded
systems
7.1 Digital parallel input / output
7.2 LCD interfacing
7.3 Analog input /output
7.4 Serial communication

Bộ Môn Kỹ Thuật Điện Tử - ĐHBK

7.3. Analog Input
• Allows an external voltage to be converted to digital 
form, stored and processed
• This type of input occurs in data loggers, control 
systems, digital audio and signal processors…
• The PIC microcontrollers is designed specifically for high‐
speed analog signal processing

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 2

1
8/9/2016

Analog input with PIC 16 Series
Device Pins Features
16F873A 28 3 parallel ports,
16F876A 3 counter/timers,
2 capture/compare/PWM,
2 serial,
5 10-bit ADC,
2 comparators
16F874A 40 5 parallel ports,
16F877A 3 counter/timers,
2 capture/compare/PWM,
2 serial,
8 10-bit ADC,
2 comparators

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 3

The PIC16F87XA ADC module

Functional blocks for the A/D converter in medium-end PIC microcontrollers. A/D,
successive approximation analog-to-digital converter; SHA, sample-and-hold
Chapter 7 - p2 4
amplifier; AMUX, analog multiplexer.

2
8/9/2016

Analog Setup
U1
• The PIC 16F877 has eight analog  13
OSC1/CLKIN RB0/INT
33
14 34
OSC2/CLKOUT RB1
inputs: RA0, RA1, RA2, RA3,  1
MCLR/Vpp/THV RB2
35
36
RB3/PGM
RA5, RE0, RE1, RE2 being  2
3
RA0/AN0 RB4
37
38
RA1/AN1 RB5
renamed AN0 to AN7. 4
5
RA2/AN2/VREF-
RA3/AN3/VREF+
RB6/PGC
RB7/PGD
39
40
6
RA4/T0CKI
7 15
RA5/AN4/SS RC0/T1OSO/T1CKI
16
RC1/T1OSI/CCP2
8 17
RE0/AN5/RD RC2/CCP1
9 18
RE1/AN6/WR RC3/SCK/SCL
10 23
RE2/AN7/CS RC4/SDI/SDA
24
RC5/SDO
25
RC6/TX/CK
26
RC7/RX/DT
19
RD0/PSP0
20
RD1/PSP1
21
RD2/PSP2
22
RD3/PSP3
27
RD4/PSP4
28
RD5/PSP5
29
RD6/PSP6
30
RD7/PSP7
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2
PIC16F877
5

CCS C Analog Input Functions

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 6

3
8/9/2016

CCS C Analog Input Functions

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 7

CCS C Analog Input Functions

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 8

4
8/9/2016

Chapter 7 - p2 9

Example
U1
13 33
RV1 14
OSC1/CLKIN RB0/INT
34
OSC2/CLKOUT RB1
1 35
MCLR/Vpp/THV RB2
36
RB3/PGM
2 37
RA0/AN0 RB4
3 38
RA1/AN1 RB5
4 39
RA2/AN2/VREF- RB6/PGC
5 40
RA3/AN3/VREF+ RB7/PGD
1k 6
RA4/T0CKI
7 15
RA5/AN4/SS RC0/T1OSO/T1CKI
16
RC1/T1OSI/CCP2
8 17
RE0/AN5/RD RC2/CCP1
9 18
RE1/AN6/WR RC3/SCK/SCL
10 23
RE2/AN7/CS RC4/SDI/SDA
24
RC5/SDO
25 LCD1
RC6/TX/CK
26
RC7/RX/DT VDD
19
RD0/PSP0 RXD
20
RD1/PSP1
21
RD2/PSP2 VSS
22
RD3/PSP3
27 MILFORD-2X16-BKP
RD4/PSP4
28
RD5/PSP5
29
RD6/PSP6
30
RD7/PSP7
PIC16F877

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 10

5
8/9/2016

Example

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 11

Voltage measurement
RV5(1)
V=2.71739
LCD1
R3 VDD

120R RXD

R1 VSS
680R
RV5 MILFORD-2X16-BKP

U1(RA3/AN3/VREF+)
V=2.55838
RV4 U1
1k 13 33
OSC1/CLKIN RB0/INT
14 34
RV3 1
OSC2/CLKOUT RB1
35
MCLR/Vpp/THV RB2
36
RB3/PGM
1k 2 37
RA0/AN0 RB4
3 38
RA1/AN1 RB5
4 39
RA2/AN2/VREF- RB6/PGC
1k 5 40
RA3/AN3/VREF+ RB7/PGD
6
RA4/T0CKI
7 15
RV1 RA5/AN4/SS RC0/T1OSO/T1CKI
16
RC1/T1OSI/CCP2
8 17
RV6 RV2 9
RE0/AN5/RD RC2/CCP1
18
RE1/AN6/WR RC3/SCK/SCL
10 23
RE2/AN7/CS RC4/SDI/SDA
1k 24
RV7 RC5/SDO
25
RC6/TX/CK
26
RC7/RX/DT
1k
RV8 19
RD0/PSP0
1k 20
RD1/PSP1
21
RD2/PSP2
22
RD3/PSP3
1k R2 RD4/PSP4
27
4k7 28
RD5/PSP5
29
RD6/PSP6
1k D1 RD7/PSP7
30

BZX79C2V7 PIC16F877

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 12

6
8/9/2016

Voltage measurement

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 13

7.3. Analog Output
Basic digital to analogue converter

a) general DAC hardware b) output voltage

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 14

7
8/9/2016

DAC

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 15

DAC Circuit
• Typical application

A1 A2 A8 
Vo  10V     
  2 4 256 

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 16

8
8/9/2016

Ex: DAC0808 & Serial DAC

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 17

Waveform Generator

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 18

9
8/9/2016

Waveform Generator Source Code (1/3)

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 19

Waveform Generator Source Code (2/3)

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 20

10
8/9/2016

Waveform Generator Source Code (3/3)

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 21

Class Assignment
• Design a system to read temperature data from the 
sensor LM35, and show the result on LCD

LCD1
LM016L
VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E
1
2
3

4
5
6

7
8
9
10
11
12
13
14

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 22

11
8/9/2016

Serial communication
Serial transmission of binary data consists of sending the bits of a word one
by one in a consecutive form and using the same pins.

Parallel vs. Serial IO

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 23

simplex vs half‐duplex vs full‐duplex

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 24

12
8/9/2016

Serial Communication

1.PIC16 USART Serial Link
2.PIC16 SPI Serial Bus
3.I2C Serial Bus

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 25

USART
• The universal synchronous/asynchronous receive transmit (USART) device is 
typically used in asynchronous mode to implement off‐board, one‐to‐one 
connections. 
• The term asynchronous  means no separate clock signal is needed to time the 
data reception, so only a data send, data receive, and ground wires are 
needed. 
• It is quick and simple to implement if a limited data bandwidth is acceptable. 

USART Operation

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 26

13
8/9/2016

USART
• RS232 driver
– MAX232

USART RS232 Signal

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 27

USART with PIC microcontroller
• The PIC has a dedicated hardware RS232 port, 
but CCS C allows any pin to be set up as an 
RS232 port, providing functions to generate the 
signals in software.

(a) Connection between two PICs (b) Synchronous mode connection. CK is


in asynchronous mode the clock pin, output in the master device
and input in the slave device

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 28

14
8/9/2016

CCS RS232 Serial Port Functions

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 29

RS232 Periperal Simulation

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 30

15
8/9/2016

The program

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 31

Outline

1.PIC16 UART Serial Link
2.PIC16 SPI Serial Bus
3.I2C Serial Bus

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 32

16
8/9/2016

SPI Bus
• The serial peripheral interface (SPI) bus provides high‐speed 
synchronous data exchange over relatively short distances 
(typically within a set of connected boards), using a master/slave 
system with hardware slave selection
• One processor must act as a master, generating the clock. Others 
act as slaves, using the master clock for timing the data send and 
receive. 
• The slaves can be other microcontrollers or peripherals with an 
SPI interface.
• The SPI signals are: 
– Serial Clock (SCK)
– Serial Data In (SDI)
– Serial Data Out (SDO)
– Slave Select (!SS)
Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 33

SPI Bus

SPI Connection

SPI Signals

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 34

17
8/9/2016

SPI Operation
• To transfer data, the master selects a slave device to 
talk to, by taking its SS line low. 
• Eight data bits are then clocked in or out of the slave 
SPI shift register to or from the master. No start and 
stop bits are necessary, and it is much faster than 
RS232. 
• The clock signal runs at the same speed as the master 
instruction clock, that is, 5MHz when the chip is 
running at the maximum 20MHz (16 series MCUs).

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 35

SPI Driver Functions

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 36

18
8/9/2016

• SPI Test 
System 
Schematic

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 37

Master and slaves source code

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 38

19
8/9/2016

Outline

1.PIC16 UART Serial Link
2.PIC16 SPI Serial Bus
3.I2C Serial Bus

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 39

I2C Bus
• The interintegrated circuit (I2C) bus is designed for 
short‐range communication between chips in the same 
system using a software addressing system. 
• It requires only two signal wires and operates like a 
simplified local area network.

I2C connection

I2C signals

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 40

20
8/9/2016

I2C Bus
• The I2C slave chips are attached to a two‐wire 
bus, which is pulled up to logic 1 when idle. 
Passive slave devices have their register or 
location addresses determined by a combination 
of external input address code pins and fixed 
internal decoding.
• As for SPI, the clock is derived from the 
instruction clock, up to 5MHz at the maximum 
clock rate of 20MHz.

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 41

I2C Bus
• To send a data byte, the master first sends a control code to set 
up the transfer, then the 8‐bit or 10‐bit address code, and finally 
the data. Each byte has a start and acknowledge bit, and each 
byte must be acknowledged before the next is sent, to improve 
reliability.
• The sequence to read a single byte requires a total of 5 bytes to 
complete the process, 3 to set the address, and 2 to return the 
data

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 42

21
8/9/2016

I2C Functions

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 43

I2C Test System
DSW1
ON OFF +5V
13 33 1 9
OSC1/CLKIN RB0/INT
14 34 2
OSC2/CLKOUT RB1
1 35 3
MCLR/Vpp/THV RB2
36 4
RB3/PGM
2 37 5
RA0/AN0 RB4
3 38 6
RA1/AN1 RB5
4 39 7
RA2/AN2/VREF- RB6/PGC
5
RA3/AN3/VREF+ RB7/PGD
40 8 R1 R2
6 4k7 4k7
RA4/T0CKI
7 15 DIPSWC_8
RA5/AN4/SS RC0/T1OSO/T1CKI
16
8
RC1/T1OSI/CCP2
17 U2
RE0/AN5/RD RC2/CCP1
9 18 6 1
RE1/AN6/WR RC3/SCK/SCL SCK A0
10 23 5 2
RE2/AN7/CS RC4/SDI/SDA SDA A1
24 7 3
RC5/SDO WP A2
25
RC6/TX/CK
26 24FC256
RC7/RX/DT
19
RD0/PSP0
20
RD1/PSP1
21
RD2/PSP2 SDA
22
RD3/PSP3
RD4/PSP4
RD5/PSP5
27
28
29
SCL I2C
RD6/PSP6 TRIG
30
RD7/PSP7
PIC16F877A
U1

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 44

22
8/9/2016

I2C Test System

Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter 7 - p2 45

23

You might also like