[chbot] AVR external interrupt and Makefile example

Col colp at paradise.net.nz
Thu Nov 12 04:40:15 GMT 2009


Hi Richard

In the interest of the group I would like to point out your supplied
code contains a race condition.

Basically the count4ms is 16 bit variable on an eight bit micro. If an
interrupt occurs 1/2 way through reading the variable you could get
unexpected results.

My suggestion is something like


#include <util/atomic.h>

ATOMIC_BLOCK( ATOMIC_FORCEON )
{
	temp = count4ms;
}
if ( temp >= 305 ) {


This is outlined in the libc user manual.
http://www.nongnu.org/avr-libc/user-manual/group__util__atomic.html


Cheers
Col.



More information about the Chchrobotics mailing list