View previous topic :: View next topic |
Author |
Message |
panayig
Joined: 10 Mar 2004 Posts: 3
|
Please advise... (newbee question) |
Posted: Sun Nov 21, 2004 1:28 pm |
|
|
Hello everybody
I would like to have some help if its possible.
I have established an RS232 asynchronous communication of a PIC16877 with my PC.
I am sending the 'A' and 'F' from the PC. I get them at the PIC (data type int8) do the proper manipulation (UART receiving etc) and endup with data1='A' and data2='F' (I have verified that using LED testing).
Now my problem is this:
I want to convert 'A' and 'F' into 'AF' and then to 0xAF. The reason is to send 0xAF through SPI to another chip.
What I have thought is to turn both of them into hex (0x0A and 0x0F respectively) and then to use make16() to make 0x00AF and then to use make8() to take the LSB thus 0xAF and program it through SPI.
The problem with make16(), as I understand, is that it will not give me 0x00AF but 0x0A0F. Is that correct?
Thus if I use make8() I will be at the beginning again since I will get 0x0F as the LSB and not what I want.
Can you please advise me how to achieve what I want? |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Sun Nov 21, 2004 4:19 pm |
|
|
This is how I have done in the past:
Code: |
#include <stdlib.h>
char Str[5];
byte Result;
Str[0]='0';
Str[1]='x';
Str[2]=getch(); //Read the 'A'
Str[3]=getch(); //Read the 'F'
Str[4]=0; //Null character, Now Str="0xAF"
Result=atoi(Str); //Now result is 175, or 0xAF
|
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Nov 22, 2004 8:08 am |
|
|
Maybe you are looking for the swap() function, which swaps high and low nibbles of a byte? Check it out to see if it does what you want. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
panayig
Joined: 10 Mar 2004 Posts: 3
|
|
Posted: Wed Nov 24, 2004 11:59 am |
|
|
Guys thank you very much for your replies.
Haplo I have used your method and worked perfectly. Thank you.
Sherpadoug thank you for the time you 've taken to consider my question. I have looked at the swap() as you suggested but the code I produced using it is far bulkier than Haplos is so I will go with Haplos suggestion. |
|
|
|