| Author |
Message |
sam_manchali
Joined: 14 Jun 2003 Posts: 105 Helped: 3 Location: sharjah
|
27 Nov 2003 11:01 msp430 bcd |
|
|
|
|
hi,
can someone help me by giving a method or code( ) in c for converting a hex o/p of the 12bit ADC on the msp to a decimal number ??
i need to do some mul. and div etc. on that data before displaying it on the LCD.
any help will be appreciated.
thank you
sam
|
|
| Back to top |
|
 |
arturt134
Joined: 01 Jan 1970 Posts: 141 Helped: 1 Location: Poland
|
27 Nov 2003 12:38 msp430 adc |
|
|
|
|
What do you mean by "decimal number"? Is this BCD number?
In example, if you have 0x12 hexadecimally you want to change it to
BCD number: 0x18?
0x18 is decimal representation of 0x12 hexadecimal number.
C routine for that conversion (hexadecimal must be less than 0x64):
unsigned char hex2bcd(unsigned char hex)
{
unsigned char bcd;
bcd = ((hex / 10) << 4) + (hex % 10);
return(bcd);
}//hex2bcd
|
|
| Back to top |
|
 |
Google AdSense

|
27 Nov 2003 12:38 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
sam_manchali
Joined: 14 Jun 2003 Posts: 105 Helped: 3 Location: sharjah
|
28 Nov 2003 4:38 binary to ascii on msp430 |
|
|
|
|
hi,
thanks for the reply.
like i had told before , i need it for the adc. so
it should be capable of taking from 0x00 to 0xFFF. i think ur routine is only for 0-100 decimal.
bye
thanks anyway, but i still need it so i am trying to buold one from the pic routine i have.
bye
sam
|
|
| Back to top |
|
 |
GeorgeM
Joined: 19 Nov 2003 Posts: 37
|
28 Nov 2003 14:34 binary-to-bcd msp430 |
|
|
|
|
Hi,
To convert 12 bit number to decimal or ASCII try the code below
void hex2dec(unsigned adchex,unsigned value)
{
unsigned value[4];
for(i=0;i<4;i++)
{
value[i]=adchex%10;
// value[i]=adchex%10+0x30; //for ASCII presentation
adchex=adchex/10;
}
}
Hope it helps.
Regards,
George
|
|
| Back to top |
|
 |
sam_manchali
Joined: 14 Jun 2003 Posts: 105 Helped: 3 Location: sharjah
|
02 Dec 2003 9:34 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>= 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
|
|
| Back to top |
|
 |
GeorgeM
Joined: 19 Nov 2003 Posts: 37
|
02 Dec 2003 15:13 hexadecimal to decimal in c |
|
|
|
|
Hi,
Yes, I gave you decimal conversion, not hex presentation. You should use the program below (in your terms).
void putdecLCD(int binary)
{
for(i=0;i<4;i++)
{
lcd_data[3-i]=binary%10;
binary=binary/10;
}
}
Lets check it for your example
binary=3908;
1) i=0; lcd_data[3]=3908%10=8; binary=3908/10=390;
2) i=1; lcd_data[2]=390%10=0; binary=3908/10=39;
3) i=2; lcd_data[1]=39%10=9; binary=39/10=3;
4) i=3; lcd_data[0]=3%10=3; binary=3/10=0;
You can see that lcd_data array is correct.
Regards,
George
|
|
| Back to top |
|
 |
sam_manchali
Joined: 14 Jun 2003 Posts: 105 Helped: 3 Location: sharjah
|
03 Dec 2003 4:49 converting 12 bit counts to decimals |
|
|
|
|
hi georgeM,
u dont seem to understand what i want .
i dont want to split 3980 to 3,9,8,0( by simple % operation)and display
but i want to convert a hexadecimal number into BCD , like on the windows acessories , calculator: type some hex num and goto decimal format, it gets converted. i want to do the same on the msp.
i hope uhave got it!!
now u see
|
|
| Back to top |
|
 |
GeorgeM
Joined: 19 Nov 2003 Posts: 37
|
03 Dec 2003 10:56 12-bit unsigned binary to decimal |
|
|
|
|
int BCDcode;
BCDcode=(lcd3<<12)+(lcd2<<8)+(lcd1<<4)+lcd0;
|
|
| Back to top |
|
 |