NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Everything Else » print register value?

April 27, 2010
by Ralphxyz
Ralphxyz's Avatar

How would I print a Register value on the LCD?

fprintf_P(&lcd_stream, PSTR("PORTC: %b"), PORTC);

Does not work (surprise surprise)

Is there a binary print parameter?

Ralph

April 27, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Ralph,

There is no very easy way to just print to a binary representation. My suggestion is to print out the hex representation with %x. An 8 bit number will print as two digit hex number, which can be pretty easy to convert to binary on paper. Eventually you will just know the binary representation from the hex.

The alternative is to write a function print out the number bit by bit.

Humberto

April 28, 2010
by wayward
wayward's Avatar

You can do something stupid like this:

#include <stdio.h>
#include <inttypes.h>

char *to_binary(char *str, uint8_t b)
{
  static uint8_t i;  /* static to force compiler to keep them on the heap, */
  static char *c;    /* not in the stack frames; can do without.           */

  i = 128;
  c = str;
  while (i > 0) {
    *c = (b & i ? '1' : '0');
    i >>= 1;
    c++; /* heh */
  }
  return str;
}

int main(void)
{
  char b[9] = {0};

  printf("%s\n", to_binary(b, 42));
}

Just call to_binary(...) passing it an address to write to, and a byte to convert. Be careful though; there's something here that you need to be wary of. If you try to do this:

printf("PORTA = %s\nPORTB = %s\n",
       to_binary(b, PORTA),
       to_binary(b, PORTB));

it will likely not print what you expect. Can you see what's wrong here without running the code? :)

April 28, 2010
by Ralphxyz
Ralphxyz's Avatar

Hey wayward that is not stupid (if it works of course) compared to some of the solutions out on the web some of which use hundreds of lines of code.

It seems a lot of new programmers such as my self want to do this.

It helps me to visuallize what is happening with the various registers after shift operations.

I just don't get the effect by viewing HEX values.

Thanks a lot, I'll be back with more questions after I answer your question and test the code.

Ralph

May 10, 2010
by Ralphxyz
Ralphxyz's Avatar

I "should" have been more explicit in how I asked my question.

This question led to this question http://www.nerdkits.com/forum/thread/723/.

"I am "trying" to show the value of the PORTB register bits. PB0, PB1, PB2, PB3, PB4, and PB5 on the LCD."

In searching the web for methods to print binary values I came upon some very elaborate schemes, none of which I could fully comprehend or even just cut and paste the code which is my normal modus operandi.

The reason I wanted to see the binary values was to literally see the pin settings. As I said I have/had limited capacity to look at a hex number and to be able to recognize the equivalent binary value.

This was of course from my lack of understanding hex because I had never used it on a daily basis, maybe once a year I might see a hex number (which I would google to see the true value in decimal.

So if I had asked how would I visualize the binary value of the PORTB and PORTC registers I probable would have gotten a response such as this:

In C there is no direct method of printing binary values such as: "fprintf_P(&lcd_stream, PSTR("PORTC: %b"), PORTC);"

but there is methods of printing HEX values with "fprintf_P(&lcd_stream, PSTR("PORTC: %x"), PORTC);".

As there are only six bit/pin values for PORTB and PORTC PB0, PB1, PB2, PB3, PB4 and PB5 and PC0, PC1, PC2, PC3, PC4 and PC5 once you know the value of the bit you coud just hard code them into your code, thus:

PORTB |= (1<<PB5);  // ON 
lcd_line_one;
fprintf_P(&lcd_stream, PSTR("PB5 0x%x= 00010101"), PORTB);    // 21
delay_ms(500);
PORTB &= ~(1<<PB5);   // OFF

Now here is the interesting bit especially for those new to programming, after doing the above sequence for all of the various pins a lot of times, and I mean probable a hundred at the minimum. I started thinking the binary values are getting in the way, that I really didn't need them since once I saw the hex value I knew what the binary representation would be.

Learning hex or any numbering system becomes easy with exposure. It is like moving to a foreign country and getting used to using their currency. When you start out you always want to translate any transaction into US dollars $. You pay the tab at a bar and you think 5000 centivos (or what ever) lets see divide that by 3.4567 and subtract 3 = $XX.00 once you get used to the currency you just accept 5000 centivos as the bill. If you really want to get good and fast at adapting to foreign currency you should get paid in the lands currency (no longer foreign) believe me you get paid in the lands currency you will quickly learn it's value.

The reason the solutions I found on the web for printing binary values were so elaborate is because they are methods to printing any random binary value where as I was actually looking for very limited specific binary values.

Oh just in case you want to actual see binary values printed out here is a neat HEX to Binary site you can just google hex to binary to find similar sites.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that the printf format string "%.3f" will show three digits after the decimal point? Learn more...