You are on page 1of 51

om

Chuyên đề II

.c
Vi điều khiển và ứng dụng

ng
co
an
Serial Communication
th
o ng
du
u
cu

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Truyền thông nối tiếp

om
 2 the Universal Asynchronous Receiver and

.c
Transmitters (UARTs)

ng
 2 the SPI synchronous serial interfaces

co
 2 the I 2 C synchronous serial interfaces

an
th
o ng
du
u
cu

2
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Synchronous serial

om
.c
ng
co
an
th
o ng
du
u
cu

3
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Asynchronous Serial
Interfaces

om
.c
ng
co
an
th
o ng
du
u
cu

4
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Some comparisons

om
.c
ng
co
an
th
o ng
du
u
cu

5
CuuDuongThanCong.com https://fb.com/tailieudientucntt
More

om
.c
ng
co
an
th
o ng
du
u
cu

6
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Synchronous Communication
Using the SPI Modules

om
.c
ng
co
an
th
o ng
du
u
cu

7
CuuDuongThanCong.com https://fb.com/tailieudientucntt
SPI applications

om
 Interfacing with memory devices

.c
 Serial EEPROMs - e.g., 25xx256

ng
 Interfacing with codecs

co
 Control Ports or PCM Data

an
 Interfacing with communication chips

th
ng
Bluetooth
o
 Boot Loader
du
u
cu

8
CuuDuongThanCong.com https://fb.com/tailieudientucntt
om
.c
ng
co
an
th
o ng
du
u
cu

9
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Giải thích các bit điều khiển

om
.c
ng
co
an
th
o ng
du
u
cu

10
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Bít điều khiển

om
.c
ng
co
an
th
o ng
du
u
cu

11
CuuDuongThanCong.com https://fb.com/tailieudientucntt
SPI overview

om
 Serial transmission and reception of 8-bit or 16-bit

.c
data

ng
 Full-duplex, synchronous communication

co
 Compatible with Motorola’s SPI and SIOP

an
interfaces
 3-wire interface
th
o ng
 Supports 4 different clock formats and serial
du

clock speeds up to 10 Mbps


u
cu

 Buffered Transmission and Reception

12
CuuDuongThanCong.com https://fb.com/tailieudientucntt
SPI - Master / Slave

om
 SPI module can be configured as Master or Slave

.c
 In any SPI data transfer, there is a single Master

ng
and a single Slave

co
 Selected by MSTEN bit, SPIxCON<5>

an
 Master generates serial clock pulse (on SCK pin)

th
 SCK frequency determined by Primary
ng
 Prescaler bits (PPRE) and Secondary Prescaler
o
du

(SPRE) bits in SPIxCON register


u
cu

Fsck = Fcy / (PPRE * SPRE)

13
CuuDuongThanCong.com https://fb.com/tailieudientucntt
om
.c
ng
co
an
th
o ng
du
u
cu

14
CuuDuongThanCong.com https://fb.com/tailieudientucntt
SPI - Serial Clock Formats

om
 4 clock formats - set by CKP and CKE bits

.c
in the SPIxCON register

ng
 SCK is low when module is idle, SDO changes on

co
clock going high (CKP=0, CKE=0)

an
 SCK is low when module is idle, SDO changes on

th
clock going low (CKP=0, CKE=1)
ng
 SCK is high when module is idle, SDO changes on
o
du

clock going low (CKP=1, CKE=0)


 SCK is high when module is idle, SDO changes on
u
cu

clock going high (CKP=1, CKE=1)

15
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Ví dụ

om
.c
ng
co
an
th
o ng
du
u
cu

16
CuuDuongThanCong.com https://fb.com/tailieudientucntt
SPI - Transmission

om
 Module is enabled by setting SPIEN bit in the SPIxSTAT register
 Transmission begins when data is written into the Master’s Transmit

.c
Buffer

ng
 SCK pulses are generated by the Master only when SPIxSR

co
contains data
 Transmission can be disabled by setting the DISSDO bit in the

an
SPIxCON register

th
 SPIxBUF is buffered ng
 You can write SPIxBUF while data is being shifted out through
o
SPIxSR
du

 SPITBF bit in the SPIxSTAT register indicates that the Transmit


u

Buffer is full
cu

 Wait until SPITBF = 0 to write data


 Transmission of the new data starts as soon as SPIxSR is idle

17
CuuDuongThanCong.com https://fb.com/tailieudientucntt
SPI - Reception

om
 Reception occurs concurrently with the transmission
 When all bits of data have been shifted in through SPIxSR,

.c
SPIxSR contents are transferred to Receive Buffer

ng
 SPI interrupt (indicated by SPIIF bit and enabled by SPIIE bit)

co
is generated so that buffer can be read

an
 SPIxBUF subject to Receive Overflow

th
 SPIRBF bit in the SPIxSTAT register = 1 indicates that the
ng
Receive Buffer is full
o
 SPIxBUF must be read before new data is completely shifted in
du

When receive overflow occurs...


u
cu

 New data not transferred to Receive Buffer


 SPIROV bit in SPIxSTAT is set

18
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Configuration

om
.c
ng
co
an
th
o ng
du
u
cu

19
CuuDuongThanCong.com https://fb.com/tailieudientucntt
SPI - Data Sizes

om
 8-bit and 16-bit data communication

.c
 SPI operation is identical for both data sizes,

ng
except number of bits transmitted

co
 For 8-bit data, Master generates 8 SCK pulses

an
 For 16-bit data, Master generates 16 SCK pulses

th
 16-bit operation is selected by setting the
ng
MODE16 bit in the SPIxCON register
o
du
u
cu

20
CuuDuongThanCong.com https://fb.com/tailieudientucntt
SPI - Framed SPI

om
 SPI supports Frame Synchronization

.c
 Enabled by setting FRMEN bit in the SPIxCON

ng
register

co
 SCK pulses are continuous in this mode

an
th
o ng
du
u
cu

21
CuuDuongThanCong.com https://fb.com/tailieudientucntt
SPI - Framed SPI

om
 Frame Master generates Frame Sync pulses

.c
 Frame Master or Slave mode is selected by clearing

ng
or setting the SPIFSD bit in the SPIxCON register

co
 Shifting of data starts only after a Frame Sync

an
pulse is generated on the SS pin

th
 4 possible Framed SPI modes ng
 􀁏 SPI Master, Frame Master
o
du

 􀁏 SPI Master, Frame Slave


 􀁏 SPI Slave, Frame Master
u
cu

 􀁏 SPI Slave, Frame Slave

22
CuuDuongThanCong.com https://fb.com/tailieudientucntt
SPI - chức năng phụ

om
 Slave Select (SS) pin functionality

.c
 In this mode, the Slave functions only as

ng
 long as the SS pin is driven low

co
 Enabled by setting SSEN bit in the SPIxCON

an
register

th
 Slave Wake-up from SLEEP ng
 Since SCK pulses are provided by the Master, SPI
o
du

Slave can function in SLEEP


u

 Slave Reception wakes up the device from


cu

SLEEP

23
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Ví dụ ghép nối với Serial EEROM
25L256

om
// 1. init the SPI peripheral

.c
#define SPI_CONF 0 x 8120 // SPI on, 8-bit master, CKE=1,CKP=0

ng
TCSEE = 0; // make SSEE pin output

co
CSEE = 1; // de-select the EEPROM

an
SPI2CON = SPI_CONF; // select mode and enable

th
ng
// send one byte of data and receive one back at the same time
o
int writeSPI2( int i)
du

{
u

SPI2BUF = i; // write to buffer for TX


cu

while( !SPI2STATbits.SPIRBF); // wait for transfer complete


return SPI2BUF; // read the received value
}//writeSPI2

24
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Đọc Serial ROM
// 25LC256 Serial EEPROM commands
#define SEE_WRSR 1 // write status register

om
#define SEE_WRITE 2 // write command
#define SEE_READ 3 // read command

.c
#define SEE_WDI 4 // write disable

ng
#define SEE_STAT 5 // read status register

co
#define SEE_WEN 6 // write enable

an
th
o ng
du
u
cu

25
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Ví dụ chương trình đầy đủ

om
.c
ng
co
an
th
o ng
du
u
cu

26
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Writing/read Data to the
EEPROM

om
// send a Write command
CSEE = 0; // select the Serial EEPROM

.c
writeSPI2( SEE_WRITE); // send command, ignore data

ng
writeSPI2( ADDR_MSB); // send MSB of memory address

co
writeSPI2( ADDR_LSB); // send LSB of memory address
writeSPI2( data); // send the actual data

an
// send more data here to perform a page write

th
CSEE = 1; // start actual EEPROM write cycle
ng
// send a Write command
o
CSEE = 0; // select the Serial EEPROM
du

writeSPI2( SEE_READ); // send command, ignore data


u

writeSPI2( ADDR_MSB); // send MSB of memory address


cu

writeSPI2( ADDR_LSB); // send LSB of memory address


data=writeSPI2( 0); // send dummy, read data
// read more data here sequentially incrementing the address
CSEE = 1; // terminate the read sequence
27
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Read 32bit values

om
.c
ng
co
an
th
o ng
du
u
cu

28
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Write 32bit values

om
.c
ng
co
an
th
o ng
du
u
cu

29
CuuDuongThanCong.com https://fb.com/tailieudientucntt
DAC ví dụ MCP4921

om
.c
ng
co
an
th
o ng
du
u
cu

30
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Ví dụ phần mềm DAC

om
.c
ng
co
an
th
o ng
du
u
cu

31
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Serial Communications using
the dsPIC30F UART Module

om
 Serial transmission and reception

.c
 􀁏 8-bit or 9-bit data

ng
 􀁏 Full-duplex, asynchronous communication

co
 􀁏 Support for communication protocols such

an
 as RS-232, RS-422, RS-485 and LIN
th
ng
 􀁏 4-deep Transmit and Receive buffers
o

 􀁏 Transmit and Receive interrupts


du

 􀁏 Error detection
u
cu

 􀁏 Support for receiver addressing

32
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Cấu

om
.c
hình

ng
truyền

co
an
thống
th
o ng
du
u
cu

33
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Phần cứng

om
.c
ng
co
an
th
o ng
du
u
cu

34
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Các thanh ghi

om
.c
ng
co
an
th
o ng
du
u
cu

35
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Giải thích thanh ghi

om
.c
ng
co
an
th
o ng
du
u
cu

36
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Thanh ghi (tiếp)

om
.c
ng
co
an
th
o ng
du
u
cu

37
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Baud Rate Generator

om
 Dedicated 16-bit Baud Rate Generator

.c
 􀁏 Baud Rate controlled by UxBRG register

ng
 (x = 1 or 2)

co
 􀁏 Baud Rate = Fcy / ( 16 * (UxBRG + 1) )

an
 where Fcy = Instruction Cycle Frequency
th
ng
 􀁏 Both transmitting and receiving devices
o
du

 must use same Baud Rate


 􀁏 Bits are transmitted and/or received at
u
cu

 the rate defined by the Baud Rate

38
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Ví dụ

om
 If FCY=8MHz, and the desired baud rate is 9600,

.c
 UxBRG=(8•106/(16•9600))-1=51.083.

ng
 UxBRG=51.

co
 The new baud rate for UxBRG=51 is 9615.384,

an
i.e. the deviation is 0.16%
th
o ng
du
u
cu

39
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Ví dụ (tiếp)

om
.c
ng
co
an
th
o ng
du
u
cu

40
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Transmission

om
 The first bit transmitted is a START bit

.c
 􀁏 Low level on UxTX pin for 1 bit time

ng
 􀁏 START bit followed by data and parity

co
 bits

an
 􀁏 Data format (8 or 9 bits) and parity type
th
ng
 (even, odd or no parity) are configured
o
du

 by PDSEL control bits in UxMODE


u

 register
cu

 􀁏 No parity for 9-bit data

41
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Transmission

om
 Last bit transmitted is a STOP bit

.c
 􀁏 High level on UxTX pin for 1 or 2 bit

ng
 times

co
 􀁏 Number of STOP bits are configured by

an
 STSEL control bit in UxMODE register
th
ng
 􀁏 During a transmission, TRMT status
o
du

bit in the UxSTA register is clear


u
cu

42
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Transmit Buffers

om
 4-deep Transmit FIFO Buffer

.c
 􀁏 The characters in the buffer are shifted

ng
 out of the buffer through UxTSR

co
 􀁏 All 8 (or 9) data bits, are buffered

an
 􀁏 Only the first character in the buffer is
th
ng
 memory-mapped and thus useraccessible
o

 􀁏 The UTXBF status bit in the UxSTA


du
u

 register indicates if the buffer is full


cu

43
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Transmit Interrupts
 Transmit Interrupt indicated by UxTXIF bit and

om
enabled by UxTXIE bit

.c
 􀁏 When UTXISEL bit in the UxSTA register = 1

ng
 􀁏 Interrupt occurs when buffer becomes empty

co
 􀁏 Used for transmitting a block of 4 characters

an
th
 􀁏 When UTXISEL bit = 0 ng
 􀁏 Interrupt occurs whenever a character is
o
du

 transferred to UxTSR
u

 􀁏 Used for transmitting a single character


cu

In this mode, an interrupt is generated as soon as


the UTXEN bit is set
44
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Reception

om
 4-deep Receive FIFO Buffer

.c
 􀁏 All 8 (or 9) data bits, as well as Error

ng
 flags, are buffered

co
 􀁏 Only the first character in the buffer is

an
 memory-mapped (UxRXREG)
th
ng
 􀁏 The error flags in the UxSTA register
o
du

 reflect the error states of the first


u

 character in the buffer


cu

 􀁏 URXDA status bit in the UxSTA register


 indicates if the buffer contains new data
45
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Receive Interrupts

om
 Receive Interrupt indicated by UxRXIF bit and

.c
enabled by UxRXIE bit

ng
 When URXISEL bits in the UxSTA register = 11

co
 􀁏 Interrupt occurs when buffer becomes full

an
 􀁏 Used for receiving a block of 4 characters
 􀁏 When URXISEL bits = 10
th
ng
 􀁏 Interrupt when buffer has 3 characters
o
du

 􀁏 Used for receiving a block of 3 characters


u
cu

 􀁏 When URXISEL bits = 01 or 00


 􀁏 Interrupt whenever a character is received
 􀁏 Used for receiving a single character
46
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Error Detection
 Parity Error

om
 􀁏 When received parity does not match the parity

.c
calculated by module from received data

ng
 􀁏 Indicated by PERR bit in the UxSTA register set

co
 Framing Error

an
 􀁏 When a STOP bit is expected on UxRX pin but
th
ng
a low logic level is detected
o
 􀁏 Indicated by FERR bit in the UxSTA register set
du

 Receive Overrun Error


u
cu

 􀁏 When the Receive Buffer is full and a 5th


character is received
 􀁏 Indicated by OERR bit in the UxSTA register set
47
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Address Detection

om
 When the UART is operating in 9-bit mode

.c
(PDSEL = 11), and the ADDEN bit in the UxSTA

ng
register is set

co
 􀁏 The module will wait for an Address word, i.e., a

an
9-bit word with the 9th bit set

th
 At this stage, the URXISEL bits in the UxSTA
ng
register must be set to 00 or 01
o
 On receiving the Address word, the user inspects
du

the lower byte to verify an address match


u
cu

 If an address match occurred, the user should clear


the ADDEN bit, after which the module will wait for
Data words (9-bit words with MSB clear)
48
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Features for LIN Support

om
 Transmission of Break Characters

.c
 􀁏 A Break character can be transmitted by

ng
setting the UTXBRK bit in the UxSTA register for

co
at least 13 bit times

an
 Autobaud Detection

th
 The UxRX pin is internally routed to an Input
o ng
Capture pin (U1RX to IC1, U2RX to IC2)
du

 Used for capturing both edges of START bit


u
cu

for determining baud rate


 Enabled by setting the ABAUD bit in the
UxMODE register
49
CuuDuongThanCong.com https://fb.com/tailieudientucntt
UART - Additional Features

om
 Alternate I/O

.c
 Some devices have an alternate pair of TX/RX pins

ng
 Enabled by setting ALTIO bit in UxMODE register

co
 Loopback Mode

an
 UxTX pin internally connected to UxRX pin

th
 Enabled by setting LPBACK bit in UxMODE
ng
Register
o
du

 Wake-up from SLEEP


u
cu

 Device can be woken up from SLEEP by START bit


 Enabled by setting WAKE bit in UxMODE register

50
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Ví dụ

om
.c
ng
co
an
th
o ng
du
u
cu

51
CuuDuongThanCong.com https://fb.com/tailieudientucntt

You might also like