Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

HiTech C BCD to ascii convert

Status
Not open for further replies.

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,936
Dear All

This is related to PIC 16f877a and HiTech C Compiler
Can I have the library function for BCD to ASCII convert

Thanks in advance
 

Packed or Unpacked BCD?

The algorithm is fairly simple:

The HEX equivalent of ASCII '0' is 30H, Therefore to convert a packed BCD to ASCII you must first unpack it and then add 30H to the unpacked BCD

Code:
Packed BCD
73H
0111 0011


Code:
Unpacked BCD
07H             03H
0000 0111    0000 0011


Code:
ASCII
37H              33 H
0011 0111     0011 0011


BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear All
Can any one please explain following codes step by step that I can not understand


Code:
unsigned short myBcd2Dec(unsigned short bcd){
 return ((bcd >> 4)*10+(bcd & 0x0F));
 }

Please help
 

Can any one please explain following codes step by step that I can not understand

Code:
unsigned short myBcd2Dec(unsigned short bcd){
 return ((bcd >> 4)*10+(bcd & 0x0F));
 }

The above routine takes a packed BCD and returns the equivalent value as an integral type.

I will assume an "unsigned short" has a size of one byte, although without knowing the exact compiler this is unknowable.

Code:
unsigned short BCD1 = 0x73;
unsigned short Result;

Result = myBcd2Dec(BCD1); // Result = 0x49 or 73

The BCD1 variable contains a Packed Binary Coded Decimal value of 73:

Code:
BCD1 = 0111   0011

The most significant four bits (nibble) contains the integer 7 and the least significant four bits (nibble) contains the integer 3.

It can be easily seen that the Packed BCD value represents the integer 73.

Let's consider the return statement:

Code:
((bcd >> 4)*10 + (bcd & 0x0F))

First:

Code:
bcd & 0x0F

Masks the lower nibble resulting in 0000 0011 or 3.

Second:

Code:
(bcd >> 4)*10

Right Shifts the upper nibble into the lower nibble resulting in 0000 0111 or 7, which is then multiplied by 10 resulting in 70, 0x46 or 0100 0110.

Finally:

Code:
((bcd >> 4)*10 [COLOR="#FF0000"]+[/COLOR] (bcd & 0x0F))

The two results 70 and 3 are added together resulting in 73, 0x49 or 0100 1001 which is the value returned by the routine.



Does the above discussion answer your question?

What are the requirements of your projects task? Are you dealing with Packed BCD or Unpacked BCD? What type format do you need the BCD converted?


BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Bigdog,
Thanks you so much for reply
The answer is excellent that what I expected,
My requirement is reading DS1307 RTC and put result to the LCD
really really great reply

Thanks
 

The following are simple routines which convert either the High Nibble or Low Nibble of a Packed BCD to an ASCII character:

Code:
unsigned char BCD2UpperCh(unsigned char bcd)
{
return ((bcd >> 4) + '0');
}

unsigned char BCD2LowerCh(unsigned char bcd)
{
return ((bcd & 0x0F) + '0');
}

The above routines use similar techniques as the routine you requested help understanding.

Adding the value of character '0' is equivalent to add 30 to the integer to convert it to ASCII.

BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Bigdog,
Thanks for reply


Code:
second=I2CRead();

The above code read RTC and store result in second

Now I need to convert BCD values in second to Ascii

Code:
bcdToascii(second);

above code call to BCD to Ascii convert sub program


Code:
unsigned char bcdToascii(unsigned char bcd){
return ((bcd>>4)+'0');

as per above code convert BCD to Ascii
I need to know where to return the result or any wrong with my program


Please help
 
Last edited:

You could use the previously posted routines along with a character array to build a string and send the string to LCD.

For Example:
Code:
unsigned char time[9];
unsigned char sec;
unsigned char min;
unsigned char hour;
...
unsigned char getSeconds();
unsigned char getMinutes();
unsigned char getHours();
...
...
...
sec = getSeconds();
min = getMinutes();
hour = getHours();
....
....
time[0] = BCD2UpperCh(hour);
time[1] = BCD2LowerCh(hour);
time[2] = ':';
time[3] = BCD2UpperCh(min);
time[4] = BCD2LowerCh(min);
time[5] = ':';
time[6] = BCD2UpperCh(sec);
time[7] = BCD2LowerCh(sec);
time[8] = '\0';

putStringLCD(time);


Does the above example help?

BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Bigdog
Thank you so much for reply

I managed it using this manner
Code:
void bcdToascii(unsigned char value){

	tmp = value;
	tmp = tmp & 0xf0;
	tmp = tmp >> 4;
	tmp = tmp | 0x30;
	[COLOR="#FF0000"]lcddata(tmp);[/COLOR]
	tmp = value;
	tmp = tmp & 0x0f;
	tmp = tmp | 0x30;
	[COLOR="#FF0000"]lcddata(tmp);[/COLOR]

any advice
 

Your essential carrying out the same method through the use of a global variable and writing directly to the LCD, which limits the flexibility of the routine.

I prefer implementing a more generic/general purpose routines making use of either function return values, like the example I previously posted or passing pointers to the routine.

You could also make use of a type structure containing the seconds, minutes and hours and using it to build the output string for the LCD.

BigDog
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top