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
Great Work Sir.
ReplyDeleteIt Is working
Thanks a lot
Ravi
Hi,
ReplyDeleteI have tested the code and it works very well.
I wand know to convert it for the embedded C of the 8051.
Many thanks!