Blogroll

Powered by Blogger.

Monday 21 April 2014

What is the Code for Arduino shiftOut() function ?

by realfinetime  |  in ATmega328 at  00:56

          After completing my blog on Arduino shiftOut I became enthusiastic to know, what is happening while calling shiftOut function. So i searched for that and found the code for the shiftOut() function. Code for shiftOut() function is given below.



void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
{
      uint8_t i;

      for (i = 0; i < 8; i++)  {
            if (bitOrder == LSBFIRST)
                  digitalWrite(dataPin, !!(val & (1 << i)));
            else      
                  digitalWrite(dataPin, !!(val & (1 << (7 - i))));
                  
            digitalWrite(clockPin, HIGH);
            digitalWrite(clockPin, LOW);            
      } 
}


To check whether this code will work, complete the following circuit.

Now upload the following program to the arduino board. shiftOut function is renamed as shiftOut_function.


//Pin connected to ST_CP of 74HC595
int latchPin = 12;
//Pin connected to SH_CP of 74HC595
int clockPin = 13;
////Pin connected to DS of 74HC595
int dataPin = 11;

void setup() 
{
    //set pins to output so you can control the shift register
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
}

//code for shiftOut function

void shiftOut_function(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
{
      uint8_t i;

      for (i = 0; i < 8; i++)  {
            if (bitOrder == LSBFIRST)
                  digitalWrite(dataPin, !!(val & (1 << i)));
            else      
                  digitalWrite(dataPin, !!(val & (1 << (7 - i))));
                  
            digitalWrite(clockPin, HIGH);
            digitalWrite(clockPin, LOW);            
      } 
}

void loop() 
{
    // count from 0 to 255 and display the number 
    // on the LEDs
    for (int display_number = 0; display_number < 256; display_number++) 
    {
       // take the latchPin low so 
       // the LEDs don't change while you're sending in bits:
       digitalWrite(latchPin, LOW);
       // shift out the bits:
       shiftOut_function(dataPin, clockPin, MSBFIRST, display_number);  
       //take the latch pin high so the LEDs will light up:
       digitalWrite(latchPin, HIGH);
       // pause before next value:
       delay(500);
   }
}


Output of the above program is given below.

                                                Q7     Q6     Q5    Q4     Q3    Q2     Q1    Q0

2 comments:

  1. Great Work Sir.

    It Is working

    Thanks a lot
    Ravi

    ReplyDelete
  2. Hi,
    I have tested the code and it works very well.
    I wand know to convert it for the embedded C of the 8051.

    Many thanks!

    ReplyDelete

IMPORTANT NOTICE

All the circuits, published in this blog is only after testing and getting proper results in my private lab. When you try these circuits, you should check the supply voltage, polarity of components, presence of childrens nearby and shorts in the circuits. This website will not be responsible for any harm happened to you or your components caused by your carelessness.

For More Electronic Tips



Proudly Powered by Blogger.