On Fri, Mar 26, 2004 at 08:00:11AM +0800, Arun Kumar Saigal wrote: > Hello, > > I am a new user of the PIC and was wondering if anyone could help me > with the problem of converting 8 bit binary to 3 BCD in the PIC 16F877. > I am programming in C. I have all the details of this in the code for my sunrise/sunset controller on my pic pages: http://www.finitesite.com/d3jsys, so I won't give them all here. But here's the basic idea. 1) Take the upper and lower nybble of the 8 bit number and convert them to their BCD equivalents. I did this by table lookup. 2) Add the converted BCD values of the upper and lower nybbles. 3) Adjust any BCD nybbles that are out of spec. So for example let's say we wanted to convert 0xCA to BCD. The upper nybble is C and the lower is A. We convert both to BCD equivalents. C0 is 192 in decimal, so we would convert the C to 0x192. A is 10 in decimal so we'd convert the A to 0x10. So we end up with upper = 0x0192 lower = 0x0010 Now we add these two values: --------------- Total = 0x01A2 Now note that the 'A' in the result doesn't work in BCD land so you have to do what's called a decimal adjust. For each nybble you decimal adjust by adding a 6 in that nybbles column. Since the 'A' is in the second column you'd add 0x60: DAA = 0x0060 -------------- Final = 0x0202 Voila! you're done. You can find more details in my code. You'll have to figure out how to write it in C yourself. But with the concepts above, it's pretty easy to do. BAJ -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body