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 » questions on overflow

February 03, 2012
by dvdsnyd
dvdsnyd's Avatar

Hi all, I have a question regarding overflows moreover, I have been looking at the real time clock example. would the program overflow because it is reading the values from memory, because it its volatile? So therefore there is only so much memory that it has before it runs out. Also, if the program were to be left running until it overflows...What would that look like on the LCD? would it just stop? Is there a way to avoid overflowing? How does one come up with the approximation of 248 days? I have posted the comments in the real time clock tutorial on the site. I can post the rest of it if it is necessary. Thanks a lot for all the help.

  // the_time will store the elapsed time
  // in hundredths of a second.
  // (100 = 1 second)
  // 
  // note that this will overflow in approximately 248 days!
  //
  // This variable is marked "volatile" because it is modified
  // by an interrupt handler.  Without the "volatile" marking,
  // the compiler might just assume that it doesn't change in 
  // the flow of any given function (if the compiler doesn't
  // see any code in that function modifying it -- sounds 
  // reasonable, normally!). 
  //
  // But with "volatile", it will always read it from memory 
  // instead of making that assumption.
   volatile int32_t the_time;
February 03, 2012
by Rick_S
Rick_S's Avatar

The reason that will overflow in approximately 248 days if it isn't reset is because the_time is defined as an int32_t datatype. Being signed, that means a max of 31 bits of data plus 1 for the sign.

Therefore the largest number that can be recorded in the_time is binary 0111 1111 1111 1111 1111 1111 1111 1111 or +/- 2,147,483,647 decimal. If you calculate the number of hundreths of a second in 248 days, you get (248 * 24 * 60 * 60 * 100) = 2,142,720,000. The overflow simply means the variable has reached it's limit. If you try to store something larger than that, the sign bit would toggle and the numbers would start over at negative. I'm not sure what would happen if you tried to store beyond double that... It may freeze, it may just ignore the extra data... but that would be a long time of the program running.

Rick

February 03, 2012
by pcbolt
pcbolt's Avatar

@dvdsynd

I actually found out what happens by mistake. I used an 8-bit unsigned variable and incremented it past the 255 (dec), 0xFF (hex) "limit". All it did was count up 253, 254, 255...then 0, 1, 2, 3 etc. The overflow flag will be set in the processor when this happens, but if nothing in your code checks it, nothing else happens. With signed numbers it's the way Rick describes. For an 8-bit signed integer, it would count up 125, 126, 127 then -128, -127, -126 etc. The program won't fill up any other memory than what is allocated for the variable (i.e. 8-bits, 16-bits, 32-bits etc).

Hope it helps.

February 03, 2012
by dvdsnyd
dvdsnyd's Avatar

Thanks guys! Both of your answers make sense. I appreciate the quick responses! David

Post a Reply

Please log in to post a reply.

Did you know that a piezoelectric buzzer can be used to play music with your microcontroller? Learn more...