本帖最后由 lin196@21cn.com 于 2015-12-18 16:09 编辑
AVR AT90USB1287, ADC 寄存器写不进去, 读出来全是0; 仿真时,在电脑软件上看不到这些寄存器有变化,发现AVR STUDIO 软件上的I/O viwe 里面的Address 是写着'na', 但不知道是什么意思;下面是ADC 寄存器图:
你们遇到过这情况吗,
#define BatDete_GetStatus Get_Bit(PINF,0)
#define BatDete_Internal_SetOut Set_Bit(DDRF,0)
#define BatDete_Internal_SetIN Clr_Bit(DDRF,0)
#define ADC_channal_0 0
unsigned int adc_result=0;
unsigned char Btemp;
void Battery_ADC_init();
void Battery_ADC_init()
{
BatDete_Internal_SetIN; //will IO set input
ADCSRA = 0b10000100; //enable ADC power, stop adc, single conversion, adc interrupt no used, ADC prescaler 16
ADCH = 0x00;
ADCL = 0x00;
ADCSRB = 0x00;
DIDR0 = 0b00000001; //ADC0D set 1
ADMUX = 0b00000000; //selection external AREF, adc result right, selection ADC0 channel
}
void ADC_start_or_stop(unsigned char StartOrStop);
void ADC_start_or_stop(unsigned char StartOrStop)
{
if(StartOrStop)
ADCSRA = (1<<ADSC); // Start ADC Conversion
else
ADCSRA = (0<<ADSC); // Stop ADC Conversion
}
unsigned int ADC_StartConversion(unsigned char ADC_channal);
unsigned int ADC_StartConversion(unsigned char ADC_channal)
{
unsigned char i;
unsigned int temp;
adc_result=0;
ADCSRA = (1<<ADEN); //enable ADC
switch(ADC_channal) //selection channal
{
case 0:
ADMUX = (0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(0<<MUX0); //selection ADC channal 0
break;
case 1:
ADMUX = (0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(1<<MUX0); //selection ADC channal 1
break;
case 2:
ADMUX = (0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(1<<MUX1)|(0<<MUX0); //selection ADC channal 2
break;
case 3:
ADMUX = (0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(1<<MUX1)|(1<<MUX0); //selection ADC channal 3
break;
case 4:
ADMUX = (0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(0<<MUX1)|(0<<MUX0); //selection ADC channal 4
break;
case 5:
ADMUX = (0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(0<<MUX1)|(1<<MUX0); //selection ADC channal 5
break;
case 6:
ADMUX = (0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(1<<MUX1)|(0<<MUX0); //selection ADC channal 6
break;
case 7:
ADMUX = (0<<MUX4)|(0<<MUX3)|(1<<MUX2)|(1<<MUX1)|(1<<MUX0); //selection ADC channal 7
break;
}
for(i=0;i<100;i++) //adc enable need time
;
ADC_start_or_stop(1);
i = 0;
while((!ADIF)&&(i<254))
i++;
temp = ADCH;
adc_result = temp<<8;
adc_result |= ADCL;
return 0;
}
|