View previous topic :: View next topic |
Author |
Message |
forgie Guest
|
Can you use printf to display binary digits? |
Posted: Thu Nov 03, 2005 9:59 pm |
|
|
Hi there. I'm new to CCS, but I have quite a lot of C experience, as well as PIC experience (using PBP). I like working in C, but I'm wondering whether or not it's possible to somehow ammend printf so that you could say write:
Byte x = 255;
printf("%b", x);
And have that output "11111111". As in make a format command for binary representation of a number. I'm currently using an LCD, and am using printf with my LCD_Putc() function. I have made a seperate function called LCD_Putb() which outputs a single byte as a binary number. Is there anyway to somehow ammend printf so that a certain command (e.g. "%b") will make printf point to LCD_Putb() instead of LCD_Putc()? Is there some built-in way to represent binary numbers?
At the moment I can just write:
printf(LCD_Putc, "whatever %X", x);
LCD_Putb(y);
Which is fine, but I'd rather be able to use it from inside printf if possible, for readability. In PBP, binary numbers are builtin to the LCDOUT command. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
forgie
Joined: 03 Nov 2005 Posts: 7
|
|
Posted: Fri Nov 04, 2005 1:04 am |
|
|
Um that thread is basically talking about doing what I've already done, which is make a function which displays a byte in binary form. What I want to know is can I amend the printf function somehow to add a formatting command which means 'binary'. It already has built-in formatting commands for hex and decimal numbers, I just want to add a formatting command for binary. Is it possible to change the way that PICC deals with printf?
edit: my function looks like this if anyone's interested
Code: |
void lcd_putb( byte b)
{
byte i;
for (i = 0; i < 8; i++)
if(bit_test(b, i))
lcd_putc('1');
else
lcd_putc('0');
}
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Nov 04, 2005 6:07 am |
|
|
forgie wrote: | Um that thread is basically talking about doing what I've already done, which is make a function which displays a byte in binary form. What I want to know is can a ammend the printf function somehow to add a formatting command which means 'binary'. It already has builtin formatting commands for hex and decimal numbers, I just want to add a formatting command for binary. Is it possible to change the way that PICC deals with printf?
edit: my function looks like this if anyone's interested
Code: |
void lcd_putb( byte b)
{
byte i;
for (i = 0; i < 8; i++)
if(bit_test(b, i))
lcd_putc('1');
else
lcd_putc('0');
}
|
|
No, you would have to write your own printf |
|
|
|