Pic program to convert bcd to binary - Please Help

Status
Not open for further replies.

Iawia

Member
Hey guys,

I have searched throughout the forum but only see alot of info for similar programs in assembly. In my pic program (writing in c), I would like to take an (int) variable, and convert it to binary and then output it to some leds.

I am unclear on how to store the variable in c (is there a binary type?).

max decimal value is 9.

Does anyone know how to do this?

Also just as a side question, is there any coding debuggers for pics? From what I can see, MPLAB has no serial display like the Arduinos. Would be nice to see output for debugging purposes.

--------------
Using:

chip: pic12F508
MPLAB IDE v8.80
HI-Tech c compiler
 
9 in base 10 or 16 is still 9. As long as it's stored as an integer your fine, it will sore perfectly fine.
It can be stored in a byte.

You don't convert int's to binary. They already are.

Testing bits to see if they are set X and 1, X and 2 or X and 4 or X and 8, you can check if they are non-zero. Shifting is another way.

and with 1, shift and with 1 etc.
 
Hi KeepIt,

can you clarify what you mean 'you don't convert int's to binary. they already are'. Are you saying I can load the GPIO like this:

GPIO = int_variable;

then my LEDS GPIO1-GPIO5 will light up respective of the variable?
 
My C is a bit rusty, but I gather that GPIO is 8 bits, so yOu would most likely have to typecast a char into a 8 bit integer.

So, assuming GPIO is an 8 bit integer and GPIO.bit 0 is the lowest order bit then:

GPIO = (GPIO .and. NOT 15) .OR. BCDVALUE, that's if you want to preserve the contents of bits 4 & 5

Reading the port and ANDING with 15 will clear the low order bits. ORing it with the BCD value will set them.

So suppose the port is 16+32+1 or 00110001 binary. The data sheet seems to say that the upper two bits get read as a zero.
Make BCD Value =6 or 1010

00110001 ; present value of port
11110000 = NOT 15 ; bits of interest
00110000 AND the values; clear the bits of interest
00001010 the value 6; The desired value of the low order bits
00111010 OR the two values; The first line with the 4 lower bits changed

I'm not using valid C constructs.

There is some interesting stuff here: https://www.electro-tech-online.com/custompdfs/2012/08/Microchip20C1820Compiler20Use.pdf

The point is to type cast into an unsigned char and how to set/clear bits.
 
Last edited:
Hi KeepIt,

can you clarify what you mean 'you don't convert int's to binary. they already are'. Are you saying I can load the GPIO like this:

GPIO = int_variable;

then my LEDS GPIO1-GPIO5 will light up respective of the variable?

Then you are reading a binary byte... (sorry nibble)

GPIO = 000X = 1
GPIO = 00X0 = 2
GPIO = 00XX = 3

etc....

Or is this an LCD with an onboard de-muliplexer....
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…