[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] possible compiler bug?

From: E. Weddington
Subject: Re: [avr-gcc-list] possible compiler bug?
Date: Wed, 28 Aug 2002 15:38:19 -0600

On 28 Aug 2002 at 15:44, Brian Dean wrote:

> Hi,
> I think there may be a bug in the code generation for writing 16 bit
> values via a pointer.  Consider the following code:
>  #include <inttypes.h>
>  #include <io.h>
>  void set_pwm(volatile uint16_t * pwmreg, uint16_t pwm)
>  {
>    *pwmreg = pwm;
>  }
>  int main(void)
>  {
>    volatile uint16_t * pwmreg, pwmval;
>    pwmval = 512;
>    pwmreg = (uint16_t *)_SFR_ADDR(OCR1AL);
>    set_pwm(pwmreg, pwmval);
>  }
> The generated assembler (below) appears to load the low byte first and
> then the high byte.  However, the AVR data sheets state that when
> accessing 16 bit registers such as the output compare registers, that
> the high byte must be loaded first followed by the low byte.  I had an
> elusive bug in a recent project that turned out to be due to this.
> After I forced the high byte be written first, followed by the low
> byte, my problems disappeared.

This is not a bug.

Note that not all 16-bit accesses require this method. See in the 
datasheet: 16-bit Timer/Counter ...> Accessing 16 bit Registers.

This applies to the 16-bit Timer/Counter registers which are the (at 
least for the ATmega128) TCNTn, OCRnA/B/C, ICRn registers.

The datasheet says: "To do a 16-bit write, the high byte must be 
written before the low byte. To do a 16-bit read, the low byte must 
be read before the high byte."

The paragraph before that says: "Not all 16-bit accesses use the 
Temporary Register for the high byte. Reading the OCRnA/B/C 16-bit 
registers does not involve using the Temporary Register.

The solution is to write your own functions / macros that handle both 
read and write access to these particular registers, to handle 
writing in the correct order (high, then low) and reading in the 
correct order (low, then high).

avr-gcc-list at http://avr1.org

reply via email to

[Prev in Thread] Current Thread [Next in Thread]