CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Printing Binary Representation.....

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest
Guest







Printing Binary Representation.....
PostPosted: Sun Jan 29, 2006 9:00 pm     Reply with quote

Hi,

I have a status byte that is read from an external piece of hardware and I'd like to print out the binary representation of the byte for clarity during troubleshooting. Can this be done directly, or will I have to test each bit individually and then display them?

Ian
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 29, 2006 9:44 pm     Reply with quote

The answer is in the archives.
http://www.ccsinfo.com/forum/viewtopic.php?t=18826
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

Binary print
PostPosted: Mon Jan 30, 2006 10:53 am     Reply with quote

This is how I print binary. This could be shortened/cleaned up.

Code:
#include <16F877a.h>
#device *=16
#use delay(clock=20000000)
#fuses hs,nowdt,nolvp,protect,put,brownout
#use rs232(baud=19200,xmit=PIN_B4,INVERT,stream=DEBUG)
#case
#zero_ram
void bin_putc( char c);
//======================= Main ==============================
void main(void)
{
  int8 data=180;//180=0xB4=1011 0100
  printf(bin_putc,"This is a test %lX\n\r",data);
  while(1)
  {

  }
}

//======================= print binary nibbles from hex ============================//
void bin_putc( char c) {
//converts hex to bin
//printf(bin_putc,"%lX\n\r",data);
switch (c) {
      case '0'   :
          fprintf(DEBUG,"0000 ");
         break;
      case '1'   :
          fprintf(DEBUG,"0001 ");
         break;
      case '2'   :
          fprintf(DEBUG,"0010 ");
         break;
      case '3'   :
          fprintf(DEBUG,"0011 ");
         break;
      case '4'   :
          fprintf(DEBUG,"0100 ");
         break;
      case '5'   :
          fprintf(DEBUG,"0101 ");
         break;
      case '6'   :
          fprintf(DEBUG,"0110 ");
         break;
      case '7'   :
          fprintf(DEBUG,"0111 ");
         break;
      case '8'   :
          fprintf(DEBUG,"1000 ");
         break;
      case '9'   :
          fprintf(DEBUG,"1001 ");
         break;
      case 'A' || 'a'   :
          fprintf(DEBUG,"1010 ");
         break;
      case 'B' || 'b'     :
          fprintf(DEBUG,"1011 ");
         break;
      case 'C' || 'c'    :
          fprintf(DEBUG,"1100 ");
         break;
      case 'D' || 'd'     :
          fprintf(DEBUG,"1101 ");
         break;
      case 'E' || 'e'     :
          fprintf(DEBUG,"1110 ");
         break;
      case 'F' || 'f'     :
          fprintf(DEBUG,"1111 ");
         break;

      default     :
         fprintf(DEBUG,"%C",c);
         break;
   }
}



Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Jan 30, 2006 2:29 pm     Reply with quote

Following is PCM Programmer code slightly optimized.
Code:


void display_binary(char c)
{
char i;

   i = 8;
   putc('0');
   putc('b');

   do
     {
       if( c & 0x80 )
         { putc('1'); }
       else
         { putc('0'); }

       c <<= 1;  i--;
     } while(i);
}



Short, fast and elegant.

Humberto
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group