Вот еще. bin2bcd32 - преобразование bin-packed bcd (num_bytes - количество байтов на выходе), ilcd_ks066_pul - вывод беззнакового числа data в память по адресу buf, options - параметры вывода (биты 0..2 - количество знаков-1, биты 4..6 - положение точки, считая справа, бит 7=1 для преобразования bin-bcd, бит 3=1 для удаления незначащих нулей. ilcd_ks066_point_char - используемый символ точки. ilcd_ks066_psl - вывод знакового числа с теми же параметрами.
Код
uint32_t bin2bcd32(uint32_t num, uint8_t num_bytes) /**< \brief Convert uint32_t num to packed BCD using num_bytes of number - universal \ingroup ul_math */
{uint32_t ul1=0; /*result*/
uint8_t uc1;
for (uc1=(4-num_bytes);uc1;uc1--) num<<=8; /*adjust input bytes*/
for (uc1=(num_bytes<<3);uc1;uc1--) /*bit shift loop*/
{uint8_t uc2,uc3;
/*BCD nibbles correction*/
ul1+=0x33333333;
for (uc3=4;uc3;uc3--)
{uc2=(uint8_t)(ul1>>24);
if (!(uc2&0x08)) uc2-=0x03;
if (!(uc2&0x80)) uc2-=0x30;
ul1=ul1<<8; ul1|=uc2;
}
/*shift next bit of input and result*/
ul1<<=1;
if ((num>>24)&0x80) ul1|=1;
num<<=1;
}
return(ul1);
}
static unsigned char ilcd_ks066_point_char='.'; /*point symbol,can be changed*/
void ilcd_ks066_pul(unsigned long data,void* ibuf,unsigned char options)
{unsigned char uc1,uc2,uc3,uc4;
uc1=(options&7)+1;
if (options&0x80)
{unsigned char uc5;
uc5=(uc1+1)>>1;
if (uc5>4) uc5=4;
if (uc5==0) uc5=1;
data=conv_bin2bcd(data,uc5);
}
uc3=((options>>4)&7);
uc2=uc1-uc3;
if (uc3) uc2++;
while (uc1)
{if ((options&8)&&(data==0)&&(uc1<uc2)) *(unsigned char *)ibuf=' ';
else
{uc4=(data&0xf)+'0';
if (uc4>'9') uc4+=7;
if ((uc1==uc2)&&(uc3))
{*(unsigned char *)ibuf=ilcd_ks066_point_char;
ibuf--;
}
*(unsigned char *)ibuf=uc4;
}
uc1--; data>>=4; ibuf--;
}
}
void ilcd_ks066_psl(signed long data,void *ibuf,unsigned char options)
{if (data<0)
{ilcd_ks066_pul(-data,ibuf,options);
*(unsigned char*)(ibuf-(options&7))='-';
} else ilcd_ks066_pul(data,ibuf,options);
}