STC单片机ADC转换的例子
#include <intrins.h>sfr ADC_CONTR= 0XBC;sfr P1ASF= 0X9D;sfr ADC_RES= 0XBD;sfr ADC_RESL= 0XBE;#define AD_SPEED 0x60void init_adc(){ADC_CONTR = ADC_CONTR|0x80; //打开adc电源P1ASF = 0X01; //设置P1.7为模拟功能}void adc_delay(unsigned char delay_time) // 延时函数{ unsigned int n; while(delay_time--) { n = 6000; while(--n); }}unsigned int get_AD_result(unsigned char channel){ unsigned char AD_finished=0; unsigned int h_val;unsigned int l_val; ADC_RES = 0; ADC_RESL = 0; channel &= 0x07; ADC_CONTR = AD_SPEED; _nop_(); ADC_CONTR |= channel; _nop_(); ADC_CONTR |= 0x80; adc_delay(1); ADC_CONTR |= 0x08; AD_finished = 0; while ( AD_finished ==0 ) { AD_finished = (ADC_CONTR & 0x10); } ADC_CONTR &= 0xE7; h_val = ADC_RES;h_val = h_val<<2;l_val = ADC_RESL & 0X03;h_val = h_val | l_val;return (h_val); }