PDA

View Full Version : Axon hardware interrupts, help!



Resilient
03-17-2009, 12:35 PM
I have the following code:



volatile int32_t left_clicks = 0;
volatile int32_t right_clicks = 0;
const int WHEEL_UPDATE = 64;


ISR (SIG_INTERRUPT4)
{
if(bit_is_clear(PINE,3))
{
left_clicks++;
}
else
{
left_clicks--;
}
}

ISR (SIG_INTERRUPT5)
{
if(bit_is_clear(PINC,1))
{
right_clicks++;
}
else
{
right_clicks--;
}
}

void main(void)
{
cbi(DDRC, PC1); //right dir, set for input
cbi(DDRE, PE5); //right clock, set for input
cbi(DDRE, PE4); //left clock, set for input
cbi(DDRE, PE3); //left dir, set for input
EICRB = 0b00100010; //sets pind 4 and 6 to interrupt on falling edge
EIMSK=0b00110000; //enables interrupts on 4 and 6
sei ();
while(1)
{
current_time = get_timer0_overflow()/WHEEL_UPDATE;
if(current_time >= 1)
{
rprintf("Left:%d Right:%d\n",left_clicks,right_clicks); //prints left and right click total every second
reset_timer0();
}
}

}


This works perfectly for the left wheel but some odd behavior is generated for the right wheel. If the left wheel is rolled backwards so it goes negative then right_clicks is set to -1. If the left wheel is rolled back to positive clicks then right_clicks is set to 0. Rolling the right wheel has no effect.

If I swap where the sensors are plugged in then the right wheel works (it changes left clicks). So its not a sensor hardware problem. Its the software or a problem with the Axon.

Any help would really be appreciated, this is driving me crazy.

Resilient
03-17-2009, 01:37 PM
Ok, I am unhappy now.

Why does:
rprintf("Left:%d Right:%d\n",left_clicks,right_clicks);

not actually print out left_clicks and right_clicks? It only prints left_clicks and some other variable that is either 0 or -1 where right_clicks should go...

I put in this code:

rprintf("Right:%d\n",right_clicks);
rprintf("Left:%d\n",left_clicks);
And everything works brilliantly.

metaform3d
03-21-2009, 03:02 PM
Why does:
rprintf("Left:%d Right:%d\n",left_clicks,right_clicks); not actually print out left_clicks and right_clicks? It only prints left_clicks and some other variable that is either 0 or -1 where right_clicks should go...Might be the size of the data type. printf() has to infer the size of the data pushed on the stack from the format string, so if you use the wrong formats it'll look at the wrong part of the stack. You should check your docs and see if "%d" can handle an int32_t. You might need "%ld" instead.

Resilient
03-21-2009, 06:51 PM
Might be the size of the data type. printf() has to infer the size of the data pushed on the stack from the format string, so if you use the wrong formats it'll look at the wrong part of the stack. You should check your docs and see if "%d" can handle an int32_t. You might need "%ld" instead.

Thanks for the idea. I wish the AVR documentation was better, but there just inst much info on the rprintf function. I know %ld is not a possible option though. So it may just be a limitation of the function.

societyofrobots
03-22-2009, 07:35 AM
I call this cross forum double posting :veryhappy:

http://www.societyofrobots.com/robotforum/index.php?topic=7370.msg56801#msg56801

Resilient
03-22-2009, 03:44 PM
I call this cross forum double posting :veryhappy:

http://www.societyofrobots.com/robotforum/index.php?topic=7370.msg56801#msg56801


Damn I was caught! :tongue:

metaform3d
03-22-2009, 07:46 PM
Thanks for the idea. I wish the AVR documentation was better, but there just inst much info on the rprintf function. I know %ld is not a possible option though. So it may just be a limitation of the function.You could try:

rprintf("Left:%d Right:%d\n", (int)left_clicks, (int)right_clicks);