Selucky
Selucky

Reputation: 3

Different values from ADC between BASCOM and C code?

I'm working on a new software version for one of our boards with an Atmel AT90CAN128 processor.

The older software version was written in BASCOM (not by me). To normalize our software we translated the software into C code.

Now I have a problem to get the right values of the ADC. I think it is a software problem because the values by the ADC differs when I use the BASCOM and the C Program on the same board.

For example:

I use a potentiometer to change the voltage. If the potential is in the same position the BASCOM program give me a value of 460 and the C program only 320.

Maybe there is a part of the BASCOM program which I didn't translated correctly.

I attached the code of the old BASCOM and of the new C code.

Code in BASCOM at the beginning of the file:

Config Adc = Single , Prescaler = 64 , Reference = Avcc     'ADC-Clock 8000000/64 = 125 kHz bei 8 MHz

' Later function 
Function Adc_abfragen() As Word
    Local Channel As Byte
    'Admux = &B  01 0 11011         ' ADMUX: 01 =reference = avcc, 0 = right adjust 11011 = adc3 = plus, adc2 = Gnd
    Channel = 3
    Start Adc
    Adc_abfragen = Getadc(channel , 88) ' 88 addieren zu channel 3 = admux = &B01011011
    Stop Adc
End Function

C code (function init_ADC is called at beginning of main function

void init_ADC(void)
{
    ADCSRA      |= (1<<ADEN);   // enable ADC

    // Prescaler to 64          
    // 8 MHz / 64 => 125kHz 

    ADCSRA      &= ~(1<<ADPS0);
    ADCSRA      |= (1<<ADPS1);                                      
    ADCSRA      |= (1<<ADPS2);

    ADMUX       &= ~(1<<REFS1); // AVCC
    ADMUX       |= (1<<REFS0);

    ADMUX       &= ~(1<<ADLAR); // right align

    // MUX4-0 adc3 = plus, adc2 = Gnd
    ADMUX       |= (1<<MUX4);
    ADMUX       |= (1<<MUX3);
    ADMUX       &= ~(1<<MUX2);
    ADMUX       |= (1<<MUX1);
    ADMUX       |= (1<<MUX0);

    // read first value
    ADCSRA      |= (1<<ADSC); // Start ADC conversion
    while(ADCSRA &(1<<ADSC)); // Wait for conversion
}

// Function is called later in the main loop
uint16_t ADC_ReadValue (void)
{
    ADCSRA      |= (1<<ADSC); // Start ADC conversion
    while(ADCSRA &(1<<ADSC));// Wait for conversion
    return ADC;
}

Upvotes: 0

Views: 495

Answers (0)

Related Questions