We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
hello everyone, i am writing program in 'c'
i have bcd to hex routine in which i am using 2 variable of unsigned int data type initialised to '0' and '1000' resp eg: unsigned int a=0;
program compiles well and downloads in chip of c8051f020
but when i debug, one of the variables contents remain unchanged
i checked the ram location where it is stored,ram location contents are not changed by statement
why this is happening,
data declared in the function is data i.e stored in genereal purpose ram
in c memory, register are all taken care by compiler but my locations are affecting
i have bcd to hex routine in one '.c' file and calling is in another '.c' file
is it creating a problem? but i have many such files and they are not affecting
even i checked stack also but nothing i could make out
thanx to all take care
I still have a flu, but I don't think that is why I don't fully get it. show some code (don't forget the right tags). also specify where exactly it goes wrong and other information that you think is necessary.
hi thanx for replay this is the code and on line unsigned int power = 1000; and hex = 0x0000; it does not give me the change i have test it with stepwise debugging and checked the ram location but value doesnot change even i checked watch window
unsigned int Bcd4toHex4(unsigned int a) { uchar i; unsigned int hex; unsigned int power = 1000; hex = 0x0000; //code of conversion return(hex);//returns the converted data }
this routine changes 255 to 0x255
and calling of the function: variable1 = Bcd4toHex4(var);
where variable1 and var both are unsigned int
thank you any queries pls let me know
You have the comment "//code of conversion" but no conversion code can be found. Did you forget to write any code?
If you convert 255 to 0x255 then you are converting decimal to BCD, not BCD to hex.
Note that a conversion BCD to hex is not meaningful. Hex is a numeric base, not a numeric format. It is just a way of _presenting_ the data. Or was the intention to generate four ASCII characters in the range '0'..'9','A'..'F', i.e. to generate four bytes of hex output? But you can't return such information in your function - four characters can't fit in a unsigned int.
To convert from BCD to a "normal" number (for later presentation with printf() in decimal with %d or hexadecimal with %x) you can do:
Use a mask 0x000f. Use a multiplier 1. Set result to zero. While the number is non-zero: - Add number bit-and mask multiplied by multiplier to result. - multiply multiplier by ten. - shift number right four steps (i.e. dividing by 16) Return the result.
But remember: I don't really know exactly what you do want to do, since the name of your function doesn't seem to match your described intention. And the reason your function does not work is because it does not contain any conversion code at all.
Walk through your input number. Add to
"[...] stored,ram location contents are not changed by statement"
Your code basically looks like (after stripping away meaningless code that does not give any side effects):
unsigned int Bcd4toHex4(unsigned int a) { return 0; }
So, why should the compiler change any contents of any memory area? You must _do_ something in your function, to get the compiler to bother to generate any code.