25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

118 satır
3.0 KiB

  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 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 General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU 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. }
  75. /*
  76. * shiftOut:
  77. * Shift data out to a clocked source
  78. *********************************************************************************
  79. */
  80. void shiftOutWithDelay (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val, int delay)
  81. {
  82. int8_t i;
  83. if (order == MSBFIRST)
  84. for (i = 7 ; i >= 0 ; --i)
  85. {
  86. digitalWrite (dPin, val & (1 << i)) ;
  87. delayMicroseconds (delay) ;
  88. digitalWrite (cPin, HIGH) ;
  89. delayMicroseconds (delay) ;
  90. digitalWrite (cPin, LOW) ;
  91. delayMicroseconds (delay) ;
  92. }
  93. else
  94. for (i = 0 ; i < 8 ; ++i)
  95. {
  96. digitalWrite (dPin, val & (1 << i)) ;
  97. delayMicroseconds (delay) ;
  98. digitalWrite (cPin, HIGH) ;
  99. delayMicroseconds (delay) ;
  100. digitalWrite (cPin, LOW) ;
  101. delayMicroseconds (delay) ;
  102. }
  103. }