CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

BCD To Binary

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pdl



Joined: 01 Jun 2005
Posts: 45
Location: UK

View user's profile Send private message

BCD To Binary
PostPosted: Thu Jun 02, 2005 3:31 am     Reply with quote

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







PostPosted: Thu Jun 02, 2005 6:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 02, 2005 10:48 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 02, 2005 11:01 am     Reply with quote

This thread has bcd conversion functions instead of macros, if that's
easier for you to understand.
http://www.ccsinfo.com/forum/viewtopic.php?t=18830&highlight=bin2bcd+bcd2bin
pdl



Joined: 01 Jun 2005
Posts: 45
Location: UK

View user's profile Send private message

PostPosted: Thu Jun 02, 2005 11:15 am     Reply with quote

totaly lost


Pete
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 02, 2005 11:34 am     Reply with quote

OK. I think this thread will help. It doesn't do the BCD part, but
I think you need to get the basic RS232 i/o working first.
http://www.ccsinfo.com/forum/viewtopic.php?t=17846&highlight=rs232+write+port
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Thu Jun 02, 2005 1:42 pm     Reply with quote

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







PostPosted: Thu Jun 02, 2005 2:42 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 02, 2005 5:02 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group