On Fri, Mar 06, 2009 at 09:41:30AM -0500, David VanHorn wrote:
> I am a bit puzzled. (and new to working in C)
>
> I have an array of longs that I need to feed out my Uart.
> Obviously the uart can't take more than a char.
>
> The code below shows some of the ideas I've tried, but in all cases,
> the ASM shows me that the pointer will be bumped four bytes, not one.
As Graham suggests you can override the compiler using casts but I
suggest a union would be cleaner. I use something like this quite a lot:
typedef union {
struct {
uint8_t, a, b, c, d;
} u8;
struct {
// verify if this should be "dc, ba" or something
// so that it makes sense with u8 above
uint16_t ab, cd;
}
uint32_t u32;
} UNION32_t;
In your case you might do this:
union {
uint8_t u8[ PROC_DATA_SIZE * sizeof(uint32_t)];
uint32_t u32[PROC_DATA_SIZE];
} Proc_Data;
Then when you want the long version use Proc_Data.u32[] and when you
want it byte at a time use Proc_Data.u8[]. This doesn't take any extra
code space or CPU cycles.
The .u32 and .u8 at the ends are a lot cleaner than forcing casts with
pointers.
Another way to do this is to make a putsc( void *vp, uint8_t count )
routine that takes a pointer and byte count to be sent out the UART. In
the putsc() routine you will have to cast the void * to uint8_t in order
to index or do pointer arithmetic. The void * in the declaration will
mute any complaints about the type of pointer you are passing. Maybe
something like this:
uint8_t
putsc( void *vp, uint8_t count )
{
uint8_t *cp = (uint8_t*)vp; // this might not allocate or cost anything
uint8_t i;
for( i = 0 ; i < count ; i++ ) {
SCI = cp[i];
...
}
return count;
}
--
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.Message
Re: [AVR-Chat] Array of Long
2009-03-06 by David Kelly
Attachments
- No local attachments were found for this message.