1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

interrupt code in cvavr

Discussion in 'AVR' started by hamzaali, Sep 24, 2014.

  1. hamzaali

    hamzaali New Member

    Joined:
    Sep 24, 2014
    Messages:
    3
    Likes:
    0
    hi guys,

    I am using the following code in cvavr, my code is not working. My adc read code is working if i don't use interrupt. So there is issue with my interrupt call, i tried to make it manual call :
    void main(void)
    {
    DDRB=0xFF;
    DDRC=0xFF;
    PORTC=0x00;
    PORTB=0x00;

    LCD_Init();
    delay_ms(100);
    adc_init();
    #asm("sei")
    while (1)
    {
    delay_ms(1000);
    PORTB.2= ~PORTB.2;
    }
    }
    interrupt [ADC_INT] void adc_isr(void)
    {
    ADCSRA |= (1<<ADSC);
    read_adc();
    }
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,145
    Likes:
    907
    Location:
    Rochdale UK
    You really need to study the data sheet.... You are starting the conversion in the ISR!! How can the the first conversion be done.... First enable the ADC then start conversion... Once you interrupt fires, read the ADC then set the conversion..

    The AVR has a free running ADC so just start the first conversion and just read when the ADIF is set..

    ADCSRA |= (1<<ADEN); // You need this to turn it on...
     
  3. hamzaali

    hamzaali New Member

    Joined:
    Sep 24, 2014
    Messages:
    3
    Likes:
    0
    well adc_init() and read_adc() are my functions. i am posting them:
    void read_adc()
    {
    while (ADCSRA & (1<<ADSC));
    theLowADC = ADCL;
    theTenBitResults = (ADCH<<8)|theLowADC;
    adc_value = theTenBitResults;

    }
    void adc_init()
    { ADCSRA &= 0x00;
    ADCSRA |= (1<<ADEN|1<<ADIE|1<<ADPS2|1<<ADPS1|1<<ADPS0);
    ADMUX &= 0x00;
    ADMUX |= (1<<REFS0);
    }​

    hope this can be useful in sorting out the error . these functions work in polling.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,145
    Likes:
    907
    Location:
    Rochdale UK

    When posting its a good idea to show all your code or we are second guessing...

    Even so you are setting your conversion in the interrupt.... The interrupt will not fire because the conversion is never started.... If you start the initial conversion after setting the SEI... then set the conversion bit after the read, it should work.
     
    • Like Like x 1
  6. hamzaali

    hamzaali New Member

    Joined:
    Sep 24, 2014
    Messages:
    3
    Likes:
    0
    Sorry if this is fowl language.
    I love you Ian Rogers.... you rock man... thanks .... i was pondering for 1 week over this.... my code is working now..... Thanks man...
     

Share This Page