PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

95 líneas
3.3KB

  1. #ifndef PWMServo_h
  2. #define PWMServo_h
  3. /*
  4. PWMServo.h - Hardware Servo Timer Library
  5. http://arduiniana.org/libraries/pwmservo/
  6. Author: Jim Studt, jim@federated.com
  7. Copyright (c) 2007 David A. Mellis. All right reserved.
  8. renamed to PWMServo by Mikal Hart
  9. ported to other chips by Paul Stoffregen
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 2.1 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <inttypes.h>
  23. #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) // Arduino
  24. #define SERVO_PIN_A 9
  25. #define SERVO_PIN_B 10
  26. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // Arduino Mega
  27. #define SERVO_PIN_A 11
  28. #define SERVO_PIN_B 12
  29. #define SERVO_PIN_C 13
  30. #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) // Sanguino
  31. #define SERVO_PIN_A 13
  32. #define SERVO_PIN_B 12
  33. #elif defined(__AVR_AT90USB162__) // Teensy 1.0
  34. #define SERVO_PIN_A 17
  35. #define SERVO_PIN_B 18
  36. #define SERVO_PIN_C 15
  37. #elif defined(__AVR_ATmega32U4__) // Teensy 2.0
  38. #define SERVO_PIN_A 14
  39. #define SERVO_PIN_B 15
  40. #define SERVO_PIN_C 4
  41. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) // Teensy++
  42. #define SERVO_PIN_A 25
  43. #define SERVO_PIN_B 26
  44. #define SERVO_PIN_C 27
  45. #elif defined(__arm__) && defined(TEENSYDUINO)
  46. #define SERVO_PIN_A 9
  47. #define SERVO_PIN_B 10
  48. // all PWM pins supported, but names not defined here...
  49. // just use the actual PWM pin number with attach()
  50. #else
  51. #define SERVO_PIN_A 9
  52. #define SERVO_PIN_B 10
  53. #endif
  54. class PWMServo
  55. {
  56. private:
  57. uint8_t pin;
  58. uint8_t angle; // in degrees
  59. uint8_t min16; // minimum pulse, 16uS units (default is 34)
  60. uint8_t max16; // maximum pulse, 16uS units, 0-4ms range (default is 150)
  61. #if defined(__AVR__)
  62. static void seizeTimer1();
  63. static void releaseTimer1();
  64. static uint8_t attachedA;
  65. static uint8_t attachedB;
  66. #ifdef SERVO_PIN_C
  67. static uint8_t attachedC;
  68. #endif
  69. #elif defined(__arm__) && defined(TEENSYDUINO)
  70. static uint32_t attachedpins[]; // 1 bit per digital pin
  71. #endif
  72. public:
  73. PWMServo();
  74. uint8_t attach(int pinArg) { return attach(pinArg, 544, 2400); }
  75. // pulse length for 0 degrees in microseconds, 544uS default
  76. // pulse length for 180 degrees in microseconds, 2400uS default
  77. uint8_t attach(int pinArg, int min, int max);
  78. // attach to a pin, sets pinMode, returns 0 on failure, won't
  79. // position the servo until a subsequent write() happens
  80. // Only works for 9 and 10.
  81. void detach();
  82. void write(int angleArg); // specify the angle in degrees, 0 to 180
  83. uint8_t read() { return angle; }
  84. uint8_t attached();
  85. };
  86. #endif