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.

msp430 adc hex to decimal routine

Status
Not open for further replies.

sam_manchali

Member level 4
Member level 4
Joined
Jun 14, 2003
Messages
69
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,288
Location
sharjah
Visit site
Activity points
735
msp430 bcd

hi,
can someone help me by giving a method or code( :D ) 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
 

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
 

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. :idea:
bye
sam
 

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=adchex%10;
// value=adchex%10+0x30; //for ASCII presentation
adchex=adchex/10;
}
}

Hope it helps.

Regards,
George
 

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
 

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
 

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!! :D

now u see
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top