Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] What is your suggestion?

2006-12-07 by David Kelly

On Thu, Dec 07, 2006 at 03:22:42PM -0000, Reza wrote:
> 
> 3- in C when you want to access a variable in both main code and 
> interrupt handlers, you have to declare it as volatile to avoid 
> memory access optimization using registers. but if variable be of 
> type int, or something larger than one byte in size, bcz access made 
> in more than one instruction you may get invalid results. consider 
> this:
> 
> volatile short n;
> 
> SIGNAL(...)
> {
>    n++;
> }
> 
> main()
> {
>    int t = n;
>    ....
> }
> 
> in main() function assembler code looks like this:
> 
>     lds r24,n
>     lds r25,n+1
> 
> if an interrupt happens between two lds instructions and if <n>
> have a value of 255 before first lds, you will get 511. in these cases
> i may use cli/sei to block interrupts, but is there another way to 
> notify the compiler to generate code for me? accessing to data types 
> other than byte is not atomic.

All the compiler could do is wrap the variable modifications in cli/sei
exactly the same as you have to do manually. Worse actually as the
compiler doesn't know whether interrupts are enabled so it has to push,
clear, pull, as is used in many library routines.

Might as well manually wrap the critical sections with cli/sei.

-- 
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.