0
\$\begingroup\$

I am trying to interface an ENC28J60 ethernet controller IC with an ATmega16 µC. In order to debug my my code, I have built an UART circuit and wrote the appropriate code and I can display any string or character through it on my PC.

But I'm not able to display the register contents of ENC28J60 through UART since the contents are in binary format. I Googled a lot but did not find a way to display binary data through UART. Any help,guidance or comments is welcome.

My SPI code to access the ENC28J60 registers.

uint8_t ENC_readControlRegister()
{
 //pull PC5(chip select) pin low.
 PORTC = (0<<5);
 //put the command corresponding to ECON1 register into SPDR register.
 SPDR = 0x1F;
 //wait until transmission is complete.
 while(!(SPSR)&(1<<SPIF)) {};
 //make PC5(chip select) pin high to signify completion of sending command.
 PORTC = (1<<5);

 //read the received data.
 return(SPDR);
}   

now how to print the uint8_t data received from this function in binary format?

\$\endgroup\$
2
\$\begingroup\$

itoa() will let you convert an int to a string representation in any radix from 2 to 36. I recommend sticking with 2, 8, or 16 for displaying binary data.

\$\endgroup\$
  • \$\begingroup\$ Now there is an apparent problem with using itoa, suppose my the value returned by the function is say "00000001"(in binary) all that'll be printed on the screen would be "1". Why is that?? \$\endgroup\$ – GriffinTaimer Mar 9 '15 at 8:56
  • \$\begingroup\$ Because the leading zeroes are redundant and pointles. Just like you write "73" instead of "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000073". \$\endgroup\$ – Majenko Mar 9 '15 at 10:21
0
\$\begingroup\$

Do you have a printf available? For a small controller you will probably need to setup it first, but then you could simply

#include "stdio.h" 

and you should be fine to display any variable you want in any way you want without too much manual handling..

For your case you could use:

printf("0x%08x", var);

where var is your 32bit variable you want to display

\$\endgroup\$
  • 1
    \$\begingroup\$ The problem with using printf() et alia is that they are very large and very slow. \$\endgroup\$ – Ignacio Vazquez-Abrams Mar 9 '15 at 6:10
  • \$\begingroup\$ Large: Yes, Slow: Kind of; But they are very simple to use and eliminate many manual conversions (thus reducing potential errors). Why not rely on something "proven". \$\endgroup\$ – Tom L. Mar 9 '15 at 6:29
0
\$\begingroup\$

I personally prefer converting to hex. Below is code that I use on Stellaris ARM platform to print 64 bit integers in hexadecimal format. It should be reasonably easy to port to AVR and 8 bits. Just change 64 bits to 8 bits and reduce the buffer length to hold 1 character for each 4 bits in your number plus a NULL byte that flags the end of the string.

void printUint64HexToUart( uint64_t number ) { 
        char message[ 18 ];

        int8_t i = 17; 
        message[ i-- ] = 0;

        // Convert each 4 bits into a single character.
        do {
                uint8_t nibble = number & 0x0f;
                if ( nibble <= 9 ) { 
                        message[ i-- ] = ( number & 0x0f ) + '0';
                } else {
                        message[ i-- ] = ( number & 0x0f ) + 'a' - 10; 
                }   
                number >>= 4;
        //} while ( number );   // Suppress leading zeroes.
        } while ( i >= 0 );     // Print leading zeroes.

        i++;

        // Output the string to UART.
        while ( message[ i ] != 0 ) { 
        do { } while ( ROM_UARTBusy( UART0_BASE ) );            // Wait for room in UART output buffer
                ROM_UARTCharPut( UART0_BASE , message[ i++ ] ); // Output a single character to UART.
        }   
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.