Hi all,
I have to interface an IC which is giving serial output bitwise from a single pin this pin is directly connected to a microcontroller i have to read this single pin to an unsigned int.
i have written a function something like that but its not working.

unsigned int read_val()
{
 unsigned char i;
 unsigned int data=0;//initially zero
 for(i=0;i<=15;i++)
   {
    data<<=1;
    if(PINB==1)
     {data|=1;}
     else
      {
      data|=0;
      }
   }
   return data;
   }

So the output of that function should be if pin is toggling as 1110110001100110
then data should contain data=0X EC66.
Thanks.

Recommended Answers

All 3 Replies

Please explain what "not working" means in your situation.

Through the crystal ball I can see that you need to pace out the sampling. A single pin IO usually implies a time-based protocol.

I would change one thing and test, hope it works

unsigned int read_val()
{
 unsigned char i;
 unsigned int data=0;//initially zero
 for(i=0;i<=15;i++)
   {
    data<<=1;
    if(PINB==1)
     {data|=1;}
     else
      {
      data&=0;
      }
   }
   return data;
   }
    if(PINB==1)
     {data|=1;}
     else
      {
      data&=0;
      }
      data<<=1; // here maybe more logical
      // also a delay might be neccessary here
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.