hex2bcd c
hi,
what i wanted is not "hex representation" but hex to decimail conversion
ie 0xF44 (hex) = 3908(decimal)
here is the code for direct display on the built in LCD(msp430f435)
// binary to BCD file
void putdec_lcd(unsigned long binary)
{
unsigned char count=0;
union {
struct {
unsigned long low; //4 bytes
unsigned long high; //4 bytes
} lng;
unsigned char byte[8];
} cdec;
static unsigned char encountered_only_zeros;
signed char c;
unsigned char d;
//at least it prints '0' if it is zero, and doesnt skip all leading zeros!
if (binary==0) {
lcd_data[0]=lcd_data[1]=lcd_data[2]=lcd_data[3]=0;
return;
}
//implement the technique above
cdec.lng.low=binary;
cdec.lng.high=0; //12W / 2l
for (c=0;c<32;c++) {
//shift hi:lo <<1
d=cdec.byte[3];
cdec.lng.low<<=1;
cdec.lng.high<<=1;
if ( (d& 0x80)==0x80) cdec.byte[4]|=1;
for (d=4;d<7;d++) {
if ((cdec.byte[d]&0x0F)>=0x05) cdec.byte[d]+=0x3;
if ((cdec.byte[d]&0xF0)>=0x50) cdec.byte[d]+=0x30;
}
}
encountered_only_zeros=1; //for skipping leading zeros
for (c=15;c>=8;c--) {
d=cdec.byte[c>>1];
if ((c&1)==0) {
d&=0x0F;
}
else {
d&=0xF0;
d>>=4;
}
if (d>=8) d-=3;
if (d!=0) encountered_only_zeros=0;
if (encountered_only_zeros==0)
{lcd_data[count]=d;count++;}
}
if(count==3)
{
lcd_data[3]=lcd_data[2];
lcd_data[2]=lcd_data[1];
lcd_data[1]=lcd_data[0];
lcd_data[0]=0;
}
if(count==2)
{
lcd_data[3]=lcd_data[1];
lcd_data[2]=lcd_data[0];
lcd_data[1]=0;
lcd_data[0]=0;
}
if(count==1)
{
lcd_data[3]=lcd_data[0];
lcd_data[2]=0;
lcd_data[1]=0;
lcd_data[0]=0;
}
_NOP();
}
// bin to BCD ends here
this is the exact routine , with some changes of the one on
www.microchipc.com
if someone can give me an improvement, they are welcome.
thanks
sam