24 #ifndef _LIGHTWEIGHT_SERVO_H
25 #define _LIGHTWEIGHT_SERVO_H
27 #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) || defined (__AVR_ATmega328PB__) || defined(__AVR_ATmega2560__)
29 #define VERSION_LIGHTWEIGHT_SERVO "2.0.0"
30 #define VERSION_LIGHTWEIGHT_SERVO_MAJOR 2
31 #define VERSION_LIGHTWEIGHT_SERVO_MINOR 0
40 #define ISR_COUNT_FOR_20_MILLIS (F_CPU / (8 * 50)) // 40000 For 50 Hz, 20 ms using a prescaler of 8. You can modify this if you have servos which accept a higher rate
41 #define ISR_COUNT_FOR_2_5_MILLIS (F_CPU / (8 * 400)) // 5000 For 400 Hz, 2.5 ms using a prescaler of 8.
43 #if defined(__AVR_ATmega2560__)
44 #define LIGHTWEIGHT_SERVO_CHANNEL_A_PIN 46
45 #define LIGHTWEIGHT_SERVO_CHANNEL_B_PIN 45
46 #define LIGHTWEIGHT_SERVO_CHANNEL_C_PIN 44
47 #elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) || defined (__AVR_ATmega328PB__)
48 #define LIGHTWEIGHT_SERVO_CHANNEL_A_PIN 9
49 #define LIGHTWEIGHT_SERVO_CHANNEL_B_PIN 10
52 void initLightweightServoPins();
53 void checkAndInitLightweightServoPin(uint8_t aPin);
54 void deinitLightweightServoPin(uint8_t aPin);
56 int writeLightweightServoPin(
int aDegree, uint8_t aPin,
bool aUpdateFast =
false);
58 void writeMicrosecondsLightweightServoPin(
int aMicroseconds, uint8_t aPin,
bool aUpdateFast =
false,
bool aDoAutoInit =
true);
60 void setLightweightServoPulseMicrosFor0And180Degree(
int aMicrosecondsForServo0Degree,
int a180DegreeValue);
61 void setLightweightServoRefreshRate(
unsigned int aRefreshPeriodMicroseconds);
64 int DegreeToMicrosecondsLightweightServo(
int aDegree);
65 int MicrosecondsToDegreeLightweightServo(
int aMicroseconds);
67 #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) || defined (__AVR_ATmega328PB__)
68 #define ISR1_COUNT_FOR_20_MILLIS ISR_COUNT_FOR_20_MILLIS
69 #define ISR1_COUNT_FOR_2_5_MILLIS ISR_COUNT_FOR_2_5_MILLIS
75 void initLightweightServoPin9And10() __attribute__ ((deprecated ("Renamed to initLightweightServoPins()")));
76 void initLightweightServoPin9();
77 void initLightweightServoPin10();
78 void initLightweightServoPin9_10(
bool aUsePin9,
bool aUsePin10);
79 void deinitLightweightServoPin9_10(
bool aUsePin9,
bool aUsePin10);
81 void write9(
int aDegree,
bool aUpdateFast = false);
82 void writeMicroseconds9(
int aMicroseconds,
bool aUpdateFast = false);
83 void writeMicroseconds9Direct(
int aMicroseconds);
85 void write10(
int aDegree,
bool aUpdateFast = false);
86 void writeMicroseconds10(
int aMicroseconds,
bool aUpdateFast = false);
87 void writeMicroseconds10Direct(
int aMicroseconds);
89 int writeLightweightServo(
int aDegree,
bool aUsePin9,
bool aUpdateFast = false);
90 void writeMicrosecondsLightweightServo(
int aMicroseconds,
bool aUsePin9,
bool aUpdateFast = false);
91 #endif // Old and fast ATmega328 functions
93 class LightweightServo {
95 uint8_t attach(
int aPin);
96 uint8_t attach(
int aPin,
int aMicrosecondsForServo0Degree,
int aMicrosecondsForServo180Degree);
98 void write(
int aTargetDegreeOrMicrosecond);
99 void writeMicroseconds(
int aTargetMicrosecond);
104 int MicrosecondsForServo0Degree = 544;
105 int MicrosecondsForServo180Degree = 2400;
106 uint8_t LightweightServoPin;
109 #endif // defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) || defined (__AVR_ATmega328PB__) || defined(__AVR_ATmega2560__)
120 #endif // _LIGHTWEIGHT_SERVO_H