You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiringShift.c 2.2 KiB

12 vuotta sitten
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }