PORTS Forum
Ports and Interfaces => Serial Ports => Topic started by: eightbits on April 29, 2012, 12:58:12 pm
-
Hello to all, (newbee)
I recently purchased the 2nd edition of the book and going through the chapter
13 , and studying the "Example Protocol". I was having some lack of understanding
in the "Responding to polls" section starting at page 291.
I don't have/use the PIC devices and I am using the Atmel AVR micros instead. Most of
the PIC (C18) code is not a problem, but I am curious about about a couple of listings.
On page 300, the function
void byte_to-ascii(unsigned char value_to_convert, value[])
and at the last two statements of this function:
converted_value[0] = upper_nibble;
converted_value[1] = lower_nibble;
The var's "converted_value[]" do not seem to be used elsewhere in the program (?).
I do see that the function that calls this routine passes the argument's
byte_to_ascii_hex(PORTB, &command_response[2]);
page 314.
So, i don't understand the use of &command_response[2]
I think that is passing the address of command_response[2], which is element 2 of
that array.
I am not clear on the relationship of the passed argument to converted_val[0].
I am hoping to get some clarification to this.
I will add that I plan on using serial receive interrupts instead of the while loop
that calls the serial_communications() from the main.c endless loop.
Thanks in advance to any clarification and suggestions.
-
byte_to-ascii_hex is a routine that other routines call call. The calling routine passes value_to_convert. The ASCII hex characters are in converted_value[] (not value[]) when the function returns.
This:
byte_to_ascii_hex(PORTB, &command_response[2]);
assumes the converted value will go in elements 2 and 3 of the array.
See Message Format on pages 283-4.
Jan
-
Yes, I see this is the function call that calls the byte_to_ascii_hex function.
But, the actual function is:
byte_to_ascii_hex( char value_to_convert, char converted_value[]);
This is my question. How is the variable converted_value[0] and converted_value[1] as in the
listing, associated with &command_response[2] ?
I am thinking that passing &command_response[2] and then the byte_to_ascii_hex function accepts that argument, (&command_response[2]), as the base address of converted_value[] ?.
So, converted_value[0] is also command_response[2]
and converted_value[1] is also command_response[3] (?)
I have not used that procedure in my limited C programming in the past.
Also, I am using a compiler for the Atmel AVR devices, but no problem in
using the C18 code ( where applicable) is still just 'C'.
Thanks for your assistance.
-
When you call the routine, you pass it the starting address where you want to store the two values. It doesn't matter what variable name the calling routine uses. All that matters is the address value that is passed.
The routine itself accepts whatever that address is and stores the values there, where the calling routine can access them when the called routine returns.
Jan
-
OK. That is as I thought. I have not entered the test code using the compiler for
the AVR, but I should be able to continue on.
Once again, thanks for the verification.