wiringShift.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * wiringShift.c:
  3. * Emulate some of the Arduino wiring functionality.
  4. *
  5. * Copyright (c) 2009-2012 Gordon Henderson.
  6. ***********************************************************************
  7. * This file is part of wiringPi:
  8. * https://projects.drogon.net/raspberry-pi/wiringpi/
  9. *
  10. * wiringPi is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * wiringPi is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. #include <stdint.h>
  25. #include "wiringPi.h"
  26. #include "wiringShift.h"
  27. /*
  28. * shiftIn:
  29. * Shift data in from a clocked source
  30. *********************************************************************************
  31. */
  32. uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order)
  33. {
  34. uint8_t value = 0 ;
  35. int8_t i ;
  36. if (order == MSBFIRST)
  37. for (i = 7 ; i >= 0 ; --i)
  38. {
  39. digitalWrite (cPin, HIGH) ;
  40. value |= digitalRead (dPin) << i ;
  41. digitalWrite (cPin, LOW) ;
  42. }
  43. else
  44. for (i = 0 ; i < 8 ; ++i)
  45. {
  46. digitalWrite (cPin, HIGH) ;
  47. value |= digitalRead (dPin) << i ;
  48. digitalWrite (cPin, LOW) ;
  49. }
  50. return value;
  51. }
  52. /*
  53. * shiftOut:
  54. * Shift data out to a clocked source
  55. *********************************************************************************
  56. */
  57. void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val)
  58. {
  59. int8_t i;
  60. if (order == MSBFIRST)
  61. for (i = 7 ; i >= 0 ; --i)
  62. {
  63. digitalWrite (dPin, val & (1 << i)) ;
  64. digitalWrite (cPin, HIGH) ;
  65. digitalWrite (cPin, LOW) ;
  66. }
  67. else
  68. for (i = 0 ; i < 8 ; ++i)
  69. {
  70. digitalWrite (dPin, val & (1 << i)) ;
  71. digitalWrite (cPin, HIGH) ;
  72. digitalWrite (cPin, LOW) ;
  73. }
  74. }