wiring_shift.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. wiring_shift.c - shiftOut() function
  3. Part of Arduino - http://www.arduino.cc/
  4. Copyright (c) 2005-2006 David A. Mellis
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General
  14. Public License along with this library; if not, write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
  18. */
  19. #include "wiring_private.h"
  20. uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
  21. uint8_t value = 0;
  22. uint8_t i;
  23. for (i = 0; i < 8; ++i) {
  24. digitalWrite(clockPin, HIGH);
  25. if (bitOrder == LSBFIRST)
  26. value |= digitalRead(dataPin) << i;
  27. else
  28. value |= digitalRead(dataPin) << (7 - i);
  29. digitalWrite(clockPin, LOW);
  30. }
  31. return value;
  32. }
  33. void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
  34. {
  35. uint8_t i;
  36. for (i = 0; i < 8; i++) {
  37. if (bitOrder == LSBFIRST)
  38. digitalWrite(dataPin, !!(val & (1 << i)));
  39. else
  40. digitalWrite(dataPin, !!(val & (1 << (7 - i))));
  41. digitalWrite(clockPin, HIGH);
  42. digitalWrite(clockPin, LOW);
  43. }
  44. }