Author Topic: [AVR] Cannot get the ADC value in Atmel Studio ! ADC register is not working  (Read 2966 times)

0 Members and 1 Guest are viewing this topic.

Offline expertmaxTopic starter

  • Regular Contributor
  • *
  • Posts: 51
Hi !

I have another issue !

Cannot get the ADC to work properly, the LED will only come on at 5V on the potentiometer !

Code: [Select]
#define F_CPU 16000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

int cylinder = 5;
uint16_t injection_time = 0;
uint16_t injection_gap = 0;

void delay(int t);
void cycle(void);
void sendFuel(int injector);
void sendSpark(int coil);
void getTPSValue(void);

int main(void)
{
DDRC |= (1 << PORTC0) | (1 << PORTC1) | (1 << PORTC2) | (1 << PORTC3) | (1 << PORTC4) | (1 << PORTC5);
MCUCR |= (1 << ISC01) | (1 << ISC00) | (1 << ISC11) | (1 << ISC10);
GICR |= (1 << INT0) | (1 << INT1);
ADCSRA |= (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2) | (1 << ADEN);
ADMUX |= (1 << REFS0) | (1 << ADLAR);

sei();

    while(1)
    {
getTPSValue();

if(injection_time < 512)
{
PORTC |= (1 << PORTC0);
}
if(injection_time > 512)
{
PORTC &= ~(1 << PORTC0);
}
    }
}

void delay(int t)
{
while (t != 0)
{
_delay_ms(1);
t = t - 1;
}
}

ISR(INT0_vect) // CKP
{
cylinder++;
cycle();
}

ISR(INT1_vect) // CMP
{
cylinder = 0;
}

void cycle()
{
if(cylinder == 1)
{
sendFuel(PORTC0);
sendSpark(PORTC4);
}
if(cylinder == 2)
{
sendFuel(PORTC2);
sendSpark(PORTC5);
}
if(cylinder == 3)
{
sendFuel(PORTC3);
sendSpark(PORTC4);
}
if(cylinder == 4)
{
sendFuel(PORTC1);
sendSpark(PORTC5);
}
}

void sendFuel(int injector)
{
PORTC |= (1 << injector);
delay(35);
PORTC &= ~(1 << injector);
delay(35);
}

void sendSpark(int coil)
{
PORTC |= (1 << coil);
delay(10);
PORTC &= ~(1 << coil);
}

void getTPSValue()
{
ADCSRA |= (1 << ADSC);
while(!(ADCSRA & (1 << ADIF)));
ADCSRA |= (1 << ADIF);
injection_time = ADC;
_delay_ms(1);
}

Thank you so much for your help !

EDIT: Did some tests and it seems like the register ADCL is totally empty, whatever the input voltage on ADC0 is !
« Last Edit: November 23, 2013, 04:19:53 am by expertmax »
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10239
  • Country: nz
PORTC0 looks odd, it would usually be PC0
But that's probably just a new naming convention i've not seen yet.

« Last Edit: November 23, 2013, 04:28:44 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10239
  • Country: nz
It's probably ADLAR=1 that's causing your problem.   (left align)

Using the ADC macro with ADLAR =1 probably results in bad data (Low High bits around the wrong way).

The reason the ADLAR option exists is so you can read the 8 MSB in one operation (when you don't care about 10bit resolution for your application). 
Since you want the full 10bits  (0-1024)  you want ADLAR=0 (default).
That way when you read the ADC macro it will assemble and output a 16bit word correctly

Try changing

ADMUX |= (1 << REFS0) | (1 << ADLAR);

to

ADMUX |= (1 << REFS0) ;
« Last Edit: November 23, 2013, 04:45:04 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline expertmaxTopic starter

  • Regular Contributor
  • *
  • Posts: 51
PORTC0 looks odd, it would usually be PC0
But that's probably just a new naming convention i've not seen yet.

PC0 is not recognized by Atmel Studio (anymore?). Anyway PORTC0, PC0, PINC0 yield the same hex value.

It's probably ADLAR=1 that's causing your problem.   (left align)

Using the ADC macro with ADLAR =1 probably results in bad data (Low High bits around the wrong way).

The reason the ADLAR option exists is so you can read the 8 MSB in one operation (when you don't care about 10bit resolution for your application). 
Since you want the full 10bits  (0-1024)  you want ADLAR=0 (default).
That way when you read the ADC macro it will assemble and output a 16bit word correctly

Try changing

ADMUX |= (1 << REFS0) | (1 << ADLAR);

to

ADMUX |= (1 << REFS0) ;


Yes, it did the trick. I also took more time to read the datasheet thoroughly and now I understand the whole concept behind the ADC, thanks for the input tho !
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf