在线情况
楼主
  • 头像
  • 级别
  • 门派
  • 职务总版主
  • 声望+9
  • 财富5
  • 积分3065
  • 经验390701
  • 文章6744
  • 注册2006-03-07
十进制转bcd三种算法
摘自:单片机坐标
作者:鞠春阳
[CODE]unsigned char Hundreds,Tens,Ones;
void DecimalToBcdAscii(signed short DecimalValue)
{
    Hundreds = 0;
// Initialize BCD values
   Tens = 0;
   Ones = 0;
 
   Hundreds:
// Hundreds
     DecimalValue = DecimalValue - 100;
       if (DecimalValue < 0)  
       {
          goto Tens1;
          }
       Hundreds = Hundreds + 1; // Increment
Hundreds count
       goto Hundreds;  
   Tens1:
// Tens
     DecimalValue = DecimalValue + 100;
   Tens2:
       DecimalValue = DecimalValue - 10;
       if (DecimalValue < 0)  
       {
         goto Ones1;
         }
       Tens = Tens + 1; //
Increment Tens count
       goto Tens2;    
   Ones1:
// Ones
     DecimalValue = DecimalValue + 10;
   Ones2:
       DecimalValue = DecimalValue - 1;
       if (DecimalValue < 0)  
       {
          goto AddAsciiOffset;
          }
       Ones = Ones + 1; //
Increment Ones count
       goto Ones2;    
    AddAsciiOffset:
// Add ASCII offset
       Hundreds = Hundreds + 48;
       Tens = Tens + 48;
       Ones = Ones + 48;
}
#include<reg51.h>
#include<stdio.h>
main()
{//clk=12Mhz
int i;
//算法1 耗时0.25s
for (i=0;i<1000;i++)
DecimalToBcdAscii(i);
//算法2 耗时0.42s
for (i=0;i<1000;i++)
{int t;
 Hundreds=i/100;
 t=i%100;
 Tens=t/10;
 Ones=i%10;
 }
Hundreds = Hundreds + 48;
Tens = Tens + 48;
Ones = Ones + 48;
//算法3 耗时1.5s
for (i=0;i<1000;i++)
{unsigned char buf[5];
 sprintf(buf,"%3d",i);
 }
} [/CODE]
[ 此帖最后由DC在2012-11-8 0:32:41从 电子通识 转移过来 ]
[COLOR=#0000ff]欢迎发贴分享设计心得、开源DIY...[/COLOR]
在线情况
2
  • 头像
  • 级别
    • 积分11
    • 经验295
    • 文章36
    • 注册2007-02-12
    我还没用到啊!很感谢版主.
    微控网感谢您的参与
    在线情况
    3
    • 头像
    • 级别
      • 积分4
      • 经验285
      • 文章9
      • 注册2007-03-14
      微控网感谢您的参与
      Powered by LeadBBS 9.2 .
      Page created in 0.1880 seconds with 6 queries.