![Pololu Zumo Shield For Arduino Скачать руководство пользователя страница 50](http://html1.mh-extra.com/html/pololu/zumo-shield-for-arduino/zumo-shield-for-arduino_user-manual_1574974050.webp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/** Arduino Uno Timer 2 Servo Example
This example code for the Arduino Uno shows how to use Timer 2 on
the ATmega328P to control a single servo.
This can be useful for
people who cannot use the Arduino IDE's Servo library.
For
example, ZumoMotors uses the same timer as the Servo
library (Timer 1), so the two libraries conflict.
The SERVO_PIN macro below specifies what pin to output the
servo on.
This pin needs to be connected to the signal input
line of the servo.
The Arduino's GND needs to be connected to
the ground pin of the servo.
The servo's ground and power pins
need to be connected to an appropriate power supply.
*/
// This line specifies what pin we will use for sending the
// signal to the servo.
You can change this.
#define SERVO_PIN 11
// This is the time since the last rising edge in units of 0.5us.
uint16_t
volatile
servoTime = 0;
// This is the pulse width we want in units of 0.5us.
uint16_t
volatile
servoHighTime = 3000;
// This is true if the servo pin is currently high.
boolean
volatile
servoHigh =
false
;
void
setup()
{
servoInit();
}
void
loop()
{
servoSetPosition(1000);
// Send 1000us pulses.
delay(1000);
servoSetPosition(2000);
// Send 2000us pulses.
delay(1000);
}
// This ISR runs after Timer 2 reaches OCR2A and resets.
// In this ISR, we set OCR2A in order to schedule when the next
// interrupt will happen.
// Generally we will set OCR2A to 255 so that we have an
// interrupt every 128 us, but the first two interrupt intervals
// after the rising edge will be smaller so we can achieve
// the desired pulse width.
ISR(TIMER2_COMPA_vect)
{
// The time that passed since the last interrupt is OCR2A + 1
// because the timer value will equal OCR2A before going to 0.
ser= OCR2A + 1;
static
uint16_t highTimeCopy = 3000;
static
uint8_t interruptCount = 0;
if
(servoHigh)
{
if
(++interruptCount == 2)
{
OCR2A = 255;
}
Pololu Zumo Shield for Arduino User’s Guide
© 2001–2019 Pololu Corporation
8. Controlling a servo
Page 50 of 52