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.

HOW TO ADD 0X ALONG WITH A DECIMAL NUMBER IN PIC16F877A programming

Status
Not open for further replies.

rangerskm

Full Member level 4
Full Member level 4
Joined
Jan 23, 2013
Messages
199
Helped
0
Reputation
2
Reaction score
0
Trophy points
1,296
Activity points
2,663
mplab ide ,hi tech c compiler

here in the expression is

y2 is ones and y1 is tens
Y=((y1*10)+y2);

i want to send Y as hexadecimal format to another function.
EX :

y1=2
y2=4
Y =((2*10)+4)=24


now i want to send this 24 in hex format argument to another function
please guide me in this..

as 0x24
 

Be careful what you ask for! you are confusing decimal and hexadecimal. The expression "Y =((2*10)+4)=24" works correctly in decimal but the hexadecimal equivalent is 0x18. ((1*16)+8).

Do you want to send 0x24 or 0x18?

Brian.
 

Why do you want to send it as hex? If you want to display it as a string "0x24" on LCD or UART then you need to convert each digit of the number to character and then concatanate it with "0x" and send the string but if you want to send a number (int, etc.. non ascii value) then you don't have to do any conversion because 0x24, 36, 0b100100 are all same. If you pass the value 0x24 or 36 or 0b100100 to a function then they all are same.
 

I want to write the user set time in ds1307 ic in that i take the user values in decimals ,that has to be appended with 0x so that it will become the correct format ,for eg let the time is 8 o clock .in decimal its 8 the ic recoginize 8 o clock as 0x08 ,then for 12 o clock the ic recognize as 0x12 here i want to pass the user entered time 8 as 0x08 in to the IC ,so suggest me an idea to append 0x in decimal

in short i want to pass decimal value in hex format as an argument to function
 

You cant append "0x" to a integer value. RTC format is in BCD. Use BCD to DEC and DEC to BCD routines.
 

It's BCD not hex then.
Still seems a strange way to do it as the value written to the DS1307 doesn't care what radix it is in. If your function needs a string input I would change it to accept a more useful format instead. If you really have to send it as a string, use:

sprintf(StringName,"0x%02d",NumberToConvert); where StringName is the number with '0x' in front of it and NumberToConvert is the decimal number before being prefixed.

Brian.
 

unsigned int f;
sprintf(f,"0x%02d",19);
TXREG=f;while(!TRMT);

is it the format to display the formatted number in uart??
 

No.
If you are sending a number with a text radix specifier like '0x' before it, you must send it as text. It would be the characters '0', 'x' then the two ascii characters of the number itself. This means it has to be a string of at least 4 characters, ideally 5 so you can add a NUL terminator to it.

There are various ways to do it but probably the simplest, especially if you are debugging is to treat the string as an array of single characters.
Code:
char f[5];
char Counter;

sprintf(f,"0x%02d",19); // this will create the string "0x19"
for(Counter = 0; Counter < 4; Counter++)
{
TXREG = f[Counter];
while(!TRMT);
}

I still have doubts about whether this is what you really want to do but give it a try.

Brian.
 
What exactly are you trying to do? Explain with an example.
 

Usually we a to_bcd conversion for numbers 0 to 99
Code:
unsigned char byte2bcd(unsigned char data) 
{
  return (data/10*16 + data%10);
}
In your example, the operation is simply Y = 16*Y1 + Y2;
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top