View previous topic :: View next topic |
Author |
Message |
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
BCD To Binary |
Posted: Thu Jun 02, 2005 3:31 am |
|
|
Hi guys and [spam] i am very new to C programming, come to think of it any programming.
i need a bit of code that takes BCD from the RS232 port and converts it in to BINARY and displays it on PORT B with 8 leds.
This needs to be simple for a newbie like me.
Before you anwser do a search for BCD i have.
is there any one that could help with this
many thanks
Pete |
|
|
Ttelmah Guest
|
|
Posted: Thu Jun 02, 2005 6:43 am |
|
|
A search for BCD here on the forum should have found routines I posted in the past. However here they are again:
Code: |
/
* used for BCD conversions if required */
const unsigned int conv[16] = {
0,10,20,30,40,50,60,70,80,90,90,90,90,90,90,90 };
/* BCD to raw conversion */
#define fromBCD(x) ((x & 0xf)+conv[(x>>4)])
/* BCD conversion sub */
unsigned int toBCD(unsigned int val)
{
int ctr;
for (ctr=0;ctr<10;ctr++)
{
if (val == conv[ctr])
return(ctr<<4);
else if (val < conv[ctr]) break;
}
--ctr;
return((val-conv[ctr])+(ctr<<4));
}
|
In your case, 'fromBCD' will return an int8 value, that can then simply be output to port B. So:
output_b(fromBCD(original_vaue));
Would output the conversion onto portB.
ToBCD, is the 'complimentary' function, if you need to go the other way.
Best Wishes |
|
|
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
|
Posted: Thu Jun 02, 2005 10:48 am |
|
|
Hi Ttelmah
thanks for replying to my post but i am even more lost now.
the pic recieves 00010001 this would equal 11 in dec or 0b in hex.
Do i use this bit of you code
/* BCD to raw conversion */
#define fromBCD(x) ((x & 0xf)+conv[(x>>4)])
and then do x=getc(); to get the BCD code from the rs232 port.
and then output_b(fromBCD(original_vaue));
were does the original_vaue come in to it
sorry to be a pain
Pete |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
|
Posted: Thu Jun 02, 2005 11:15 am |
|
|
totaly lost
Pete |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
|
Posted: Thu Jun 02, 2005 1:42 pm |
|
|
Code: | int x;
int temp;
x = getc();
temp = x >> 4; //get the top nibble
temp *= 0x0A; // multiply by 10 - high nibble tells how many 10's there are
x += temp; //add to lower nibble - total is how many 1's you have
output_b(x); |
That's pretty simple, right? (check for errors, I did not test). This is pretty much the same thing that Ttelmah did. He used an array that takes up very little RAM and I used the multiply that takes up ROM or Flash for the multiply function. _________________ -Matt |
|
|
Ttelmah Guest
|
|
Posted: Thu Jun 02, 2005 2:42 pm |
|
|
pdl wrote: | Hi Ttelmah
thanks for replying to my post but i am even more lost now.
the pic recieves 00010001 this would equal 11 in dec or 0b in hex.
Do i use this bit of you code
/* BCD to raw conversion */
#define fromBCD(x) ((x & 0xf)+conv[(x>>4)])
and then do x=getc(); to get the BCD code from the rs232 port.
and then output_b(fromBCD(original_vaue));
were does the original_vaue come in to it
sorry to be a pain
Pete |
You need the 'conv' array defined as well (code won't work without it).
then if you do:
Code: |
x=getc();
output_b(fromBCD(x));
//'x' is your 'original value'...
|
Port B will receive the binary value corresponding to the BCD value in 'X'.
Best Wishes |
|
|
pdl
Joined: 01 Jun 2005 Posts: 45 Location: UK
|
|
Posted: Thu Jun 02, 2005 5:02 pm |
|
|
Hi Guys
i would just like to say thankyou i have that part of my code working now.
here we go now i have to get this to work with the USB now
many thanks
Pete |
|
|
|