RE: [AVR-Chat] Array of Long
2009-03-06 by subscriptions@aeolusdevelopment.com
David VanHorn Wrote
>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.
>
>What's the best way to get this data to my Uart?
>
>Yes, I CAN sit and spin for the UDR to be ready.
>
>void Do_Serial_Output(void)
>{
> unsigned char i = 0;
> unsigned char t = 0;
> if (0 < Proc_Data_Size) { // If there's data to send..
> while ( i<4 ) {
> while (!(UCSRA & (1<< UDRE))) {}
> //memcpy(&t,&(Proc_Data[Proc_Data_Out_Index])+i,1);
> //UDR = t;
> //UDR =
(char)(*((char*)(&(Proc_Data[Proc_Data_Out_Index])+i)));
> UDR = &(Proc_Data[Proc_Data_Out_Index])+i;
> ++i;
> }
>
> Proc_Data_Size--;
> Proc_Data_Out_Index++;
> if (Proc_Samples < Proc_Data_Out_Index) Proc_Data_Out_Index =
>0; // Handle wrap
> }
>}
Assuming you are sending the long out as binary, something like the
following is what I would use (serial_out is left out, I'm assuming the HW
control you have a handle on)
void put_bytes( unsigned char * const buf, int num)
{
int i;
for( i=0; i < num; i++){
serial_out( *buf);
buf++;
}
}
void put_long_array( const long arr[], int num)
{
int i;
for( i=0; i < num; i++) {
put_bytes( (unsigned char *)&arr[i], sizeof(arr[0]));
}
}
Obviously you can combine the routines but this has the advantage of
clarity and put_bytes can be re-used elsewhere. unsigned char is used to
avoid any sign issues. Even if char is unsigned it's wise to be explicit so
that you are used to being clear about what you want and expect, that way
when you do use a system with chars that are signed your code still
functions correctly. (Never assume the signedness of char, even when it's
documented) const is used to show you are not changing the contents of the
arguments. Compilers, static analyser and humans can all make use of that
information.
Finally I (almost) always use array notation when accessing arrays. Again
for clarity.
Note this code is unchecked, just typed into an e-mail edit window so ....
Robert
--------------------------------------------------------------------
mail2web.com - Microsoft® Exchange solutions from a leading provider -
http://link.mail2web.com/Business/Exchange