Continue to Site

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.

  • 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.

interrupt code in cvavr

Status
Not open for further replies.

hamzaali

New Member
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();
}
 
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...
 
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.
 
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.
 
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...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top