MC9S12XS128 AD转化例程
0赞要让ATD 开始转换工作,必须经过以下三个步骤:
1.将ADPU 置1,使ATD 启动;
2.按照要求对转换位数、扫描方式、采样时间、时钟频率及标志检查等方式进行设置;
3.发出启动命令;
如果上电默认状态即能满足工作要求,那么只要将ADPU 置1,然后通过控制寄存器发出转换命令,即可实现转换。
程序描述:由通道ATD0进行单通道A/D转换,转换值在B口显示。
程序:(用指针实现AD转换)
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
byte AD_Value; //AD转换结果
void Delay(int i) { //延时程序
int j;
for(;i>0;i--)
for(j=500;j>0;j--)
;
}
/***---------------初始化程序---------------***/
(初始化程序与上述相同)
/***---------------读取AD转换结果---------------***/
void AD_GetValue(word *AD_Value){
*AD_Value=ATD0DR0;
}
void main(void) {
/* put your own code here */
_DISABLE_COP(); //关看门狗
InitBusClk();
InitAD();
DDRB=0XFF; //设PORTB为输出口
PORTB=0x00;
EnableInterrupts;
for(;;) {
while(!ATD0STAT2L_CCF7); //等待转换结束,退出循环
AD_GetValue(&AD_Value); //读取转换结果
Delay(400);
PORTB=AD_Value; //转换结果在B口显示
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
大功告成,yeah