Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C++
#include <xc.h>         // library of functions for this chip 
#include <stdio.h>      // library containing printf() function
#include "configureUSART.h" // library for configureUSART(baud)
#include "configuration_bits.h"


void printCharAsBinary(unsigned char number);
void WaitOneSecond(void);

int main(void)  
{
    unsigned i = 0;
    configureUSART(9600ul, 1); // configure MCU serial communication module to run at 9600 baud 
                               // defined in configureUSART.c

    WaitOneSecond();   // The splash screen lasts about one second
                       // LCD will not respond to printf() until it is finished.

    for(i=0; i < 256; i++)
      {
	printf("\n\r %u = ",i);
        printCharAsBinary((unsigned char)i);
      }                         

while(1)
   {
	  // MCUs run continuously so an endless loop is required.
   }
}	

void printCharAsBinary(unsigned char number)
{

if ( ((number & 0b10000000) >> 7 ) == 1)
   printf("0b1");
else
   printf("0b0");

if ( ((number & 0b01000000) >> 6 ) == 1)
   printf("1");
else
   printf("0");

if ( ((number & 0b00100000) >> 5 ) == 1)
   printf("1");
else
   printf("0");

if ( ((number & 0b00010000) >> 4 ) == 1)
   printf("1");
else
   printf("0"); 

if ( ((number & 0b00001000) >> 3 ) == 1)
   printf("1");
else
   printf("0");

if ( ((number & 0b00000100) >> 2 ) == 1)
   printf("1");
else
   printf("0");

if ( ((number & 0b00000010) >> 1 ) == 1)
   printf("1");
else
   printf("0");

if ( ((number & 0b00000001) ) == 1)
   printf("1");
else
   printf("0");
}

void WaitOneSecond(void)
{
int  i = 0;
for(i=0; i<=5; i++) 
   {
    _delay(50000ul);  // 50 000 * 4 / 1 000 000 = 1/5 s 
   }
}


What I have tried:

I am supposed to create a code for binary conversion of int, I have the following code for char. I understand that char is 8 bits and int is 16 bits and I would need more shift operation to check " how many powers of two are there"

if I have 7 here would I have 14. could someone explain to me how these shift operators are set up. I understand they are checking the power of two but how are they doing it and if yes how can I apply this to higher bits. how do i set up shift operators?

Using serLCD by sparkfun and PIC18F4525
Posted
Updated 21-Jan-18 8:02am
v2

You must understand the so called bit shift operator (>>) which moves the bits of the input to lower side. So a repeating call moves all bits out of the memory.

This operator moves the bits of the memory which is interpreted as some variable. To really understand it you must understand that all bits are in the memory and the actual value is the interpretation of that memory. Easiest to understand with strings. Some explaation with some sample code from Microsoft.

I found a really nice implementation of your task on stack overflow. The results is nicely stored in string buffers.
 
Share this answer
 
I have solved this thank you all for help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900