65
1 单单单单单单单单 Microcontroller Princ iples and Application s 单单单单单 单单 单单 单单单单 (、、)

单片机原理及应用 Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

  • Upload
    lacey

  • View
    243

  • Download
    0

Embed Size (px)

DESCRIPTION

单片机原理及应用 Microcontroller Principles and Applications 非电量接口 (声、光、机械等). introduction. MCU is not capable to do everything, special components and circuits are always needed Output: LED, 7Seg-LED, relays, LCD, buzzer Speaker, motor, IrDA, For input: Switch, button, keypad Sensor,. - PowerPoint PPT Presentation

Citation preview

Page 1: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

1

单片机原理及应用 Microcontroller Principles and

Applications

非电量接口(声、光、机械等)

Page 2: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

2

introduction

• MCU is not capable to do everything, special components and circuits are always needed

• Output:– LED, 7Seg-LED, relays, LCD, buzzer– Speaker, motor, IrDA,

• For input:– Switch, button, keypad– Sensor,

Page 4: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

4

Control LED试验板的 LED 电路

Page 5: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

5

实验板的核心板上有 8 个 LED 指示灯,经 245 驱动,连接到 PORTB 上,低电平点亮。控制其闪动的 C 程序(test1.c) 如下:

void deley(void)

{unsigned int i,j;for(i=0;i<1000;i++) for(j=0;j<500;j++); }

void main(void) { /* put your own code here */ PORTB=0X55; DDRB|=0XFF; for(;;) { deley(); PORTB_PB0=~PORTB_PB0; PORTB_PB1=~PORTB_PB1; }

此处可解释并演示程序

Page 6: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

6

7-segment LED 7 段数码管

• 把 7 个细长的 LED 排成‘日’字型• 单独控制每个段的亮灭,可以表示

出数字 0 ~ 9• 使用多个数码管表示数字• 应用非常广泛• 多种颜色,多种尺寸

Page 7: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

7

7 段数码管原理与结构• 由 7/8 个 LED 共同组成,使用一个公用端,• 共阳型

– 所有 LED 的阳极连在一起,成为公用端

• 共阴型– 所有 LED 的阴极连在一起,作为公用端

Page 8: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

8

7 段数码管编码a

b

c

d

e

f

g

如何表示数字 2 ?abcdefg7 个段

哪些点亮,哪些熄灭

Page 9: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

9

7 段数码管编码a

b

c

d

e

f

g

如何表示数字 2 ?abcdefg7 个段

哪些点亮,哪些熄灭

待表示的数字和 abcdefg 的状态构成了 7 段数码管的编码

Page 10: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

10

7 段数码管编码a

b

c

d

e

f

g

N a b c d e f g

0 1 1 1 1 1 1

1 1 1

2 1 1 1 1 1

3 1 1 1 1 1

4 1 1 1 1

5 1 1 1 1 1

6 1 1 1 1 1 1

7 1 1 1

8 1 1 1 1 1 1 1

9 1 1 1 1 1 1

1 – 表示点亮0 – 表示熄灭

Page 11: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

11

7 段数码管编码a

b

c

d

e

f

g

能否表示 ABCDEF ?

Page 12: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

12

7 段数码管使用• 使用单个数码管时公用端接入电源或地,用

微控制器的 IO 控制每一个段 LED 的亮灭。• IO 端口的值就是数码管编码,由待显示的数

据决定• 多位数码管表示一个多位数时,每个数码管

仅能表示一位,对每位都要进行编码转换• 多个数码管可以使用动态扫描的方法节约 IO

端口。

Page 13: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

13

数码管动态扫描

段控制

位控制

通过位控制信号使得任意时刻只有一位数码管处于显示状态

通过段控制信息确定该位数码管显示的数字

定时更改位控制信息和段控制信息

利用视觉暂留现象得到稳定的数字显示

多个 LED 同时点亮的电流都经过公共端, MCU 的 IO 无法承受,必须使用三极管

此处可在黑板画时序图解释动态显示过程

Page 14: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

14

数码显示实验• 实验板上有 4 位数码管,共阳极结构,与前图

一样,不过段选和片选控制都经过 7407 驱动,引出 12 根连线,对应关系如下

12 11 10 9 8 7 6 5 4 3 2 1

C4 a f C3 C2 b C1 g c dp d e

实验中可将段选连接到 PORTP ,将片选连接到 PORTT ,连线关系如下:

Page 15: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

15

12 11 10 9 8 7 6 5 4 3 2 1

T3 P0 P5 T2 T1 P1 T0 P6 P2 P7 P3 P4

连好之后,就可以先将一个数码管点亮, C 程序 (test2.c) 如下:

此处可解释点亮原理并解释演示程序

DDRP|=0XFF; DDRT|=0XFF;// 点亮一个数码管,显示 2

PTP=~0x5b; PTT=0xfe;

Page 16: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

16

对与数码管控制,直接控制段码并不方便,正如在数字电路中有 BCD-7 段码译码器,在程序中可以使用软件译码的方式,例程 (test2.c) 如下:

// 软件 BCD-7 段译码const unsigned char DisplayDecode[]={~0x3f,~0x06,~0x5b,~0x4f,~0x66,~0x6d,~0x7d,~0x07,~0x7f,~0x6f,~0x77,~0x7c,~0x39,~0x5e,~0x79,~0x71};//0-9 ABCDEF PTP=DisplayDecode[3];

Why use const ?好处?

此处可解释软件译码原理和好处并演示程序

Page 17: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

17

目前,我们已点亮一个数码管,但实际我们需要点亮全部数码管并显示不同内容,按前面的分析,必须采用动态显示的方式,按人眼的反应,刷新频率得大于 80Hz ,即每段显示 3ms 。这个 3ms 可以用软件延迟实现,但为提高 CPU效率,一般使用定时器。 C 程序 (test3.C 或 t

est4.c) 如下:// 动态显示, 4 个数码管显示不同内容

if(DisplayPauseTimer==gPIT_counter){

PTT|=0xf;DisplayPauseTimer+=1;//10

0DisplayNumber++;if(DisplayNumber>=4)

DisplayNumber=0;

Page 18: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

18

PTP=DisplayDecode[DisplaySuma[DisplayNumber]];

switch(DisplayNumber){case 0:

PTT&=0xfe;break;

case 1:PTT&=0xfd;break;

case 2:PTT&=0xfb;break;

default:PTT&=0xf7;break;

}}

此处需回顾 PIT 的使用,解释多任务调度、好处 并演示程序

Page 19: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

19

Questions?

Page 20: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

20

Switch - 开关• 开关有两种状态-闭合

和断开• 船形开关,拨位开关• 同一个开关可以提供多

组触点

Page 21: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

21

开关状态的读取• 利用外部电路使得开关的闭合和断开状态

能得到两种不同的电平值• 开关断开时要有默认电平值(上拉 / 下拉)

MCU 的 IO 口

5V

Page 22: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

22

按键• 带有自恢复装置的开

关,常开常闭型• 各种形状,各种尺寸• 耐高压,轻触式,带

指示• 一个按键也可以提供

多组触点

Page 23: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

23

按键• 用于产生一个脉冲• 按下和释放的时刻会有抖动• 需要设计去抖动电路或软件去抖

Page 24: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

24

键盘• 不同于开关,按键可以自行释放• 如果多个按键不会同时按下,就可以做成键盘,利用扫描方式获取按键信息,减少占用的 IO 数目。

Page 25: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

25

键盘• 键盘由多个按键组合而成

Page 26: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

26

键盘

包含 16 个按键的键盘需要多少个 IO来连接?

5V

1 个 MCU 的 IO 口,可以检查一个按键的状态

Page 27: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

27

键盘5V

行端口

列端口

可以利用扫描方式获取按键信息,减少占用的 IO 数目

Page 28: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

28

逐行扫描5V

行端口

列端口

0

1

1

1

1 1 1 1

对第一行进行扫描: 行输出端口输出 0111 检查列回读端口的值

Page 29: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

29

逐行扫描5V

行端口

列端口

1

0

1

1

1 0 1 1

对第二行进行扫描: 行输出端口输出 1011 检查列回读端口的值

最坏的情况下需要进行 4次扫描过程, faster?

对第一行进行扫描: 行输出端口输出 0111 检查列回读端口的值

1 1 1 1

Page 30: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

30

反转扫描5V

行端口

列端口

1 1 0 1

0

0

0

0

行端口输出 0000检查列端口的值

根据列端口的值,已经可以确定在哪一列有键按下

1 1 1 1

Page 31: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

31

反转扫描5V

行端口

列端口

1

1

1

1

行端口输出 0000检查列端口的值

根据列端口的值,确定哪一列有键按下

将行端口和列端口输入输出方向反转

列端口输出 0000检查行端口的值

根据行端口的值,确定哪一行有键按下

0 0 0 0

1

0

1

1

Page 32: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

32

反转扫描• Hey , Wait 。。。。。。

– 行列反转时上拉电阻怎么办?• OK 。。。。

– 给行列都加上上拉电阻• 缺点

– 作为输出时,这个上拉电阻其实没有什么作用,反而白白消耗了电流

• 改进– 用 MCU 端口的内部上拉电阻,根据输入输出状态进行动态配置

• 限制– 有些 IO 端口只支持成组打开 / 关闭上拉电阻。那么需要两个端口

的 IO配合使用。– 有些 IO 端口的每一个引脚都有独立的上拉电阻控制位。 Perfect!

Page 33: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

33

5V

行端口

列端口

0

1

1

1

1 1 1 1

对第一行进行扫描: 仅第一行输出, 其余行端口为输入 检查所有输入端口

5V

多键按下的扫描识别

Page 34: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

34

逐行扫描5V

行端口

列端口

1

0

1

1

1 1 1 1

对第一行进行扫描:

5V

对第二行进行扫描: 仅第二行输出, 其余行端口为输入 检查所有输入端口

1 0 1 1

Page 35: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

35

逐行扫描5V

行端口

列端口

1

1

0

1

1 1 1 1

对第一行进行扫描:

5V

对第二行进行扫描:

1 1 1 0

对第三行进行扫描: 仅第三行输出, 其余行端口为输入 检查所有输入端口

Page 36: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

36

逐行扫描5V

行端口

列端口

1

1

1

0

1 1 1 1

对第一行进行扫描:

5V

对第二行进行扫描: 对第三行进行扫描: 对第四行进行扫描: 仅第四行输出, 其余行端口为输入 检查所有输入端口

Page 37: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

37

扫描策略• 循环扫描?

– 可行,可靠–占用 CPU 时间

• 定时扫描– 可能丢失按键动作

• 键盘中断!– 发现有任何按键按下时启动扫描

Page 38: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

38

键盘中断-无键按下5V

行端口输出

列端口等待键盘中断

0

0

0

0

1 1 1 1

Page 39: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

39

键盘中断-有键按下5V

行端口输出

列端口产生键盘中断

0

0

0

0

1 1 0 11 1 1 1

Page 40: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

40

本实验板上有一个 4*4 的薄膜键盘,板上无上拉电阻,接口是 X1-4 、 Y1-4 ,实验中可将其连接到 PORTA 口上,连线关系如下:

X4 X3 X2 X1 Y4 Y3 Y2 Y1

PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0

采用逐行扫描的方式, Y1-4 ( PA0-3 )为输出, X1-4( PA4-7 )做为输入,使能 PORTA 的上拉电阻,周期循环扫描,间隔 25ms ,一次一行, 100ms完成一次扫描。在这样取样间隔下,已滤除按键抖动。 C 程序 (test4.c) 如下:

Page 41: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

41

DDRA=15;//输出 /*Ports ABEK, BKGD pin Pull-up Control Register (PUCR) 7:PUPKE 6:BKPUE 5:0 4:PUPEE 3:0 2:0 1:PUPBE 0:PUPAE */ PUCR|=(1<<0);

// 行列键盘检测if(KeyDetectTimer==gPIT_counter){

KeyDetectTimer+=10;KeyCurentInput=PORTA;KeyCurentInput>>=4;KeyInputStat=((KeyLastInput[KeyLine]^Key

CurentInput)&KeyLastInput[KeyLine]);KeyLastInput[KeyLine]=KeyCurentInput;

此处需解释 并演示程序

Page 42: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

42

if(KeyInputStat&15){

for(i=0;i<4;i++){

if(KeyInputStat&1){

DisplaySuma[3]=DisplaySuma[2]; DisplaySuma[2]=DisplaySuma[1]; DisplaySuma[1]=DisplaySuma[0];

DisplaySuma[0]=KeyV[KeyLine][i];break;

}KeyInputStat>>=1;

}}KeyLine++;if(KeyLine>=4)

KeyLine=0;

Page 43: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

43

switch(KeyLine){case 0:

PORTA=~1;break;

case 1:PORTA=~2;break;

case 2:PORTA=~4;break;

default:PORTA=~8;break;

}}

unsigned char KeyCurentInput=0,KeyInputStat,KeyLastInput[4],i;const unsigned char KeyV[4][4]={1,2,3,4,5,6,7,8,9,0,10,11,12,13,1

4,15};

Page 44: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

44

Questions?

Page 46: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

46

蜂鸣器驱动电路• 蜂鸣器需要较大电流, MCU 的 IO 不能提供足够的电流

• 需要使用驱动电路

Page 48: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

48

Relay-继电器• 基于电磁作用,控制信号和被控制开关之间没有

电气联系,被控制端的电气干扰被有效的隔离。

5V

~220V

Page 50: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

50

Opto-coupler 光耦

24V

5V

Page 51: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

51

传感器• 传感器是将外界信息按一定规律转换成可

用信号的机械电子装置。–温度,湿度,磁场,压力,位移和转动,加速

度,流量,应变–热敏,光敏,气敏,力敏,磁敏,湿敏,声敏,放射性敏感,色敏,味敏

Page 52: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

52

温度传感器• 热敏电阻

– 电阻的阻值是温度的函数– 根据阻值通过查表或拟合的方式

反算温度• 智能温度传感器

–智能芯片,数字接口

Page 53: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

53

湿度传感器• 烧结型半导体陶瓷湿度敏感器件 –湿度影响半导体器件的电

阻率,改变其电阻值

Page 54: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

54

温湿一体智能传感器• 温度湿度测量• 自动校准• 数字接口

Page 55: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

55

振动 /加速度传感器• 测量其敏感轴方向的线性加速

度大小,• 主要技术:压阻、压电和电容• 压阻式在汽车工业广泛应用

– 安全气囊、防抱死、牵引控制。• 压电主要在工业上防止机器故障,– 可重复性、稳定性和自生性 . 。

• 电容式具有温度效应小 ,灵敏度高 ,加工工艺简单等优点

Page 56: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

56

海拔 / 高度 传感器• 绝对压力测量范围: 30KP

a--120KPa; 30KPa--1600KPa

• 单电源 +2.4V--3.3V供电 • 片内温度测量 • 标准数字输出: SPI 和 TWI

Page 57: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

57

酒精浓度传感器• 当半导体表面吸附

有还原性气体(如酒精等有机溶剂蒸汽)时,半导体微结晶粒子接触界面的导电电子比例就会发生变化,从而使电阻值随被测气体的浓度改变而变化。

其他各类气体传感器: 氢气,甲醛, CO , CO2 , SO2 O2 , H2S ,。。。

Page 58: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

58

转速 / 速度传感器• 光电编码器

Page 59: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

59

距离 / 接近传感器• 超声波

–超声波发射器发射超声波,超声波接收器接收反射波。–还可以用来测厚度,无损探测

Page 60: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

60

流量传感器

Page 61: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

61

光传感器• 光子能量激发束缚电子,电子空穴移动后形成电压。

Page 62: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

62

光电传感器• 用途广泛

–判断是否遮挡(流水线计数器–测量距离

Page 63: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

63

步进电机

Page 64: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

64

直流电机

Page 65: 单片机原理及应用   Microcontroller Principles and Applications 非电量接口 (声、光、机械等)

65

Questions?