[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] Time-efficient read of high byte in two-byte variable
From: |
E. Weddington |
Subject: |
Re: [avr-gcc-list] Time-efficient read of high byte in two-byte variable |
Date: |
Thu, 08 May 2003 16:00:02 -0600 |
On 8 May 2003 at 14:44, Wallace White wrote:
> What's a good, quick way to read the high byte of a two-byte variable
> in C?
>
> I am sending an int out the UART, unformatted. Here's what my tries so
> far have compiled into (my 16-bit variable is encPosDetentsNow):
>
> 872: UDR = (unsigned char) (encPosDetentsNow >> 8);
> +00000517: 91600079 LDS R22,0x0079 Load direct from
> +data
> space
> +00000519: 9170007A LDS R23,0x007A Load direct from
> +data
> space
> +0000051B: 2F87 MOV R24,R23 Copy register
> +0000051C: 2799 CLR R25 Exclusive OR
> +0000051D: FD87 SBRC R24,7 Skip if bit in
> register cleared
> +0000051E: 959A DEC R25 Decrement
> +0000051F: B98C OUT 0x0C,R24 Out to I/O location
>
> The following is a little shorter:
>
> 871: UDR = (((unsigned int) encPosDetentsNow) & 0xFF00) >> 8;
> +00000517: 91600079 LDS R22,0x0079 Load direct from
> +data
> space
> +00000519: 9170007A LDS R23,0x007A Load direct from
> +data
> space
> +0000051B: 2F87 MOV R24,R23 Copy register
> +0000051C: 2799 CLR R25 Exclusive OR
> +0000051D: B98C OUT 0x0C,R24 Out to I/O location
>
> What I'd really like to get would be just
> LDS R23,0x007A ; load the high byte
> OUT 0x0C,R24 ; and output it to UDR
> without going to inline assembly.
>
> This is using the current WinAVR with -O3.
>
There's been a lot of discussion and it seems the most efficient way
is to use a union.
#include <inttypes.h>
typedef struct
{
unsigned char lo;
unsigned char hi;
} hilow_t;
typedef union
{
uint16_t word;
hilow_t byte;
} mytype ;
mytype encPosDetentsNow;
....
{
....
encPosDetentsNow.word = 0xFFFF;
UDR = encPosDetentsNow.byte.hi;
....
}
See what you get with this.
Eric
RE: [avr-gcc-list] Time-efficient read of high byte in two-byte variable, Ralph Mason, 2003/05/08