[chbot] AVR external interrupt and Makefile example

Richard Jones rjtp at ihug.co.nz
Sat Nov 14 21:11:58 GMT 2009


Many thanks Col,

You located the 'bug' I referred to in my original post.
11 improvement opportunities to go!
However I was unaware of the ATOMIC_BLOCK c99 feature and have to confess
its portability is a real benefit compared to adding sei and cli
instructions.

Richard

On Thu, 12 Nov 2009 17:40:15 +1300, Col <colp at paradise.net.nz> wrote:
> 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.
> 
> _______________________________________________
> Chchrobotics mailing list Chchrobotics at lists.linuxnut.co.nz
> http://lists.ourshack.com/mailman/listinfo/chchrobotics
> Mail Archives: http://lists.ourshack.com/pipermail/chchrobotics/
> Web site: http://kiwibots.org
> Meeting Dates:
> Tue 15 December 2009 6.30pm
> Tue 16 February 2010 6.30pm
> When replying, please edit your Subject line to reflect new content.



More information about the Chchrobotics mailing list