MC9S08AC16 AD使用
0赞
发表于 2011/5/14 14:20:33
阅读(2959)
前篇博文介绍了飞思卡尔MC9S08AC16,现在上传一个ad转换的源代码,AD转换器内部结构如图所示
源码:
/*****************头文件*****************************************/
#include
#include "derivative.h" /* include peripheral declarations */
/*********************变量和函数定义****************************/
unsigned int DATA;
void ADC_INI(void);
void IO_INIT(void);
/*****************主函数*****************************************/
void main(void) {
void ADC_INI(void);
void IO_INIT(void);
/*****************主函数*****************************************/
void main(void) {
EnableInterrupts; /* enable interrupts */
/* include your code here */
ADC_INI();
IO_INIT();;
/* include your code here */
ADC_INI();
IO_INIT();;
for(;;) {
__RESET_WATCHDOG(); /* feeds the dog */
ADC1SC1=0x28; //选择通道号,使能adc转换
while(!ADC1SC1_COCO)//
__RESET_WATCHDOG();
DATA=ADC1R;
if(DATA>=500)
PTAD=0X02;
else
PTAD=0X00;
ADC1SC1=0x1f;
} /* loop forever */
/* please make sure that you never leave main */
}
__RESET_WATCHDOG(); /* feeds the dog */
ADC1SC1=0x28; //选择通道号,使能adc转换
while(!ADC1SC1_COCO)//
__RESET_WATCHDOG();
DATA=ADC1R;
if(DATA>=500)
PTAD=0X02;
else
PTAD=0X00;
ADC1SC1=0x1f;
} /* loop forever */
/* please make sure that you never leave main */
}
/*****************adc中断*****************************************/
interrupt 19 void ADC_INT(void) {
interrupt 19 void ADC_INT(void) {
ADC1SC1_COCO=0; // clear TOF清标志位
}
}
/*****************adc初始化*****************************************/
void ADC_INI(void) {
void ADC_INI(void) {
ADC1SC1=0x28;//选择通道号,使能ADC
ADC1SC2=0x00;
ADC1CFG = 0x49;//四分频、10位模式、时钟源为总线时钟/2
APCTL2=0b00000001; //选择管脚16(通道1?
ADC1SC1=0x1f; //禁止 ADC
}
/***********************I/O初始化********************************/
void IO_INIT(void){
PTADD=0xff;
PTAD=0X00;
}
ADC1SC2=0x00;
ADC1CFG = 0x49;//四分频、10位模式、时钟源为总线时钟/2
APCTL2=0b00000001; //选择管脚16(通道1?
ADC1SC1=0x1f; //禁止 ADC
}
/***********************I/O初始化********************************/
void IO_INIT(void){
PTADD=0xff;
PTAD=0X00;
}