0
\$\begingroup\$

What's up folks?

I've been developing a project using atmega328p and avr-gcc. Recently, I've tried to configure the ADC to single sample. The codes below show my main.c, ADC.c and ADC.h

//main.c

#include <avr/io.h>
#include <stdint.h>
#include <avr/sleep.h>
#include "drivers/ADC.h"

volatile int timer_counter = 0; //value of timer_counter
volatile uint8_t converteu;

int main (void){
        CLKPR = (1<<CLKPCE); // enabling bit writing in clock divider
        CLKPR = 2; //clock divider = 4, from 8 MHz to 2 MHZ
        configure_ADC(); // clock = 62.5 KHz, AREF = AVCC, single sample

        DDRB = 7; // PB0, PB1, PB2 as output

        while(1){
           sei();
           set_sleep_mode(SLEEP_MODE_IDLE);
           sleep_mode();
        }
}
//ADC.c

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

ISR(ADC_vect){
        PORTB ^= 2;      
}

void configure_ADC(){

        ADMUX |= (1<<REFS0); //short circuiting AVCC with AREF, ADC0 is the port enable to be read


        ADMUX &= ~((1<<MUX0)+(1<<MUX1)+(1<<MUX2)+(1<<MUX3)); //selecting ADC0
        ADCSRA |= (1<<ADEN); //ADC enable
        ADCSRA |= (1<<ADPS2) + (1<<ADPS0); // AVCC as reference

        ADCSRA |= (1<<ADIE); //enabling interruption
        PRR &= ~PRADC; // no low power on ADC
}

//ADC.h
void configure_ADC();

These code should do nothing, just configure ADC to run in single sample. However when I run it in my microcontroller, without activating ADSC (bit that starts a conversion), the PORTB 2 is activated. The only place that enables PORTB 2 is in the ADC's ISR.

The ISR is executed without activating the ADSC bit.

What's happening with this code?

\$\endgroup\$
14
  • \$\begingroup\$ What does PORTB gets activated mean? \$\endgroup\$
    – User323693
    Feb 9 '20 at 4:41
  • \$\begingroup\$ "These code should do nothing, just configure ADC to run in single sample." - but the comment says "auto triggering enabled, trigger_event = free running"! \$\endgroup\$ Feb 9 '20 at 7:24
  • \$\begingroup\$ The free running comment is a past configuration that I tried. I forgot to erase it. I will edit the question. \$\endgroup\$ Feb 9 '20 at 12:40
  • \$\begingroup\$ @User323693 PORTB gets activated means that a logic one is written on the pin. \$\endgroup\$ Feb 9 '20 at 12:42
  • \$\begingroup\$ What is teh default state of the pin? If you comment that port B Part in ADC ISR wil it still behave the same? \$\endgroup\$
    – User323693
    Feb 9 '20 at 16:30
0
\$\begingroup\$

I guess I discovered the reason for the ISR being requested without changes in ADSC.

The Idle mode's description, in the datasheet, states:

" If the ADC is enabled, a conversion starts automatically when this mode is entered."

The idle mode is triggering the ADC.

\$\endgroup\$
1
  • \$\begingroup\$ That's good. you seem to have found the solution \$\endgroup\$
    – User323693
    Feb 10 '20 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.