本帖最后由 wnwnwn 于 2013-6-15 19:43 编辑
麻烦大家帮我看下这段程序,为什么两个通道采集上来得数据都是一样的,而且实时改变输入值,采集无变化。
我在网上查说是要先采集ADCL,然后再采集ADCH数据,可程序确实是先低后高,怎么还是不行呢?
#define F_CPU 4000000UL
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
typedef unsigned char INT8U;
typedef unsigned int INT16U;
const PROGMEM INT8U SEG_CODE[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff};
volatile INT8U Display_Buffer[] = {0, 0, 0, 10, 0, 0, 0, 10};
void ADC_Convert(INT8U CH);
int main(void)
{
INT8U i;
DDRA = 0xfc;
DDRC = 0xff; PORTC = 0x00;
DDRD = 0xff; PORTD = 0x00;
ADCSRA = 0xe6;
SFIOR = 0x00;
_delay_ms(3000);
while (1)
{
ADC_Convert(0);
ADC_Convert(1);
for (i = 0; i < 8; i++)
{
PORTC = 0xff;
PORTD = _BV(i);
PORTC = pgm_read_byte(&SEG_CODE[Display_Buffer]);
if (i == 0 || i == 4)
PORTC &= 0x7f;
_delay_ms(4);
}
}
}
void ADC_Convert(INT8U CH)
{
INT16U Result;
if (CH == 0)
ADMUX = 0x40;
else
ADMUX = 0x41;
//Result = (int)(ADC * 500.0 / 1023.0);
Result = (INT16U)((ADCL + (ADCH << 8)) * 500.0 / 1024.0);
Display_Buffer[CH * 4] = Result / 100;
Display_Buffer[CH * 4 + 1] = Result / 10 % 10;
Display_Buffer[CH * 4 + 2] = Result % 10;
}
avr ad连续转换.rar
(51.11 KB)
|