NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Limitations to what an interrupt can do?

June 29, 2012
by steely600
steely600's Avatar

Im mucking around with a motor control project to learn about PWM and interrupts. In main() i have a while loop, and in that I have a switch statement switch(toggle). "toggle" is the variable used to control the statement, in each of the cases I have code to control the motor(case0: //no power, case1://medium power etc). To increment toggle I am using an external interrupt INT0 (pin PD2) but for some reason this interrupt will do everything but change the variable toggle. Is this some kind of limitation of interrupts? Or is to do with the location of the variable toggle? (the variable toggle is initilised in main(), switch(toggle) doesnt recognise it if its located outside main())

June 29, 2012
by steely600
steely600's Avatar

Ah I got it!

stupid volatile....

Sorry, maybe take this whole forum post down

June 29, 2012
by pcbolt
pcbolt's Avatar

@ steely600 -

The placement of where you declare the variable "toggle" is important. It should be declared above (and outside) of both main() and the interrupt routine. Also, it should be declared as "volatile" so the compiler doesn't optimize its functionality away. The code for the "Real Time Clock" project uses a variable called "the_time" in the exact way you describe. The interrupt changes the variable and main() displays the change.

June 30, 2012
by steely600
steely600's Avatar

Yeah you're right pcbolt, I did try use the "volatile" declaration first off but I created quite a few problems when compiling so I thought I wasnt using it in the right context. Only after reading a post from here did i learn its proper use. Now I can manipulate variables quite easily from the ISR. Now i need to properly debounce it....

June 30, 2012
by pcbolt
pcbolt's Avatar

There was a thread on this forum about debouncing HERE. There is a link there for a "Hack-a-Day" article that is very informative. I've used the code in the "alternate solution" paragraph of that article that works like a champ. Instead of using a pin change interrupt, it uses a timer and some interesting code.

July 01, 2012
by steely600
steely600's Avatar

Yep, debouncing is the next thing I need to look at. I still cant toggle through the switch cases easily enough. I didnt pay enough attention to it the first time round, I just thought a short delay_ms(XX) would solve it but it doesnt prevent multiple button presses, it just delays the code a little bit. ( I think thats what going on)

Post a Reply

Please log in to post a reply.

Did you know that NerdKits has been featured on Slashdot, Hack A Day, Hacked Gadgets, the MAKE blog, and other DIY-oriented websites? Learn more...