23
ĐẠ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Ử Bùi Minh Thành – Bộ Môn Kỹ Thuật Điện Tử - ĐHBK 1 Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3)

Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

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

TRƯỜNG ĐẠI HỌC BÁCH KHOAKHOA ĐIỆN-ĐIỆN TỬ

BỘ MÔN KỸ THUẬT ĐIỆN TỬ

Bùi Minh Thành – Bộ Môn Kỹ Thuật Điện Tử - ĐHBK 11

Embedded System Design

Chapter 5: C Peripheral Interfaces

(Part 3)

Page 2: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

References

• Martin Bates, “Programming 8-bit PIC Microcontrollers in C”, Newnes, 2008

• PIC 16F877A datasheet

• PIC C Help

2

Page 3: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

Outline

1.PIC16 UART Serial Link

2.PIC16 SPI Serial Bus

3.I2C Serial Bus

3

Page 4: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

Outline

1.PIC16 UART Serial Link

2.PIC16 SPI Serial Bus

3.I2C Serial Bus

4

Page 5: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

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.

5

USART Operation

USART RS232 Signal

Page 6: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

• Any pair of pins can be used for this interface, as data rate is quite low, allowing the signals to be generated in software.

• However, a dedicated hardware port is provided, which must be used if an interrupt is needed

6

Page 7: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

CCS RS232 Serial Port Functions

7

Page 8: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

RS232 Periperal Simulation

8

Page 9: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

The program

9

Page 10: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

Outline

1.PIC16 UART Serial Link

2.PIC16 SPI Serial Bus

3.I2C Serial Bus

10

Page 11: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

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)

11

Page 12: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK 12

SPI Connection

SPI Signals

Page 13: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

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).

13

Page 14: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

SPI Driver Functions

14

Page 15: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

• SPI Test System Schematic

15

Page 16: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

Master and slaves source code

16

Page 17: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

Outline

1.PIC16 UART Serial Link

2.PIC16 SPI Serial Bus

3.I2C Serial Bus

17

Page 18: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

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.

18

I2C connection

I2C signals

Page 19: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

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.

19

Page 20: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

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

20

Page 21: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

I2C Functions

21

Page 22: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK

I2C Test System

22

RA0/AN02

RA1/AN13

RA2/AN2/VREF-4

RA4/T0CKI6

RA5/AN4/SS7

RE0/AN5/RD8

RE1/AN6/WR9

RE2/AN7/CS10

OSC1/CLKIN13

OSC2/CLKOUT14

RC1/T1OSI/CCP216

RC2/CCP117

RC3/SCK/SCL18

RD0/PSP019

RD1/PSP120

RB7/PGD40

RB6/PGC39

RB538

RB437

RB3/PGM36

RB235

RB134

RB0/INT33

RD7/PSP730

RD6/PSP629

RD5/PSP528

RD4/PSP427

RD3/PSP322

RD2/PSP221

RC7/RX/DT26

RC6/TX/CK25

RC5/SDO24

RC4/SDI/SDA23

RA3/AN3/VREF+5

RC0/T1OSO/T1CKI15

MCLR/Vpp/THV1

U1PIC16F877A

R14k7

R24k7

+5V

I2CSDA

SCL

TRIG

SCK6

SDA5

WP7

A01

A12

A23

U2

24FC256

OFFON1

2

3

4

5

6

7

8

9

DSW1

DIPSWC_8

Page 23: Embedded System Designbmthanh/ESD/Ch5_p3_v1.pdf · Embedded System Design Chapter 5: C Peripheral Interfaces (Part 3) Bùi Minh Thành - Bộ môn KỹThuậtĐiệnTử - ĐHBK

Bùi Minh Thành - Bộ môn Kỹ Thuật Điện Tử - ĐHBK 23