The compiler ate my code!

by Ivan Hamilton 3/25/2008 11:13:00 AM

I've been playing with my little side project, and that involves Atmel AVR microcontrollers. I love the Atmel AVR family because they're simple and compiler friendly. Friendly enough that even GCC supports them... under Windows too (WinAVR). I'm writing some code to generate PWM signals to drive brushed DC electric motors. Now, these great little chips often support built-in PWM generation. In fact, the particular model I'm using supports six hardware PWM channels. Call me a crazy, but I decided to ignore this perfectly good hardware feature and flip the output pins on and off in code.

In doing so, I've got a set of raw output variables which I convert to a PWM structure. When I call the conversion function in my main loop, it works fine. When I call the conversion function from a interrupt handler (connected to a timer), it doesn't update. WTF?

It's the little things in life... hours spent scratching your head about a simple code change. "Hmmm... works here... not here. Okay... Interrupt handler is definitely called."  A trip to AVR Studio's emulator and setting break points on the lines where raw outputs were changed was the key. Quite simply... I couldn't.

When the call that uses these output variables was only within an interrupt handler (and not called from the main body), GCC would optimise away setting (and even calculating) the output variables. My structure was never updated because GCC couldn't see where it was ever used. A similar (non-microcontroller) example is given at over at LinuxDevices.

Adding the volatile keyword to my variable declaration fixed the issue (yay!). Is there a performance impact? Certainly. Enough to worry about? Not yet.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Pragmatism | CNC

Related posts

Comments

8/29/2009 12:15:59 AM

I was actually searching something different on Google, but found this post and i'm glad i did, thanks, this good read. :)

Jack

8/29/2009 10:47:24 AM

This is a great blog, usually i don't post comments on blogs but I would like to say that this post really forced me to do so!

hcg injections

8/31/2009 4:54:17 AM

I can only say thanks, this post has actually helped me with something ;)

where to buy salvia divinorum

11/25/2009 11:26:38 PM

What a facinating blog. I've bookmarked it and added your feed to my RSS Reader

Gail Honeyman

11/27/2009 12:28:55 AM

You got a really useful blog. Thanks for sharing your thoughts. Outstanding, Brady Prachett @ Gummistövlar

Gummistövlar

11/27/2009 12:40:26 AM

Thanks for supporting this idea as it gives answers to all possible questions.

flash development

11/27/2009 12:48:43 AM

I always feel great when I find the blog useful for and it takes place in my collection.

autism resources

Add comment


 

biuquote
Loading



Powered by BlogEngine.NET 1.3.1.30
Original theme by Mads Kristensen

About the author

Name of author Ivan Hamilton
"My inner nerd can beat up your inner nerd."

E-mail me Send mail

Adsense

Calendar

<<  April 2018  >>
MoTuWeThFrSaSu
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

View posts in large calendar

Recent comments

Tags

None

    Entropy

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2018

    Sign in