Skip to content

Commit 0bdf4ba

Browse files
committed
library Servo added, but now the TIM1_CC interrupt needs to be enabled by hand in stm8s_it.h
1 parent f464742 commit 0bdf4ba

12 files changed

Lines changed: 859 additions & 0 deletions

File tree

docs/api/Servo.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Servo Library
2+
3+
This library can control a great number of servos. It makes careful use
4+
of timers: the library can control 12 servos using only 1 timer.
5+
Derived from the Arduino Servo library v1.8.0.
6+
7+
8+
This library has a slightly diffent user interface than the usual singleton
9+
libraries. This allows for handling more than one servo per Sketch but it
10+
requires some more attention when porting an existing sketch from C++ to C.
11+
12+
Each Servo instance is identified by a channel-ID. In order to match the C++
13+
class constructor syntax it is defined as a type `Servo`.
14+
15+
This channel-ID is used as a file-descriptor-like value and need to be
16+
passed to all API functions except for `Servo_attach()`. This function
17+
claimes the next free channel number.
18+
19+
## Example
20+
21+
Read a potentiometer on analog input 0 and set a servo pulse length between
22+
1000us and 2023us:
23+
```c
24+
#include <Arduino.h>
25+
#include <Servo.h>
26+
27+
Servo myservo; // just a simple unsigned char to hold the channel-ID
28+
29+
int val; // variable to read the value from the analog pin
30+
31+
void setup() {
32+
myservo = Servo_attach(9); // attaches the servo on pin 9, returns channel-ID
33+
}
34+
35+
void loop() {
36+
val = analogRead(0); // reads the value of the potentiometer
37+
Servo_write(myservo, val+1000);// sets the servo position
38+
delay(15);
39+
}
40+
```
41+
42+
43+
Original Arduino C++-Sytax:
44+
```c
45+
#include <Servo.h>
46+
47+
Servo myservo; // create servo object to control a servo
48+
49+
int val; // variable to read the value from the analog pin
50+
51+
void setup() {
52+
myservo.attach(9); // attaches the servo on pin 9 to the servo object
53+
}
54+
55+
void loop() {
56+
val = analogRead(0); // reads the value of the potentiometer
57+
myservo.write(val+1000); // sets the servo position
58+
delay(15);
59+
}
60+
61+
```
62+
63+
64+
65+
## API
66+
67+
68+
data type `Servo`: A type definition for a simple unsigned char to hold the
69+
channel number returned my `Servo_attach()`. Needed for every servo. Syntax
70+
identical to the Arduino class constructor.
71+
72+
Arduino syntax |sduino syntax
73+
-------------------- |---------------------
74+
`Servo myservo;` |`Servo myservo;`
75+
`myservo.attach(pin);` |`myservo = Servo_attach(pin);`
76+
`myservo.attach(pin,min,max);` |`Servo_attach_minmax(pin,min,max);`
77+
`myservo.detach();` |`Servo_detach(byte channel);`
78+
`myservo.write(val);` |`Servo_write(myservo, val);`
79+
`myservo.writeMicroseconds(val);`|`Servo_writeMicroseconds(myservo, val);`
80+
`val = myservo.read();` |`val = Servo_read(myservo);`
81+
`val = myservo.readMicroseconds();`|`val = Servo_readMicroseconds(myservo);`
82+
`myservo.attached()` |`Servo_attached(myservo);`
83+
84+
85+
86+
`uint8_t Servo_attach(int pin);`
87+
attach the given pin to the next free channel, sets pinMode, returns channel
88+
number or 0 if failure.
89+
90+
`uint8_t Servo_attach_minmax(int pin, int min, int max);`
91+
as above but also sets min and max values for writes.
92+
93+
`void Servo_detach(byte channel);`
94+
95+
`void Servo_write(byte channel, int value);` if value is < 200 it is treated
96+
as an angle and scaled according the minimum and maximum pulsewidth defined
97+
using the attach() function earlier, otherwise as pulse width in
98+
microseconds, unscaled.
99+
100+
`void Servo_writeMicroseconds(byte channel, int value);`
101+
Write pulse width in microseconds, unscaled.
102+
103+
`int Servo_read(byte channel);`
104+
returns current pulse width as an angle between 0 and 180 degrees.
105+
106+
`int Servo_readMicroseconds(byte channel);`
107+
returns current pulse width in microseconds for this servo (was read_us() in
108+
first Arduino release).
109+
110+
`bool Servo_attached(byte channel);`
111+
return true if this servo is attached, otherwise false .
112+
113+
114+
## Relationship between PWM/analog output and Servo output
115+
116+
It is not possible to use a timer for PWM and the Servo Library at the same
117+
time. Since this library currently uses timer1, the PWM function
118+
(`analogWrite()`) is disabled for the pins connected to timer1 (for the
119+
STM8S103 this is pin PC3 and PC4 or digital pin 5 and 6).
120+
121+
Pins connected to timer2 (PA3, PD3, PD4, digital pin 2,12,13) are still
122+
usable for PWM output.
123+
124+
125+
126+
127+
## Possible improvements
128+
129+
130+
### A more sophisticated pseudo-OO API
131+
132+
Define a set of preprocessor macros that more OO-like definitions like this
133+
become possible:
134+
135+
Current syntax |OO-like syntax |Arduino syntax
136+
-------------------- |--------------------- |--------------
137+
myservo = Servo_attach(pin); |myservo_attach(pin); |myservo.attach(pin);
138+
Servo_write(myservo, val); |myservo_write(val); |myservo_write(val);
139+
val = Servo_read(myservo); |val = myservo_read(); |val = myservo_read();
140+
141+
142+
### Optimizing the handle_interrupts() function
143+
144+
SDCC compiles this function very inefficiently. The code size is around 500
145+
Bytes, and it is executed as part of the CC interrupt routine. Expected CPU
146+
load for a full servo group of 12 servos is approx. 2%.
147+
148+
(Calculated for 16MHz CPU clock, 13 interrupts every 20ms = 650
149+
interrupts/sec, approx. 500 clock cycles each)
150+
151+
152+
### Using more than one CC channel per timer
153+
154+
it might be possible to use all capture+compare (CC) channels of one timer
155+
at the same time, attaching one servo group to each CC-channel. This way it
156+
would be possible to serve up to 48 servos using the four CC-channels of
157+
timer TIM1. Monitoring the repetion period might become a little complex, as
158+
it must be ensured that all servos on all channels have finshed before.
159+
160+

docs/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ handle more than one stepper per Sketch.
281281
[API description](api/Stepper.html)
282282

283283

284+
### Servo
285+
This library can control a great number of servos. It makes careful use
286+
of timers: the library can control 12 servos using only 1 timer.
287+
[API description](api/Servo.html)
288+
289+
284290

285291

286292
## Current status and to-do list
@@ -299,6 +305,7 @@ SPI: working, no interrupt support
299305
LiquidCrystal (for text LCD based on the HD44780 controller)
300306
PCD8544 (for Nokia 5110 type displays)
301307
[Stepper](api/Stepper.html) (multi-instance design for more than one stepper at a time)
308+
[Servo](api/Servo.html) (multi-instance design for more than one servo at a time)
302309

303310
#### implemented and partly working
304311
Wire/I2C
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
= Servo Library for Arduino =
2+
3+
This library allows an Arduino board to control RC (hobby) servo motors.
4+
5+
For more information about this library please visit us at
6+
http://www.arduino.cc/en/Reference/Servo
7+
8+
== License ==
9+
10+
Copyright (c) 2013 Arduino LLC. All right reserved.
11+
Copyright (c) 2009 Michael Margolis. All right reserved.
12+
13+
This library is free software; you can redistribute it and/or
14+
modify it under the terms of the GNU Lesser General Public
15+
License as published by the Free Software Foundation; either
16+
version 2.1 of the License, or (at your option) any later version.
17+
18+
This library is distributed in the hope that it will be useful,
19+
but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21+
Lesser General Public License for more details.
22+
23+
You should have received a copy of the GNU Lesser General Public
24+
License along with this library; if not, write to the Free Software
25+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Controlling a servo position using a potentiometer (variable resistor)
3+
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
4+
5+
modified on 8 Nov 2013
6+
by Scott Fitzgerald
7+
http://www.arduino.cc/en/Tutorial/Knob
8+
modified to C syntax on 30 Jan 2017 for use with the sdunio environment
9+
by Michael Mayer
10+
*/
11+
12+
#include <Arduino.h>
13+
#include <Servo.h>
14+
15+
Servo myservo; // create servo object to control a servo
16+
17+
int potpin = 0; // analog pin used to connect the potentiometer
18+
int val; // variable to read the value from the analog pin
19+
20+
void setup() {
21+
myservo = Servo_attach(9); // attaches the servo on pin 9 to the servo object
22+
}
23+
24+
#define myservo_write(V) Servo_write(myservo,V)
25+
26+
void loop() {
27+
28+
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
29+
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
30+
// Servo_write(myservo,val); // sets the servo position according to the scaled value
31+
myservo_write(val); // sets the servo position according to the scaled value
32+
delay(15); // waits for the servo to get there
33+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOARD_TAG = stm8sblue
2+
3+
include ../../../../../../../sduino.mk
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BOARD_TAG = stm8sblue
2+
3+
include ../../../../../../../sduino.mk
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Sweep
2+
by BARRAGAN <http://barraganstudio.com>
3+
This example code is in the public domain.
4+
5+
modified 8 Nov 2013
6+
by Scott Fitzgerald
7+
http://www.arduino.cc/en/Tutorial/Sweep
8+
modified to C syntax on 30 Jan 2017 for use with the sdunio environment
9+
by Michael Mayer
10+
*/
11+
12+
#include <Arduino.h>
13+
#include <Servo.h>
14+
15+
Servo myservo; // create servo object to control a servo
16+
// twelve servo objects can be created on most boards
17+
18+
int pos = 0; // variable to store the servo position
19+
20+
void setup() {
21+
myservo = Servo_attach(9); // attaches the servo on pin 9 to the servo object
22+
}
23+
24+
void loop() {
25+
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
26+
// in steps of 1 degree
27+
Servo_write(myservo,pos); // tell servo to go to position in variable 'pos'
28+
delay(15); // waits 15ms for the servo to reach the position
29+
}
30+
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
31+
Servo_write(myservo,pos); // tell servo to go to position in variable 'pos'
32+
delay(15); // waits 15ms for the servo to reach the position
33+
}
34+
}
35+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#######################################
2+
# Syntax Coloring Map Servo
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Servo KEYWORD1 Servo
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
attach KEYWORD2
15+
detach KEYWORD2
16+
write KEYWORD2
17+
read KEYWORD2
18+
attached KEYWORD2
19+
writeMicroseconds KEYWORD2
20+
readMicroseconds KEYWORD2
21+
22+
#######################################
23+
# Constants (LITERAL1)
24+
#######################################
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Servo
2+
version=1.1.2
3+
author=Michael Margolis, Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Allows Arduino/Genuino boards to control a variety of servo motors.
6+
paragraph=This library can control a great number of servos.<br />It makes careful use of timers: the library can control 12 servos using only 1 timer.<br />On the Arduino Due you can control up to 60 servos.<br />
7+
category=Device Control
8+
url=http://www.arduino.cc/en/Reference/Servo
9+
architectures=stm8

0 commit comments

Comments
 (0)