motor.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * motor.c:
  3. * Use the PiFace board to demonstrate an H bridge
  4. * circuit via the 2 relays.
  5. * Then add on an external transsitor to help with PWM.
  6. *
  7. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  8. ***********************************************************************
  9. * This file is part of wiringPi:
  10. * https://projects.drogon.net/raspberry-pi/wiringpi/
  11. *
  12. * wiringPi is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * wiringPi is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  24. ***********************************************************************
  25. */
  26. #include <wiringPi.h>
  27. #include <piFace.h>
  28. #include <softPwm.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. int outputs [2] = { 0,0 } ;
  33. #define PIFACE_BASE 200
  34. #define PWM_OUT_PIN 204
  35. #define PWM_UP 202
  36. #define PWM_DOWN 203
  37. void scanButton (int button)
  38. {
  39. if (digitalRead (PIFACE_BASE + button) == LOW)
  40. {
  41. outputs [button] ^= 1 ;
  42. digitalWrite (PIFACE_BASE + button, outputs [button]) ;
  43. printf ("Button %d pushed - output now: %s\n",
  44. button, (outputs [button] == 0) ? "Off" : "On") ;
  45. }
  46. while (digitalRead (PIFACE_BASE + button) == LOW)
  47. delay (1) ;
  48. }
  49. int main (void)
  50. {
  51. int pin, button ;
  52. int pwmValue = 0 ;
  53. printf ("Raspberry Pi PiFace - Motor control\n") ;
  54. printf ("==================================\n") ;
  55. printf ("\n") ;
  56. printf (
  57. "This program is designed to be used with a motor connected to the relays\n"
  58. "in an H-Bridge type configuration with optional speeed control via PWM.\n"
  59. "\n"
  60. "Use the leftmost buttons to turn each relay on and off, and the rigthmost\n"
  61. "buttons to increase ot decrease the PWM output on the control pin (pin\n"
  62. "4)\n\n") ;
  63. wiringPiSetup () ;
  64. piFaceSetup (PIFACE_BASE) ;
  65. softPwmCreate (PWM_OUT_PIN, 100, 100) ;
  66. // Enable internal pull-ups & start with all off
  67. for (pin = 0 ; pin < 8 ; ++pin)
  68. {
  69. pullUpDnControl (PIFACE_BASE + pin, PUD_UP) ;
  70. digitalWrite (PIFACE_BASE + pin, 0) ;
  71. }
  72. for (;;)
  73. {
  74. for (button = 0 ; button < 2 ; ++button)
  75. scanButton (button) ;
  76. if (digitalRead (PWM_UP) == LOW)
  77. {
  78. pwmValue += 10 ;
  79. if (pwmValue > 100)
  80. pwmValue = 100 ;
  81. softPwmWrite (PWM_OUT_PIN, pwmValue) ;
  82. printf ("PWM -> %3d\n", pwmValue) ;
  83. while (digitalRead (PWM_UP) == LOW)
  84. delay (5) ;
  85. }
  86. if (digitalRead (PWM_DOWN) == LOW)
  87. {
  88. pwmValue -= 10 ;
  89. if (pwmValue < 0)
  90. pwmValue = 0 ;
  91. softPwmWrite (PWM_OUT_PIN, pwmValue) ;
  92. printf ("PWM -> %3d\n", pwmValue) ;
  93. while (digitalRead (PWM_DOWN) == LOW)
  94. delay (5) ;
  95. }
  96. delay (5) ;
  97. }
  98. return 0 ;
  99. }