Skip to content
This repository has been archived by the owner on Jan 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #22 from zfields/implemetShiftInOut
Browse files Browse the repository at this point in the history
Implemented shiftIn() and shiftOut()
  • Loading branch information
Lou Amadio committed Aug 6, 2014
2 parents e88e486 + 8691085 commit 9b1b4a5
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions source/arduino.h
Expand Up @@ -1014,6 +1014,42 @@ inline int analogRead(int channel)
return _ArduinoStatic.analogRead(channel);
}

inline uint8_t shiftIn(uint8_t data_pin_, uint8_t clock_pin_, uint8_t bit_order_)
{
uint8_t buffer(0);

for (uint8_t loop_count = 0, bit_index = 0 ; loop_count < 8 ; ++loop_count) {
if (bit_order_ == LSBFIRST) {
bit_index = loop_count;
} else {
bit_index = (7 - loop_count);
}

digitalWrite(clock_pin_, HIGH);
buffer |= (digitalRead(data_pin_) << bit_index);
digitalWrite(clock_pin_, LOW);
}

return buffer;
}

inline void shiftOut(uint8_t data_pin_, uint8_t clock_pin_, uint8_t bit_order_, uint8_t byte_)
{
for (uint8_t loop_count = 0, bit_mask = 0; loop_count < 8; ++loop_count) {
if (bit_order_ == LSBFIRST) {
bit_mask = (1 << loop_count);
} else {
bit_mask = (1 << (7 - loop_count));
}

digitalWrite(data_pin_, (byte_ & bit_mask));
digitalWrite(clock_pin_, HIGH);
digitalWrite(clock_pin_, LOW);
}

return;
}

//
// Arduino Sketch Plumbing
//
Expand Down

0 comments on commit 9b1b4a5

Please sign in to comment.