Jump to content

Recommended Posts

Posted (edited)

Hi there gents

 

I managed to find this function which converts a 2 byte hex to 2 byte BCD, but since 2 bytes of hex can hold a decimal value 0 to 255, it is limited to a hex value between 0x00 and 0x63 (0 to 99).

 

uint8_t hex2bcd (uint8_t x)
{
uint8_t y;
y = (x / 10) << 4;
y = y | (x % 10);
return (y);
}
I need some help adapting this to produce a 12 bit (can use a 16bit variable) BCD output.
I have developed my own code, but it is very very long and highly inefficient, so I am looking to use the simplest code and/or fasted code possible.
CK

 

edit: came up with this, which doesn't seem to work.

 

uint16_t hex2bcd12bit (uint8_t x)

{

uint16_t B1;
uint16_t B2;
uint16_t B3;
B1 = x & 0x0F;
B2 = (x & 0xF0) >> 4;
B1 = B1 % 10;
B2 = B2 + B1 / 10;
B3 = B2/10;
B2 = B2 % 10;
B3 = B3<<8 + B2<<4 + B1;
return (B3);
}
Edited by CasualKilla
Posted (edited)
y = (x / 100) << 8; // will get 1xx-2xx


but x contain upper 1/2, and you have to get rid of.


x = x % 100; // modulo


Then the rest of original hex2bcd().


Result type will have to be changed.
Edited by Sensei

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.