Author Topic: ADC ISR causing reset?  (Read 4760 times)

0 Members and 1 Guest are viewing this topic.

Offline dualTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
ADC ISR causing reset?
« on: October 26, 2009, 09:25:11 PM »
I'm having a very bad reset issue and I think it may have to do with my ISR. I use AT90USB646, 16MHz, AVR Studio 4.17. I am using a structure I called filter to do all my software filtering and I have all the filtering within the ISR. I am not sure if this is causing the reset in my ISR. But here is what I have now. BTW, the one reason why I deduced my problem to be the ISR is my AVR does not reset whenever I comment sei() in my main code.

Code: [Select]
int main(void)

hardware(0); //initialize all hardware
on; //turn on power led
calibrate(); //calibrate sensors
behavior_Init(); //initialize behavior structures
//LCD_INIT(); //initialize LCD
//LCD_COMMAND(LCD_CLEAR_HOME); // Clear LCD screen, send cursor to start
//sprintf(out,"test"); //lcd test print
//LCD_STRING(out); //print out
//_delay_ms(1000); //power up delay
ADCSRA |= (1<<ADSC); //start conversion
//sei(); //enable global interrupts

// LCD_COMMAND(LCD_CLEAR_HOME); // Clear LCD screen, send cursor to start
// sprintf(out,"%d %d", straight.priority, winner.priority);
// LCD_STRING(out);
// LCD_ADDR(0x40); // Send cursor to address 0x40 (second row)
// LCD_STRING("str8t, wnr"); // Write "ATMEL Lecture" to LCD

arbitrate(); //arbitrate behaviors
LeftMotor(winner.motorLeft, winner.movespeed); //set left motor parameters
RiteMotor(winner.motorRight, winner.movespeed);//set right motor parameters


//read value into sensor index Amux, array input filtIndex
sensor[Amux].input[sensor[Amux].index] = ADCW;
//do filter
//subtract oldest from sum and add newest
sensor[Amux].sum = sensor[Amux].sum - sensor[Amux].oldest + sensor[Amux].input[sensor[Amux].index];
sensor[Amux].output = sensor[Amux].sum >> FILTPOWER;
sensor[Amux].index++; //increment index when all analog inputs are filled
if(filtIndex > 15) sensor[Amux].index = 0;
sensor[Amux].oldest = sensor[Amux].input[sensor[Amux].index];

//prepare indices for subsequent reading
Amux++; //increment mux counter
if(Amux>7) Amux=1; //reset counter when 8
ADMUX &=~0x0f; //clear ADC control reg mux bits
ADMUX |= (Amux<<MUX0); //set adc control mux to next channel
ADCSRA |= (1<<ADSC); //start conversion

ADC ISR causing reset?

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: ADC ISR causing reset?
« Reply #1 on: October 27, 2009, 07:52:28 AM »
Do you have an ISR (interrupt service routine)? If not then when an interrupt happens it goes back to the beginning of your code and looks like a reset.

Offline dualTopic starter

  • Jr. Member
  • **
  • Posts: 43
  • Helpful? 0
Re: ADC ISR causing reset?
« Reply #2 on: October 27, 2009, 08:35:58 AM »
yeah, it's in the code that I posted. I have seen it work too when I used to not have the filtering within the ISR itself

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: ADC ISR causing reset?
« Reply #3 on: October 27, 2009, 10:24:30 AM »
Your ISR looks way too long.

Have you ISR only have the read and set to a variable(remember to declare it as volatile). Your main code should have all the ifs and thens. Perhaps set the variable to 0 after doing the filter algorithm thing, so that after each ISR is done( the variable would obviously be > 0 in the ADC ISR), the code knows when to apply the filter.
Code: [Select]
volatile int variable=0;


while(variable !=  0) {
variable = 0;  // clear variable


ISR(ADC_vect) {
variable = Analog_Read();  // or whatever the function to read analog is.

Check out the Roboduino, Arduino-compatible board!

Link: http://curiousinventor.com/kits/roboduino


Offline rgcustodio

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 0
  • Use "Search" and ye might find answers!
Re: ADC ISR causing reset?
« Reply #4 on: October 28, 2009, 12:03:51 AM »
I think the length of the interrupt handling function doesn't matter. The only effect it will have is that other interrupts will be missed while the interrupt handler is running!

sei() enables interrupts from being seen by the processor. If you don't call it your processor will not see any interrupts and thus your interrupt handler will not be called. Not sure if the AVRs need to call sei() on startup. Check your processor's documentation.

Other things to consider:
1) interrupt handler registration. Are you registering your interrupt handler properly?
2) variables accessed in the interrupt handler. Are they all properly initialized? Some might be uninitialized causing invalid memory access. This might be way of the system of handling invalid memory access inside an interrupt, to reset the system.
3) did you disable the interrupts (cli()) when one interrupt is already running??

The best thing one can do when it's raining is to let it rain. - H. W. Longfellow

understanding is the path to enlightenment

Offline napnyl

  • Jr. Member
  • **
  • Posts: 10
  • Helpful? 0
Re: ADC ISR causing reset?
« Reply #5 on: October 30, 2012, 08:56:19 PM »
Add this to your code:

Code: [Select]
maybe you are not writing an interrupt you enabled as in my case. it won't affect other interrupts just does something in the interrupt vectors you didn't write. I'm using it and reseting has gone.


Get Your Ad Here