飞思卡尔HC12系类AD转换模块讲解.pdf
Freescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.TMFreescale HCS12 微控制器微控制器微控制器微控制器TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 1Internal BusSCI1256K FLASEEPROM12K SRAMATD1Star12 CPUBKP INT MMICM BDM MEBI4K BYTESEEPROMSIM msCAN3msCAN2msCAN1SCI1SPI 2orPWMCH4-7BDLCormsCAN0msCAN4orIICSPI 1or PWMCH 0-3SPI 0ATD0PIM PLL PITECT8CHANPWM8CHANA/D转换模块转换模块转换模块转换模块TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 2AD转换模块转换模块转换模块转换模块特点特点特点特点:8/10 位精度位精度位精度位精度 7 us,10-位单次转换时间位单次转换时间位单次转换时间位单次转换时间.采样缓冲放大器采样缓冲放大器采样缓冲放大器采样缓冲放大器 可编程采样时间可编程采样时间可编程采样时间可编程采样时间 左左左左/右右右右 对齐对齐对齐对齐,有符号有符号有符号有符号/无符号结果数据无符号结果数据无符号结果数据无符号结果数据 外部触发控制外部触发控制外部触发控制外部触发控制 转换完成中断转换完成中断转换完成中断转换完成中断 模拟输入模拟输入模拟输入模拟输入8通道复用通道复用通道复用通道复用 模拟模拟模拟模拟/数字输入引脚复用数字输入引脚复用数字输入引脚复用数字输入引脚复用 1到到到到8转换序列长度转换序列长度转换序列长度转换序列长度 连续转换模式连续转换模式连续转换模式连续转换模式 多通道扫描方式多通道扫描方式多通道扫描方式多通道扫描方式TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 3A/D 寄存器寄存器寄存器寄存器转换结果寄存器转换结果寄存器转换结果寄存器转换结果寄存器控制寄存器控制寄存器控制寄存器控制寄存器状态寄存器状态寄存器状态寄存器状态寄存器TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 4Address offset$0002A/D 控制寄存器控制寄存器控制寄存器控制寄存器2ADPU-A/D 电源使能/禁止1=A/D模块上电0=禁止A/D,以减少功耗AFFC-A/D 快速转换完成标志位清零1=快速标志位清零顺序每次读取结果寄存器自动清零0=正常标志位清零顺序需要手动对状态标志位清零AWAI-A/D 等待模式1=等待模式下,转换0=等待模式下,禁止转换ASCIE-A/D 顺序完成中断使能ASCIF-A/D 顺序完成标志位高电平触发,每次触发,执行连续转换X111低电平触发,每次触发,执行连续转换X101上升沿触发,每次触发,执行一次转换X110下降沿触发,每次触发,执行一次转换X100忽略外部触发,执行连续转换后10 xx忽略外部触发,执行一次转换后停止00 xx描述SCANETRIGEETRIGPETRIGLEATDCTRL2TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 5FRZResponse00Ignore IFREEZE01Reserved10Finish conversion,then freeze11Freeze ImmediatelyAddress offset$0003转换序列长度转换序列长度转换序列长度转换序列长度FIFO 结果寄存器 FIFO1=结果寄存器没有映射到转换序列0=结果寄存器没有映射到转换序列A/D 控制寄存器控制寄存器控制寄存器控制寄存器3ATDCTRL3转换序列长度就是需要转换的通道数转换序列长度就是需要转换的通道数转换序列长度就是需要转换的通道数转换序列长度就是需要转换的通道数比如:通道0,1,2作为AD采集通道则转换序列为3,即S2C=1,S1C=1在控制寄存器5 ATD0CTL5中:设CC CB CA=0 0 0,转换序列从通道0开始;ATD0DR0,ATD0DR1,ATD0DR2存放转换结果设CC CB CA=0 1 0,转换序列从通道2开始。仍然是ATD0DR0,ATD0DR1,ATD0DR2存放转换结果TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 65位 模数计数器预分频器-由A/D控制寄存器中的PRS4:0控制-分频系数从2到64-如果 PRS4:0=0,预分频不起作用注:设置PRS4:0时,A/D Clock 不能大于 2 MHz.最大最大最大最大 A/D 时钟时钟时钟时钟=2.0 MHz (最小最小最小最小 A/D CLOCK=0.5 MHz)ATDCTL4(HI)-A/D 控制寄存器控制寄存器控制寄存器控制寄存器SRES8-A/D 精度选择精度选择精度选择精度选择1=8 位位位位0=10位位位位采样时间选择采样时间选择采样时间选择采样时间选择Address offset$0004A/D 时钟选择时钟选择时钟选择时钟选择/预分频预分频预分频预分频除除除除25位位位位 模数计数器模数计数器模数计数器模数计数器预分频器预分频器预分频器预分频器PRS0-PRS4系统时钟系统时钟系统时钟系统时钟A/D 时钟时钟时钟时钟16 A/D时钟周期118 A/D时钟周期104 A/D时钟周期012 A/D时钟周期00采样时间采样时间采样时间采样时间SMP 1:0TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 7转换时间转换时间转换时间转换时间A/D 时钟2,4,8,16时钟周期总是2 个时钟周期转换时间计算举例:(假设 2MHZ A/D 时钟频率)例 1:转换时间=Initial Sample Time+Programmed Sample Time+Resolution Period=2+2+10=14 A/D Clocks=7uSec例 2:转换时间=Initial Sample Time+Programmed Sample Time+Resolution Period=2+16+10=28 A/D Clocks=14uSecCCF标志位置位,如果在8通道模式下,序列结束TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 8SCAN 连续转换模式1=连续转换模式0=单次转换注意注意注意注意:对这个寄存器写操作时对这个寄存器写操作时对这个寄存器写操作时对这个寄存器写操作时,将会中断当前的转换将会中断当前的转换将会中断当前的转换将会中断当前的转换,然后重新启动新的转换序列然后重新启动新的转换序列然后重新启动新的转换序列然后重新启动新的转换序列通道选择通道选择通道选择通道选择0 0 0=Chan 0-1 1 1 =Chan 7Address offset$0005A/D控制寄存器控制寄存器控制寄存器控制寄存器5MULT 多通道采样模式1=多通道转换0=单通道转换DSGN 符号选择1=有符号0=无符号ATDCTRL5 DJM 对齐方式1=右对齐0=左对齐TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 9ATDSTAT0Address Offset$0006$0007A/D 状态寄存器状态寄存器状态寄存器状态寄存器FIFOR 当结果寄存器在读出之前已经被写入时,置位(CCF没有清零)SCF 转换序列完成标志-在单次转换模式时,当转换完成后置位(SCAN=0)在连续转换模式时,当第一次转换完成后置位(SCAN=1).当(AFFC=0),写1清零.ETORF-外部触发覆盖标志-如果在转换过程中高/低电平出现,置位CC2:0 转换计数器3-位计数器指向下一个将要转换的通道CCF7-CCF0 独立通道转换完成标志位每个相应的通道转换结束后置位,当相应的A/D结果寄存器被读出时,清零ATDSTAT1TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 10转换结果寄存器转换结果寄存器转换结果寄存器转换结果寄存器Address Offset$0010-$0011-$001E -$001FAddress Offset$0010-$0011-$001E -$001F左对齐数据存放格式左对齐数据存放格式左对齐数据存放格式左对齐数据存放格式右对齐数据右对齐数据右对齐数据右对齐数据存放格式存放格式存放格式存放格式/假设左对齐10位精度Word AD_wValue;AD_wValue=ATD0DR06;TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 11A/D口寄存器口寄存器口寄存器口寄存器PORTAD1ADA7ADA6ADA5ADA4ADA3ADA2ADA1ADA0ATDPT注意:任何一个引脚可以用作A/D或数字输入Address Offset$000FATDDIEN-ATD 数字输入使能TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 12AD使用示例使用示例使用示例使用示例-查询方式查询方式查询方式查询方式void AD_Init(void)/初始化ATD0CTL2=0 xC0;/AD模块上电,快速清零,无等待模式,禁止外部触发,中断禁止ATD0CTL3=0 x20;/每个序列4次转换,No FIFO,Freeze模式下继续转换ATD0CTL4=0 x83;/8位精度,2个时钟,ATDClock=BusClock*0.5/PRS+1 ;PRS=3,divider=8ATD0CTL5=0 xA0;/右对齐无符号,单通道采样,通道0 ATD0DIEN=0 x00;/禁止数字输入void AD_GetValue(word*AD_wValue)/读取AD转换结果*AD_wValue=ATD0DR0;/Read out the Result Registerword AD_wValue;void main(void)word i;AD_Init();/AD初始化DDRB=0 xFF;PORTB =0 x00;EnableInterrupts;for(;)while(!ATD0STAT1_CCF0);/等待转换结束AD_GetValue(&AD_wValue);/读取转换结果PORTB=(byte)AD_wValue;/在B口显示转换值 TMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 13AD使用示例使用示例使用示例使用示例-中断方式中断方式中断方式中断方式word AD_wData=0;void main(void)word i;AD_Init();DDRB=0 xFF;PORTB =0 x00;EnableInterrupts;for(;)PORTB=(byte)AD_wData;void AD_Init(void)ATD0CTL2=0 xC2;/AD模块上电,快速清零,无等待模式,禁止外部触发,中断开放ATD0CTL3=0 x20;ATD0CTL4=0 x83;ATD0CTL5=0 xA0;ATD0DIEN=0 x00;#pragma CODE_SEG NON_BANKEDvoid interrupt 22 Int_AD0(void)AD_wData=ATD0DR0;/Read out the Result RegisterTMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 14AD使用例程使用例程使用例程使用例程void AD_Init(void)/AD初始化/控制寄存器2:上电,标志位快速清零,开中断ATD0CTL2 =(ATD0CTL2_ADPU_MASK|ATD0CTL2_AFFC_MASK|ATD0CTL2_ASCIE_MASK);/控制寄存器3:转换序列长度为3,FIFO模式ATD0CTL3 =(ATD0CTL3_S2C_MASK|ATD0CTL3_S1C_MASK|ATD0CTL3_FIFO_MASK);/控制寄存器4:ATD0CTL4 =(ATD0CTL4_SRES8_MASK|ATD0CTL4_PRS1_MASK|ATD0CTL4_PRS0_MASK);/控制寄存器5:ATD0CTL5 =(ATD0CTL5_DJM_MASK|ATD0CTL5_SCAN_MASK|ATD0CTL5_MULT_MASK);ATD0DIEN=0 x00;/禁止数字输入缓冲#pragma CODE_SEG NON_BANKED/中断服务程序void interrupt 22 Int_AD0(void)AD_wData0=ATD0DR0;/将结果寄存器中的值存放到数组中AD_wData1=ATD0DR1;/将结果寄存器中的值存放到数组中AD_wData2=ATD0DR2;/将结果寄存器中的值存放到数组中#pragma CODE_SEG DEFAULT_ROMTMFreescale Semiconductor Confidential and Proprietary Information.Freescale and the Freescale logo are trademarks of Freescale Semiconductor,Inc.All other product or service names are the property of their respective owners.Freescale Semiconductor,Inc.2005.Slide 15word AD_wData3;/全局变量存放 AD0,AD1,AD2的结果void main(void)dword i;AD_Init();/AD 初始化DDRB=0 xFF;PORTB =0 x00;AD_wData0=0;/全局变量初始化AD_wData1=0;AD_wData2=0;EnableInterrupts;for(;)if(ATD0CTL2_ASCIE=0)/采用查询方式while(!ATD0STAT1_CCF0);/等待通道0转换结束AD_wData0=ATD0DR0;/读通道0的转换结果while(!ATD0STAT1_CCF1);/等待通道1转换结束AD_wData1=ATD0DR1;/读通道1的转换结果while(!ATD0STAT1_CCF2);/等待通道2转换结束AD_wData2=ATD0DR2;/读通道2的转换结果PORTB=(byte)AD_wData0;/用PORT B来显示AD0的值for(i=0;i0 xFFFFF;i+);/delayPORTB=(byte)AD_wData1;/用PORT B来显示AD1的值for(i=0;i0 xFFFFF;i+);/delayPORTB=(byte)AD_wData2;/用PORT B来显示AD1的值for(i=0;i0 xFFFFF;i+);/delay