Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1069301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:24:12+00:00 2026-05-16T20:24:12+00:00

I am trying to write a function in C that will shift out the

  • 0

I am trying to write a function in C that will shift out the individual bits of a byte based on a clock signal. So far I have come up with this…

void ShiftOutByte (char Data)
{
    int Mask = 1;
    int Bit = 0;
    while(Bit < 8)
    {
        while(ClkPin == LOW);
        DataPin = Data && Mask;
        Mask = Mask * 2;
        Bit++;
    }
}

where DataPin represents the port pin that I want to shift data out on and ClkPin is the clock port pin.

I want the device to shift out 8 bits, starting on the LSB of the byte. For some reason my output pin stays high all the time. I am certain that the port pins are configured properly so it is purely a logical issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:24:14+00:00Added an answer on May 16, 2026 at 8:24 pm

    Your solution is almost there, but there a few problems:

    • When clock goes high, you will pump out data in a burst before the clock has a chance to go low again, so you need to pause while clock is high before you check for it being low (unless the hardware pauses execution until clock goes low). This can be done either at the end of the loop, or at the beginning. I chose the beginning in the sample below, because it allows you to return from the function while clock is still high, and do some processing while it is still high.
    • You used the logical and (&&) instead of bitwise and (&) as others have pointed out
    • I am not familiar with your architecture, so I can’t say if DataPin can only accept 0 or 1. But that is also another point where things may go wrong. Data & Mask will return a bit that is shifted left (1, 2, 4, 8, …) depending on the bit position.

    So, in summary, I would do something like:

    void ShiftOutByte (unsigned char Data)
    {
        int Bit;
        for(Bit=0; Bit < 8; ++Bit)
        {
            while(ClkPin == HIGH);
            while(ClkPin == LOW);
            DataPin = Data & 1;
            Data >>= 1;
        }
    }
    

    Note: I have used a for loop instead of the while ... Bit++ pattern for clarity of purpose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that will let me red-shift or blue-shift a
I'm trying to write a generic function that will block out the UI while
I am trying to write a function that will allow me to browse for
I'm trying to write a function that will: If there is no region selected,
i am trying to write a function that will make DataRow[column] return nullable typed
I am trying to write a function that will filter a list of tuples
I am trying to write a function that will: Disable the submit button when
I am trying to write a function that will randomly return an (x,y) co-ordinates
I'm trying to write a function that will return all the available cars for
I am trying to write a function that will convert a generic list to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.