71
Lecture4 Page 1 微微微微微微微微 Communications 微微微微微微微微微 微微 微微微 、( Serial/parallel 微微 微微微 、( Synchronous/asynchronous 微微微 微微微 、( Point-to-point/bus 微微微 微微微微 、( Half-duplex/full-duplex 微微微 微微微微 、( Master-slave/equal partners 微微微微 微微微微微 、( Single-ended/differential 微微微微微微

Communications

  • Upload
    badu

  • View
    29

  • Download
    1

Embed Size (px)

DESCRIPTION

通信基础知识. Communications. 通信方式的几个要素 串行、并行( Serial/parallel ) 同步、异步( Synchronous/asynchronous ) 点对点、总线( Point-to-point/bus ) 半双工、全双工( Half-duplex/full-duplex ) 主从式、对等式( Master-slave/equal partners ) 单端信号、差分信号( Single-ended/differential ). 通信基础知识 —— 串行、并行. Communications -characteristics. - PowerPoint PPT Presentation

Citation preview

Page 1: Communications

Lecture4 Page 1 微控制器开发技术

Communications

通信方式的几个要素

串行、并行( Serial/parallel )同步、异步( Synchronous/asynchronous )点对点、总线( Point-to-point/bus )半双工、全双工( Half-duplex/full-duplex )主从式、对等式( Master-slave/equal partners )单端信号、差分信号( Single-ended/differential )

通信基础知识

Page 2: Communications

Lecture4 Page 2 微控制器开发技术

Communications -characteristics

串行通信 数据按位传输 通信方只需要很少的连接 – resource/pin efficient 速度较慢,传输信息少 传输距离较远

并行通信 数据按字节 / 字传输 通信方需要多个连接信号 -- difficult for PCB routing 传输速度快 传输距离较短

你接触过哪些串行 / 并行通信?

通信基础知识 —— 串行、并行

Page 3: Communications

Lecture4 Page 3 微控制器开发技术

Communications -characteristics

同步通信接收方可以得到发送方的通信时钟信号传输速率高

异步通信发送方和接收方使用独立的时钟来处理通信信号通信双方约定通信速率通信中需要约定起始 / 停止位或定义特殊的帧结构传输速率较低

通信基础知识 —— 同步、异步

Page 4: Communications

Lecture4 Page 4 微控制器开发技术

Communications -characteristics

点对点通信线路上只有两个通信方 不需要寻址

总线通信线路上有多个通信方需要建立寻址机制

通信基础知识 —— 拓扑结构

Page 5: Communications

Lecture4 Page 5 微控制器开发技术

Communications -characteristics

半双工不能同时进行发送和接收一方发送时,其他通信方只能处于接收状态需要额外的逻辑来控制发送和接收状态的切换有发生冲突的危险

全双工通信端可以同时进行发送和接收需要两个独立的通信通道使用简单,数据传输率高

对讲机?电话机?

通信基础知识 —— 半双工、全双工

Page 6: Communications

Lecture4 Page 6 微控制器开发技术

Communications -characteristics

主从式只有主端可以发起数据传输从端等待主端的允许才能传输数据

对等式任何节点可以发起数据传输需要建立冲突检测和仲裁机制

通信基础知识 —— 主从式、对等式

Page 7: Communications

Lecture4 Page 7 微控制器开发技术

Communications -characteristics

单端信号信号幅度以地电平作为参考通信双方必须建立共同的参考地电平干扰造成的信号幅度变化可能导致错误解释

差分信号每个信号使用两根线传输,信号的值由两个线上的电

平的差值表示通信方不需要共同的参考地对干扰抑止能力强传输速率高

通信基础知识 —— 单端、差分信号

Page 8: Communications

Lecture4 Page 8 微控制器开发技术

微控制器常见的通信类型

总线类型 线数 通信类型 多主 波特率( bps) 器件数目 总线长度UART 2 异步 No 3K-1M 2 *

SPI 3 同步 Yes 1M-10M <10 板内

I2C 2 同步 Yes ~1M <10 板内

CAN 2 异步 Yes 20K-1M 128 百米~公里

LIN 1 异步 No <20K 16 几十米

Page 9: Communications

单片机原理及应用

Tsinghua Freescale Embedded System Center www.tsinghua-mot.net

by AlexGong [email protected]

10.2 同步串行外设接口 SPI

Page 10: Communications

Lecture4 Page 10 微控制器开发技术

本节提要

一、 SPI 概述二、 SPI 模块功能三、 SPI 模块引脚四、 SPI 模块寄存器五、 SPI 模块编程范例

Page 11: Communications

Lecture4 Page 11 微控制器开发技术

本节提要

一、 SPI 概述二、 SPI 模块功能三、 SPI 模块引脚四、 SPI 模块寄存器五、 SPI 模块编程范例

Page 12: Communications

Lecture4 Page 12 微控制器开发技术

SPI 特性

可设置主机模式 / 从机模式 可选择 8 位或 16 位数据宽度 可选择全双工模式 ( 一个 ) 或单线双向模式 可灵活设置的时钟极性和相位 可选择从最高位或最低位开始传送 可编程的传送速率 可实现双缓存操作 与 LCD 驱动器 , D/A 转换器 , RTC,EEPROM 等

通信接口简单;

Page 13: Communications

Lecture4 Page 13 微控制器开发技术

SPI 的数据流动

Page 14: Communications

Lecture4 Page 14 微控制器开发技术

7 0 7 0MOSI

MISO

CPU 端IO 端

CPU 端IO 端

7 07 MOSI

MISO0

重复 8 次

SPI 工作原理

Page 15: Communications

Lecture4 Page 15 微控制器开发技术

本节提要

一、 SPI 概述二、 SPI 模块功能三、 SPI 模块引脚四、 SPI 模块寄存器五、 SPI 模块编程范例

Page 16: Communications

Lecture4 Page 16 微控制器开发技术

SPI 方框图SPI 时钟发生

器移位寄存器

控制寄存器

Page 17: Communications

Lecture4 Page 17 微控制器开发技术

主机模式

控制整个数据传送通过定义 SS 选择通信从机确定 SCK 波特率,相位和极性切换 SCK 脉冲 驱动 MOSI 信号采样 MISO 信号

通过写入 SPIDR 触发传输过程

Page 18: Communications

Lecture4 Page 18 微控制器开发技术

从机模式

响应传输仅当定义了相应的 SS 才被激活利用事先定义的相位和极性检测 SCK 跳变沿 驱动 MISO 信号采样 MOSI 信号

Page 19: Communications

Lecture4 Page 19 微控制器开发技术

本节提要

一、 SPI 概述二、 SPI 模块功能三、 SPI 模块引脚四、 SPI 模块寄存器五、 SPI 模块编程范例

Page 20: Communications

Lecture4 Page 20 微控制器开发技术

.

SPI 引脚 1 —— 串行数据引脚

MOSI — Master Out/Slave In Pin

MISO — Master In/Slave Out Pin

SPI 置为主机模式: MISO – 数据输入线 MOSI – 数据输出线

SPI 置为从机模式: MISO – 数据输出线 MOSI – 数据输入线

作用:发送和接收串行数据

Page 21: Communications

Lecture4 Page 21 微控制器开发技术

.

SPI 引脚 2 —— 串行时钟

SCK — Serial Clock Pin

主机模式下, SCK 信号来源于内部 MCU 总线时钟

同步信号,对 MOSI 和 MISO 引脚传输的串行输入、输出数据进行同步

主机和从机在 SCK 信号的跳变沿进行移位和采样数据

SCK 的时钟频率由 SPI0BR 选择 主机和从机遵守相同时序模式

Page 22: Communications

Lecture4 Page 22 微控制器开发技术

SPI 信号 – SCK 极性 & 相位

SCK 奇数跳变沿驱动,偶数跳变沿采样

Page 23: Communications

Lecture4 Page 23 微控制器开发技术

SPI 信号 – SCK 极性 & 相位 ( 续 )

SCK 偶数跳变沿驱动,奇数跳变沿采样

Page 24: Communications

Lecture4 Page 24 微控制器开发技术

.

SPI 引脚 3 —— 外设片选

SS — Slave Select Pin

主机模式: SS 信号上拉到高电平

主机和从机对 SS 引脚使用不同

从机模式: SS 信号为低电平

Page 25: Communications

Lecture4 Page 25 微控制器开发技术

本节提要

一、 SPI 概述二、 SPI 模块功能三、 SPI 模块引脚四、 SPI 模块寄存器五、 SPI 模块编程范例

Page 26: Communications

Lecture4 Page 26 微控制器开发技术

SPI 寄存器 1/4

SPICR1 - SPI 控制寄存器 1

1 = SPI Interrupt Enabled0 = SPI Interrupt Disabled

1 = SPI SYSTEM Enabled0 = SPI SYSTEM Disabled

1 = SPI IS MASTER0 = SPI IS SLAVE

DDRS7 SSOE MASTER MODE SLAVE MODE

0 0 SS INPUT SS INPUT (MODF Enabled) 0 1 GP INPUT SS INPUT 1 0 GP OUTPUT SS INPUT 1 1 SS OUTPUT SS INPUT

1 = SPI LSB FIRST 0 = SPI MSB FIRST

1 = SPI Transmit Interrupt Enable0 = SPI Transmit Interrupt Disabled

SPICR2 - SPI 控制寄存器 2

SPISWAI = SPI Stop in Wait Mode 1 = HALT SSI CLOCK WHEN CPU IN WAIT MODE

MODFEN - Mode Fault Enable 0 = Mode Fault Disabled 1 = Mode Fault Enabled

BIDIROE - Output Enable in Bidirectional mode 0 = Output Buffer Disabled 1 = Output Buffer Enabled

Page 27: Communications

Lecture4 Page 27 微控制器开发技术

练习: 20M 总线时钟, 最大 SPI 波特率 = ? 最小 SPI 波特率 = ?

SPI 寄存器 2/4

Page 28: Communications

Lecture4 Page 28 微控制器开发技术

SPI 寄存器 3/4

SPISR - SPI 状态寄存器

SPRF - SPI 接收中断标志 1 = New is received into SPIDR

SPTEF - SPI 发送缓存空中断标志 MODF – 主机模式错误标志 1 = SPI Data Register is Empty 1 = SPI Master was selected as Slave

Page 29: Communications

Lecture4 Page 29 微控制器开发技术

SPI 寄存器 4/4

SPI Data Register(SPIDR = SPIDRH:SPIDRL) - SPI 数据寄存器

注 : 用户仅当 SPI 发送缓冲空标志置位时才能写入该寄存器 .

注 : 发送数据与接收数据使用相同寄存器

Page 30: Communications

Lecture4 Page 30 微控制器开发技术

本节提要

一、 SPI 概述二、 SPI 模块功能三、 SPI 模块引脚四、 SPI 模块寄存器五、 SPI 模块编程范例

Page 31: Communications

Lecture4 Page 31 微控制器开发技术

SPI 模块编程范例

SPI 模块编程方法① SPI初始化:先写 SPI0BR ,设定 SPI 波特率;再写

控制寄存器 SPI0CR1 和 SPI0CR2 ,设定时钟的极性、相位、 SPI 数据传输宽度、 SPI 工作模式、中断允许位,并使能 SPI 模块

② SPI 收发数据:通过查询 SPI0SR 中的状态位或采用中断方式,读写数据寄存器,完成数据收发。

Page 32: Communications

Lecture4 Page 32 微控制器开发技术

SPI 模块编程范例 ( 续 1)

SPI 模块编程范例

1. SPI初始化//SPI初始化函数void SPI_Init()

{

SPI0BR=0x44; // 设定波特率 SPI0CR1=0x5C; //关闭 SPI 中断,置 SPI 主机模式,使能 SPI 模块 SPI0CR2=0x00; // 数据传输宽度为 8 位,即一个字节 }

Page 33: Communications

Lecture4 Page 33 微控制器开发技术

SPI 模块编程范例 ( 续 2)

//SPI 发送一个字节的函数// 参数: send_data 为待发送的 1字节数据void SPI_send_char(unsigned char send_data)

{

while((SPI0SR&0x20)==0);

SPI0DRL=send_data;

}

//SPI 接收一个字节的函数//返回:接收的 1字节数据unsigned char SPI_rev_char(void)

{

while((SPI0SR&0x80)==0);

return SPI0DRL;

}

2. SPI 收发数据

Page 34: Communications

Lecture4 Page 34 微控制器开发技术

SPI 驱动数码管显示的典型应用

硬件连接:教材 P80 : 1.MCU 的 MOSI( 17)接 164 的 A 脚( 1) 2.MCU 的 SCK( 18)接 164 的 CLK 脚( 8) 3.164 的 B( 2)和 /MR( 9)接 +5V 4.164 的 Q0~Q7 ,接数码管的 a~DP板上 12、 11、 10、 9、 8、 7、 6、 5、 4、 3、 2、

1 1 位、 a 、 f 、 2、 3、 b、 4、 g、 c、 DP、 d、

e

Page 35: Communications

Lecture4 Page 35 微控制器开发技术

74LS164 原理

符号 引脚 说明

DSA 1 数据输入

DSB 2 数据输入

Q0~Q3

3~6 输出

GND 7 地 (0 V)

CP 8时钟输入(低电平到高电平边沿触

发)

/M/R 9 中央复位输入(低电平有效)

Q4~Q7

10~13

输出

VCC 14 正电源

Page 36: Communications

Lecture4 Page 36 微控制器开发技术

SPI 驱动数码管显示的典型应用

软件: 见 3-SPI练习程序

Page 37: Communications

异步串行通信( SCI )通信基础知识异步串行通信协议 RS-232 标准SCI 功能简介SCI 寄存器SCI 软件开发方法SCI 应用程序示例

Page 38: Communications

SCI

Page 39: Communications

EIA RS-232-C美国电子工业协会正式公布的异步串行通信标准,也是目前最常用的异步串行通信标准,用来实现计算机与计算机之间、计算机与外设之间的数据通信

RS-232-C 电平采用负逻辑,即,逻辑 1 : -3~ -25V, 逻辑 0 : +3~ +25V (需要电平转换)

RS-232-C 接口标准有 22 根线,采用标准 25芯 D 型插头座; PC 上使用简化的 9芯 D 型插座

多种通信应答(握手)方式: 硬件握手,自应答, XON/XOFF 模式

波特率: 300bps ~ 4Mbps , 物理层有多种实现方式(铜缆,光缆,红外,无线,微波)

起始位 / 停止位 / 数据位 /奇偶校验 / 全双工半双工模式

广泛应用: Modem , 20mA 电流环, RS485 个人计算机都有标准的 RS232 接口,驱动程序,超级终端

RS232 standard

Page 40: Communications

RS232 standard

Page 41: Communications

简化的 9芯 D 型接头信号定义(硬件流量控制) DTE DCE

3 TxD out TxD Transmit Data2 RxD in RxD Receive Data5 GND GND

7 RTS out RTS Require To Send8 CTS in CTS Clear To Send6 DSR in DSR Data Set Ready9 DCD in DCD Data Care Detected4 DTR out DTR Data Terminal Ready

RS232 standard

Page 42: Communications

简化的 9芯 D 型接头信号定义(自应答方式)

DTE / DCE

2 TxD out TxD (Transmit Data )

3 RxD in RxD (Receive Data)

7 GND GND (Ground)

4 RTS out RTS (Require to Send)

5 CTS in CTS (Clear to Send)

6 DSR in DSR (Data Terminal Ready)

8 DCD in DCD (Data Carry Detected)

20 DTR out DTR (Data Set Ready)

Ready always, or XON/XOFF protocol, ASCII only!

RS232 standard

Page 43: Communications

简化的 9芯 D 型接头信号定义(无应答方式)

1 protection ground

2 RxD in RxD (Receive Data)

3 TxD out TxD (Transmit Data )

5 GND GND (Ground)

6 DSR in DSR (Data Terminal Ready)

7 RTS out RTS (Require to Send)

8 CTS in CTS (Clear to Send)

9 DCD in DCD (Data Carrier Detected)

4 DTR out DTR (Data Set Ready)

No Handshake, should use XON/XOFF protocol,ASCII only

RS232 standard

Page 44: Communications

RS232 standard

TTL level vs. RS232 level

Page 45: Communications

RS232 standard

TTL level vs. RS232 level

Page 46: Communications

RS232 standard

RS232标准( 1962)出现的比 TTL( 1964)还早

需要接口转换芯片

MAX232 MicrocontrollerDB

9

TTL/CMOS

RS232

Page 47: Communications

Frame format

0 1 2 3 4 5 6 7 8

LSB MSB stop idleidle start

UART

NRZ (not return zero) encoding Several configurable parameters

Baud rate, #data bits, parity, #stop bits Nomenclature: 9600,N81 LSB- Least significant bit MSB- Most significant bit

Parity (stop)

Page 48: Communications

Start/stop 位

接受端需要明确数据帧的边界,即该帧从什么时候开始,什么时候结束。

数据位传送之前,都要发送 START 位 (always ‘0’) ( 从空闲状态开始的下降沿,是 START 位的开始)

在数据位传送结束后,还要传送 STOP 位 (always ‘1’).

Page 49: Communications

数据位识别

接受端在其时钟上升沿采样数据线电平,以识别数据位的高低状态。

What is the data transmitted above?

Page 50: Communications

数据位识别

Tx_clk

Rx_clk

TxD

RxD

传输过程会引起信号的延迟,会引入外部干扰导致的噪声及毛刺

Page 51: Communications

SCI(UART)-oversampling

- 通信双方有约定的通信速率(波特率)- 接受端按照 16 倍波特率的频率对信号进行 "over samples"

- 使用其中 3 个采样结果,通过 3 取 2 的机制确定实际的信号电平状态- 如果 3 个采样结果不一致, noise 标志会置起- 接受端在总线进入空闲状态后,通过监测下降沿,启动对起始位的判断,从而开始整个数据帧的接受。

Page 52: Communications

SCI(UART)-oversampling

Page 53: Communications

SCI- 波特率

波特率: bits per second

Question1: 按照 9600,N81 的通信参数,每秒可以传送多少个字节?Hints: don’t forget the start/stop bits.

Question2: 对于一个总线频率为 16MHz的微控制器,其 SCI 最高支持的波特率是多少?

Page 54: Communications

波特率误差容限

接受端必须能正确的识别整个数据帧,从起始位到停止位

0/1 0/1 0/1 0/1 0/1 0/1 0/1 10/101 1

16*(1+8) +9

9Ttx < (153/16)Trx <10Ttx

clock difference tolerance~5%

10

9 Transmitter

Receiver

Page 55: Communications

校验位

校验位附加在数据帧末尾,用于检查传输过程中是否发生了数据位错误。

LSB MSB stopstart Parity

Page 56: Communications

Parity bit

偶校验 -- 所有数据位和校验位中, 1 的数目是偶数

奇校验 --所有数据位和校验位中, 1 的数目是奇数

Example:

what is the even parity for “10011100”

what is the odd parity for “01010110”

Page 57: Communications

SCI 可能产生的事件

- Tx Empty (IRQ)

- Tx Complete (IRQ)

- Rx Full (IRQ)

- Idle (IRQ)

- Rx Overrun (IRQ)

- Noise Error

- Framing Error

- Parity Error

Page 58: Communications

SCI 故障

Framing Error: 在数据帧结尾没有收到正确的停止位

Receiver overrun: 下一个数据到达时,上一个数据还没有被处理

Parity Error: 接受的校验和与协议规定的不匹配 .

Page 59: Communications

So, are we clear about UART?

Let’s play a game!I need one volunteer

This volunteer will be a UART sender, all your students will be UART receivers.

LSB firstProtocol is 1,N81 (1bit per second)Hand-raise is ‘1’, hand-down is ‘0’

Page 60: Communications

Transmitter: Receiver:

.

TDR BUFFER

SHIFT REGISTER

DATA IN

DATAOUT

T8PARITY

GEN.

DATA OUT

RDR BUFFER

SHIFT REGISTERDATAIN

R8

PARITYCHECK

SCI – 双缓冲寄存结构

发送和接受都具有双缓冲结构 CPU 不能读写移位缓冲寄存器CPU 仅能访问数据寄存器

Page 61: Communications

.

SCI – 缓冲结构

发送时,数据自动从数据寄存器转移到移位寄存器 接受时,数据自动从移位寄存器转移到数据寄存器 发送和接受的数据寄存器使用相同的寄存器地址 读写该寄存器地址具有不同的含义 !

Question: what’s the function of the instructions below while(1){ asm LDAA SCI0DR asm STAA SCI0DR }

Page 62: Communications

Transmitter block diagramTx data regist

er

Tx clock genNote the ÷16

Shift register

Parity gen

Control logic

Interrupt request gen

Page 63: Communications

Receiver block diagramRx data register

shift register

Rx clock gen

Control logic

Page 64: Communications

SCI(UART)

Page 65: Communications

SCIBH/L - SCI Baud Rate Control Register

必须连续写入 BDH 和 BDL 寄存器,仅仅写入 BDH没有作用。

Bus clock 1 ~ 8191

24M bus clockExercise1: 9600bps wanted , BR = ? BDH = ? BDL = ?Exercise2 : the maximum baud rate = ?

SCI Registers 1/5

Page 66: Communications

SCCR1 - SCI Control Register 1Address Offset $0002

1 = 9-BIT DATA 0 = 8-BIT DATA

1 = IDLE LINE WAKE-UP 0 = ADDRESS MARK WAKE-UP

1 = NORMAL IDLE TIME 0 = SHORT IDLE TIME

1 = EVEN PARITY0 = ODD PARITY

1 = PARITY Enabled0 = PARITY Disabled

SCISWAI - SCI Stop in Wait mode 0 = Normal operation 1 = SCI Disabled in Wait mode

SCI Registers 2/5

Page 67: Communications

SCICR 2 - SCI Control Register 2

ILIE - Idle Line Interrupt Enable1 = IDLE IRQ Enabled0 = IDLE IRQ Disabled

TE _ Transmitter Enable 1 = Transmitter Enabled0 = Transmitter Disabled

RE Receiver Enable1 = Receiver Enabled0 = Receiver Disabled

SBK - Send Break1 = Send Break0 = Terminate Break

RWU - Receiver Wake-up 1 = Enter Receiver Wake-UP 0 = Exit Receiver Wake-UP

TCIE - Transmit Complete Interrupt Enable1 = Transmitter Complete IRQ Enabled0 = Transmitter Complete IRQ Disabled

RIE - Receiver Interrupt Enable1 = Receiver IRQ Enabled0 = Receiver IRQ Disabled

TIE - Transmitter Interrupt Enable1 = Transmitter IRQ Enabled0 = Transmitter IRQ Disabled

SCI Registers 3/5

Page 68: Communications

SCISR 1 - SCI Status Register 1

SCI Registers 4/5

SCISR 2 - SCI Status Register 2

RAF - Receiver Active Status 1 = Receiver Active 0 = Receiver not Active

BRK13 - Break Transmit Character Length 0 = Break Char is 10 or 11 bits long 1 = Break Char is 13 or 14 bit long

TXDIR - Transmitter pin direction 0 = TxD pin is input in Single-Wire mode 1 = TxD pin is output in Single-Wire mode

RDRF: Receive data Register FullTC : Transmit CompleteTDRE: Transmit data register empty

Page 69: Communications

SCIDRH/L - SCI Data Register High/Low

Note: R8 and T8 are used when 9-bit char is required

SCI Registers 5/5

Notice: same register for both transmit data and receive data

Page 70: Communications

SCI programming

Put_Char BRCLR SCI0SR1,#$80,* STAB SCI0DRLRTS

void Put_Char(char c){ while ( (SCI0SR1 & 0x80) == 0);

SCI0DRL = c; }

Page 71: Communications

Get_Char BRCLR SCI0SR1,$20,*LDAB SCI0DRL

RTS

char Get_Char(void);{ while ((SCI0SR1 & 0x20) == 0);

return(SCI0DRL); }

SCI programming