Yahoo Groups archive

AVR-Chat

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

Message

Re: [AVR-Chat] Re: Array of Long

2009-03-06 by David VanHorn

On Fri, Mar 6, 2009 at 9:55 AM, Graham Davies <Yahoo37849@ecrostech.com> wrote:
> --- In AVR-Chat@yahoogroups.com, David VanHorn <microbrix@...> wrote:
>
>> I have an array of longs that
>> I need to feed out my Uart ...
>
> David, in the statement:
> UDR = &(Proc_Data[Proc_Data_Out_Index])+i;
> on the right hand side you take the address of an array element and add i.  What is puzzling you is why the result jumps up four at a time.  The reason is that the address of the array element is a typed pointer.  In other words, the compiler knows that it's the address of a long and will treat it as such.  For each "one" you add to the value of this pointer, the compiler will move forward four bytes, so as to point to the next long object in memory.

I  understand that part.  The compiler is treating the array as I defined it.
But obviously I can't feed a long to the UART...


> A better solution would be to cast the address of the entire array to be a pointer to bytes.

? :)


>It is then clearer in the program text what you are trying to do, which is to treat an array of longs as an array of bytes for some specific purpose.  Remember to multiply the number of longs in the array by sizeof ( long ) to get the number of bytes.  Or, if the length of the array is known at compile time and you want to send it all, just use sizeof ( Proc_Data ).

The array size is known at compile time, but not the number of
elements in the array.
A processing routine puts data into this array, two longs per
millisec.  I have to transmit the data via the uart.
All the processing happens during the T0 ISR, so it's ok if I'm
sitting here waiting for the uart. Nothing else is happening in the
machine, so I don't expect any timing issues.

I'm just frustrated because what I want would be dead easy in ASM, but
at least half of the point of the exercise is to learn C.

I'd really like to see a "C cookbook", not a language manual, and not
a project, but a collection of commonly used functions.

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.