[AVR] Variable value convert in decimal

Status
Not open for further replies.

BHARGAVSHANKHALPARA

Junior Member level 3
Joined
May 6, 2010
Messages
27
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
GUJARAT INDIA
Visit site
Activity points
1,592
Hi all...

i am programming ATMEGA32 microcontroller and ATMEL STUDIO 6 for programming in my application.

in my application user enter 3 difference value which i have store in char variable.

At the end i combine this 3 value in one variable and compare it with predefine value. when i combine in one variable it automatically convert into decimal.

for example i have 3 variable named S1,S2,S3

user enter S1=2,S2=0,S3=9;

at end combine value in TOTAL variable which is INT.

TOTAL = (S3 | (S2 << 4) | (S1 << 8));

so TOTAL value should be 209

but it shows after decimal conversation 521.

when i compare it with 209 this gives no result, but it gives result when my predefine value 521.

i want to get result when user enter 209 and my predefine value also 209.
 

You seem to be assuming that 2<<8 is 200. In my calulation it's 512.

The C operator << is describing binary rather than BCD operation.
 

you can try thiis:
uint_16 a;
int b;
a = S3;
a = (a<<8)|S2;
a = (a<<8)|S1;
b = a ;
 

you can try this:
uint_16 a;
int b;
a = S3;
a = (a<<8)|S2;
a = (a<<8)|S1;
b = a ;
Did you understand at all what the original poster wants to achieve? It rather looks like he want to assemble decimal digits, like

Code:
TOTAL = (S3 + (S2 * 10) + (S1 * 100));
respectively performed as sequential action
Code:
TOTAL = (S1*10 + S2)*10 + S3;
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…