第一次用51单片机,编了个adc的程序,是24bit的,先放在这里吧,纪念纪念!
其他的初始化语句就免了,
程序功能:串口波特率设置为9600,串口发给单片机一个字符,adc转换一次,并发回转换结果
运行效果:以前只用过12bit的,这个24bit的果然够niux
ADC0 convert once! Vin = 228.592800 mV
ADC0 convert once! Vin = 228.586900 mV
ADC0 convert once! Vin = 228.583800 mV
ADC0 convert once! Vin = 228.595700 mV
ADC0 convert once! Vin = 228.589200 mV
ADC0 convert once! Vin = 228.598300 mV
ADC0 convert once! Vin = 228.583200 mV
ADC0 convert once! Vin = 1054.450000 mV
ADC0 convert once! Vin = 1054.437000 mV
ADC0 convert once! Vin = 1054.443000 mV
ADC0 convert once! Vin = 1054.425000 mV
ADC0 convert once! Vin = 1054.448000 mV
ADC0 convert once! Vin = 1054.438000 mV
ADC0 convert once! Vin = 1054.421000 mV
ADC0 convert once! Vin = 1054.442000 mV
ADC0 convert once! Vin = 1054.449000 mV
//********* ADC 初始化********
void ADC0_Init (void)
{
//unsigned ADC0_decimation;
ADC0MD =0x00;//bit7 使能 bit2..0方式控制
ADC0CN = 0;
//ADC0CN |= 0x10;//1 双极性 0 单极性
//ADC0CN |=0x08;//电流源使能
ADC0CN |=0x00;//PGA 增益设置: 0x00~0x07 1 2 4 8 16 32 64 128
PGA = 1;
ADC0CF = 0x00;
#ifdef FF
ADC0CF |= 0x10;// 1 for fast filter 0 for sinc3 filter
#endif
//ADC0CF |= 0x04;//bit3 1 外部ref 0 内部ref 2.5V
ADC0CLK = (SYSCLK/MDCLK)-1; //MDCLK = sysclk / (ADC0CLK+1)
ADC0DECH = 0x0f;
ADC0DECL = 0xff; //抽取比 设置
/*ADC0_decimation = (unsigned long) SYSCLK/ (unsigned long) OWR /
(unsigned long) (ADC0CLK+1)/(unsigned long)128;
ADC0_decimation--;
ADC0DEC = ADC0_decimation;*/
//ADC0DAC = 0x00;//偏移DAC数值
ADC0BUF = 0x00;//输入缓冲器控制寄存器
ADC0STA = 0x00;//清零状态寄存器
ADC0COH = 0;
ADC0COM = 0;
ADC0COL = 0;//偏移
ADC0CGH =0;
ADC0CGM =0;
ADC0CGL =0;//增益
ADC0MUX = 0x00;
ADC0MUX |= 0x00;//正输入通道选择
//bit7..4 :‘0--7’ ain0 -- ain7
//‘f’ 温度传感器
//其他 agnd
ADC0MUX |=0x01;//负输入通道选择
//bit7..4 :‘0--7’ ain0 -- ain7
//‘f’ 温度传感器
//其他 agnd
REF0CN = 0x01;
ADC0MD |= 0x80;//ADC0 enable
}
void main (void)
{
float ADC0_result;
PCA0MD &= ~0x40; // 禁止看门狗
PORT_Init(); // 初始化端口I/O
SYSCLK_Init (); // 初始化系统时钟
UART0_Init(); // 串口初始化
ADC0_Init ();
EA=1; //IE.7 使能CPU中断
//EIE1 |= 0x08; //ADC0 中断使能
//ES0=1; //IE.4 使能 USART 发送接收中断
printf("USARTtest...\n");
ADC_reg(); //对ADC0进行校准
while(1)
{
if(RI0)
{
//printf("recieved one byte!\n");
ADC_once();
RI0 = 0;
}
if(ADC0STA & 0x20) //转换结束AD0INT
{
ADC0STA &= ~0x20; //清除结束标志
ADC0_result = get_ADC0_result();
printf("\nADC0 convert once! Vin = %08.6f mV",ADC0_result);
}
}
}
用户377235 2012-2-14 11:05
你说的是他俩单挑,真要是大国插手就不好说了!
xucun915_925777961 2010-11-13 14:13