《单片机ADC模块课件.ppt》由会员分享,可在线阅读,更多相关《单片机ADC模块课件.ppt(38页珍藏版)》请在三一办公上搜索。
1、单片机原理与应用,10.3 A/D转换模块,2,单片机原理与应用,本节提要,一、A/D转换基础知识二、ATD模块简介三、ATD模块寄存器四、ATD模块编程范例,3,单片机原理与应用,本节提要,一、A/D转换基础知识二、ATD模块简介三、ATD模块寄存器四、ATD模块编程范例,4,单片机原理与应用,ADC概述,微控制器处理的是离散量 (数字量) ,而现实中往往是连续量 (模拟量)物理量如:温度, 压力,湿度以及速度等等传感器 将物理量转换为电信号 (电压, 电流)需要模拟-数字转换器将电信号转换为数字量以供微控制器处理,5,单片机原理与应用,基本概念,传感器,控制器,执行器,6,单片机原理与应用
2、,ADC 工作原理,将输入电压与若干等间距的参考电压进行比较因此需要比较器Uin Uref Uout=V+ 1Uin Uref Uout=0V 0,Uin,Uref,Uout,1位ADC,7,单片机原理与应用,ADC 工作原理,3 个比较器可将电压分成4 级 (以 2位来表示),Uref1,Uref2,Uref3,Rank0,Rank1,Rank2,Rank3,Uref1,Uref2,Uref3,Uout1,Uout2,Uout3,Quiz1: For voltage in rank2, Uout1 = ? Uout2 = ? Uout3 = ?,Umax,Umin,Uin,8,单片机原理与应
3、用,ADC工作原理,2位ADC,Uin,Uref,R,R,R,R,D0,D1,编码器,8位 ADC, 需要多少个比较器?,9,单片机原理与应用,ADC工作原理,Flash Converter ADC优点: 速度极快缺点: 非常复杂逐次逼近式A/D转换器,10,单片机原理与应用,ADC工作原理,逐次逼近式转换器转换过程 ( 4位 ),SA-ADC 完成一次转换至少需要 n 个时钟周期,11,单片机原理与应用,ADC工作原理,必须进行采样保持以避免输入电压在转换期间发生变化,12,单片机原理与应用,ADC工作原理,逐次逼近式转换器( SA-ADC )非常快比较复杂SA-ADC在单片机中较为常见单片
4、机中常见典型为8-12位具有多个多路复用的模拟通道,13,单片机原理与应用,ADC 参数,转换位数(Bits)分辨率 = 2-n (8bits = 0.39%)转换速度(Speed)由工作时钟频率和转换位数确定转换范围(Range)05V, 010V, -5V+5V. Etc.转换通道数(Channels)Distinguish the channel and converter,14,单片机原理与应用,本节提要,一、A/D转换基础知识二、ATD模块简介三、ATD模块寄存器四、ATD模块编程范例,15,单片机原理与应用,8/10/12位可选 转换速度较高,10位精度,单次转换时间为7usec
5、采样时间可编程设置 多种转换模式:数据对齐方式、单次/连续转换、 转换结果比较 外触发控制 转换结束可产生中断 可用于8个模拟输入通道的模拟输入多路转换器. 模拟/数字输入引脚复用 18 转换序列长度. 多通道扫描,S12单片机ADC特性,16,单片机原理与应用,转换时间计算举例: (假设A/D 时钟为2MHz,10位精度 )转换时间 = Initial Sample Time + Programmed Sample Time + Resolution Period = 2 + 2 + 10 = 14 A/D Clocks = 7uSec,S12单片机ADC转换时间,17,单片机原理与应用,S
6、12单片机ADC模块框图,ADC 引脚可用于数字输入,ADC 结果寄存器,模拟输入通道,参考电压,18,单片机原理与应用,本节提要,一、A/D转换基础知识二、ATD模块简介三、ATD模块寄存器四、ATD模块编程范例,19,单片机原理与应用,ATD 模块寄存器,共27个寄存器控制寄存器(ATDCTL0ATDCTL5,6个)状态寄存器(ATDSTAT0ATDSTAT1,2个 )比较使能寄存器(ATDCMPE, 1个)比较方式寄存器(ATDCMPHT , 1个,)转换输入使能寄存器(ATDDIEN , 1个)转换结果寄存器(ATDDR0ATDDR15,16个)其中: 8位寄存器: ATDCTL0AT
7、DCTL5, ATDSTAT0 16位寄存器:其余,20,单片机原理与应用,ATD Registers 1/12,ATD Control Register 0 (ATDCTL0),WRAP3-0 - Wrap Around Channel Select 0 = ReservedX = 多通道转换模式 从ANx回绕到AN0 1x 15 仅在ATDCTL5的MULT=1时有效,21,单片机原理与应用,ATD Registers 2/12,SRES1:0 - A/D Resolution Select,ATD Control Register 1 (ATDCTL1),ETRIGSEL - Exter
8、nal Trigger Source Select S12XS此位无效,ETRIGCH3:0 - External Trigger Channel Select,SMP_DIS - Discharge Before Sampling Bit 0=No discharge 1 = discharge,注:写ATDCTL1会终止当前转换序列,22,单片机原理与应用,ATD Registers 3/12,ATD Control Register 2 (ATDCTL2),AFFC - ATD Fast Flag Clear All0 = 向CCFn 标志位写1则清零1 =读ATD转换结果寄存器则CCF
9、n 标志位清零(当比较功能被禁止) 写ATD转换结果寄存器则CCFn 标志位清零(当比较功能被使能),ICLKSTP - Internal Clock in Stop Mode Bit0 = 停止模式下,停止当前转换,退出停止模式后ATD转换自动重新开始1 = 停止模式下,可继续使用内部时钟进行ATD转换,ETRIGLE - External Trigger Level/Edge ControlETRIGP - External Trigger Polarity,ETRIGE - External Trigger Mode Enable0 = 禁止外部触发信号1 = 使能外部触发信号,ASCI
10、E- ATD Sequence Complete Interrupt Enable0 = 禁止ATD转换序列结束中断1 = 使能ATD转换序列结束中断,ACMPIE - Internal Clock in Stop Mode Bit0 = 禁止比较中断1 = 使能比较中断,注:写ATDCTL2会终止当前转换序列,23,单片机原理与应用,ATD Registers 4/12,ATD Control Register 3 (ATDCTL3),DJM - Result Register Data Justification0=Left justified data in the result reg
11、isters1=Right justified data in the result registers,S8C, S4C,S2C, S1C - Conversion Sequence Length,FIFO - Result Register FIFO Mode0=Conversion results are placed in the corresponding result register up to the selected sequence length.1=Conversion results are placed in consecutive result registers
12、(wrap around at end).,FRZ1:0 - Background Debug Freeze Enable,注:写ATDCTL3会终止当前转换序列,24,单片机原理与应用,ATD Registers 5/12,设置预分频因子时,0.25MHzAEDCLK 8.3MHz,ATD Control Register 4 (ATDCTL4),SMP2:0 - Sample Time Select,PRS4:0 - ATD Clock Prescaler,采样时间长,采样点电平更准确的逼近外部信号电平,注:写ATDCTL4会终止当前转换序列,25,单片机原理与应用,ATD启动:写ATDC
13、TL5会终止当前转换序列,并开始新一次转换此后,若外部触发信号每次满足触发条件都会开始一次ATD转换,ATD Registers 6/12,ATD Control Register 5 (ATDCTL5),SC - Special Channel Conversion0=Special channel conversions disabled1=Special channel conversions enabled,SCAN - Continuous Conversion Sequence Mode 0=Single conversion sequence1=Continuous conver
14、sion sequences (scan mode),MULT - Multi-Channel Sample 0=Sample only one channel1=Sample across several channels,CD,CC,CB,CA - Analog Input Channel Select Code,26,单片机原理与应用,单通道,单次转换多通道,单次转换单通道,序列转换模式多通道,序列转换模式特殊通道测试模式,ADC 工作方式,27,单片机原理与应用,ATD Registers 7/12,ATD Status Register 0 (ATDSTAT0),SCF - Sequ
15、ence Complete Flag0=Conversion sequence not completed1=Conversion sequence has completed,ETORF - External Trigger Overrun Flag0=No External trigger over run error has occurred1=External trigger over run error has occurred,FIFOR- Result Register Over Run Flag0=No over run has occurred1=Overrun condit
16、ion exists (result register has been written while associated CCFx flag was still set),CC3:0 - Conversion Counter 当前转换结果将写入的结果寄存器编号 例如: CC3:0=0110, 转换结果写入第6个结果寄存器先入先出模式下,不被初始化,当计数到最大值回最小值非先入先出模式下,转换开始和结束时被初始化为0两种模式下终止ATD转换或开始新ATD转换均被清零,28,单片机原理与应用,ATD Registers 8/12,ATD Compare Enable Register (ATDC
17、MPE),CMPE15:0 - Compare Enable for Conversion Number n (n= 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) of a Sequence0=No automatic compare1= Automatic compare of results for conversion n of a sequence is enabled,对一个序列中第n次ATD转换结果进行比较,除CMPEn=1,还要完成如下设置:将比较阈值写入ATDDRn写ATDCPMHT的CMPHTn位选择比较方法,注:
18、写ATDCMPE会终止当前转换序列,29,单片机原理与应用,ATD Registers 9/12,ATD Status Register 2 (ATDSTAT2),CCF15:0 - Conversion Complete Flag n (n= 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)0=Conversion number n not completed or successfully compared1=If (CMPEn=0): Conversion number n has completed. Result is rea
19、dy in ATDDRn. If (CMPEn=1): Compare for conversion result number n with compare value in ATDDRn, using compare operator CMPGTn is true. (No result available in ATDDRn),出现以下任一情况,CCFn被清零:写ATDCTL5当AFFC=0,向CCFn写1当AFFC=1,且CMPEn=1,写ATDDRn,30,单片机原理与应用,ATD Registers 10/12,ATD Input Enable Register (ATDDIEN)
20、,IEN15:0 - ATD Digital Input Enable on channel x (x= 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)0=Disable digital input buffer to ANx pin1=Enable digital input buffer on ANx pin,通用I/O口AD口与ATD模块的模拟输入引脚复用将某个模拟输入引脚作为通用输入引脚使用时,必须将对应IEN位置位,31,单片机原理与应用,ATD Registers 11/12,ATD Compare Higher Than
21、 Register (ATDCMPHT),CMPHT15:0 - Compare Operation Higher Than Enable for conversion number n (n= 15, 14, , 2, 1, 0) of a Sequence0= If result of conversion n is lower or same than compare value in ATDDRn, this is flagged in ATDSTAT21= If result of conversion n is higher than compare value in ATDDRn
22、, this is flagged in ATDSTAT2,注:写ATDCMPHT会终止当前转换序列,32,单片机原理与应用,ATD Registers 12/12- data format,ATD Conversion Result Registers (ATDDRn),ATDDR0ATDDR15,关闭比较操作时,ATD转换结果写入ATDDRn,要写入的ATDDRn的编号由ATDSTAT0的CC3CC0转换计数给出,转换结果在ATDDRn中的放置位置与转换精度和结果对齐方式有关,使能比较功能时,ATD转换结果和比较阈值都总是12位,与SRES1:0(ATDCTL1)无关,DJM=0 左对齐,
23、DJM=1 右对齐,转换结果不放入结果寄存器,比较完成后丢失,33,单片机原理与应用,本节提要,一、A/D转换基础知识二、ATD模块简介三、ATD模块寄存器四、ATD模块编程范例,34,单片机原理与应用,ATD模块编程范例,ATD模块编程方法,ATD模块编程范例,ATD初始化:先写除ATD0CTL5外的控制寄存器,再写ATD0CTL5完成初始化,并启动转换;,ATD结果读取:通过查询ATD0STAT0中的SCF位或采用中断方式,确定转换完成,读取相关结果寄存器取得ATD转换结果。,任务:分别对特殊通道VRH、VRL和(VRH+VRL)/2进行 ATD转换,35,单片机原理与应用,ATD模块编程
24、范例(续1),/*/ *S12X128 ATD模块范例程序*/*#include /* common defines and macros */#include /SCI相关函数/SCI初始化函数void SCI_init(void);void SCISendChar(char c1);void SCISendString(char * pc2);void SCISendHex(char c1);unsigned char dtoa(char c1);/ATD相关函数/ATD初始化函数void ATD_init(void) ;,36,单片机原理与应用,ATD模块编程范例(续2),/*主程序*/
25、void main(void) SCI_init();ATD_init();ATD0CTL5=0 x45; /单次转换,单通道,选择特殊通道VRLwhile(!(ATD0STAT0) /END OF MIAN,37,单片机原理与应用,ATD模块编程范例(续3),/*SCI相关函数*/void SCI_init(void) SCI0BDH=0; SCI0BDL=52; SCI0CR2=0 x0C;void SCISendChar(char c1) while(SCI0SR1_TC=0); SCI0DRL=c1; void SCISendString(char *pc1) while(*pc1)!=0) SCISendChar(*pc1); pc1+ ; void SCISendHex(char c1) SCISendChar(dtoa(c1,38,单片机原理与应用,ATD模块编程范例(续4),/*ATD相关函数*/ATD初始化函数void ATD_init(void) ATD0CTL1=0 x0F; /选择8位转换精度ATD0CTL2=0 x40; /打开CCF快速清零,关闭外部触发输入,关闭中断ATD0CTL3=0 x08; /数据左对齐,non-fifo,转换序列长度为1ATD0CTL4=0 xE3; /采样时间为24个ATD时钟周期,ATDClk=8MB/8=1MHz,
链接地址:https://www.31ppt.com/p-1552714.html