uStepper S
uStepperServo.cpp
Go to the documentation of this file.
1/********************************************************************************************
2* File: uStepperServo.cpp *
3* Version: 2.3.0 *
4* Date: December 27th, 2021 *
5* Authors: Thomas Hørring Olsen *
6* Emil Jacobsen *
7* *
8*********************************************************************************************
9* uStepperServo class *
10* *
11* This file contains the implementation of the class methods, incorporated in the *
12* uStepperServo Arduino library. Currently the servo pulse are only available on pin 3 *
13* of the uStepper.
14*
15* The library is used by instantiating an uStepperServo *
16* object, as follows: *
17* *
18* uStepperServo servo; *
19* *
20* *
21* after instantiation of the object, the object's setup method, should be called within *
22* arduino's setup function: *
23* *
24* example: *
25* *
26* uStepperServo servo; *
27* *
28* void setup() *
29* { *
30* servo.setup(); *
31* } *
32* *
33* The servo pulse widths are normally around 500 us for 0 deg and 2500 us for 180 deg *
34* (default values in this library). These values can be redefined to fit your servos *
35* specifications by calling the setMaximumPulse and SetMinimumPulse functions. *
36* *
37* example: *
38* *
39* uStepperServo servo; *
40* *
41* void setup() *
42* { *
43* servo.setup(); *
44* servo.SetMaximumPulse(2400); *
45* servo.SetMinimumPulse(600); *
46* } *
47*********************************************************************************************
48* (C) 2021 *
49* *
50* uStepper ApS *
51* www.ustepper.com *
52* administration@ustepper.com *
53* *
54* The code contained in this file is released under the following open source license: *
55* *
56* Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International *
57* *
58* The code in this file is provided without warranty of any kind - use at own risk! *
59* neither uStepper ApS nor the author, can be held responsible for any damage *
60* caused by the use of the code contained in this file ! *
61* *
62********************************************************************************************/
73#include <uStepperServo.h>
74
76{
77
78}
79
81{
82 this->setMaximumPulse(2500);
83 this->setMinimumPulse(500);
84
85 TCCR4A = (1 << 1) | (1 << 5) | (1 << 4); //WGM41 = 1, VGM40 = 0 , Set when up counting, clear when down counting (COM4B0 = 1, COM4B1 = 1)
86 TCCR4B = (1 << 4) | (1 << 3); //WGM43 = 1, WGM42 = 1 (Fast PWM mode, TOP = ICR4)
87 ICR4 = 39850;
88 TIMSK4 = 0; //enable overflow and OCA interrupts
89 this->write(0);
90 TCCR4B |= (1 << 1); //Enable clock at prescaler 8. 16MHz/8 = 2MHz/40000 = 50Hz Servo Pulse frequency
91 DDRD |= (1 << 2);
92 PORTD |= (1 << 2);
93}
94
96{
97 min16 = (uint16_t)(us * TICKSTOUS);
98}
99
101{
102 max16 = (uint16_t)(us * TICKSTOUS);
103}
104
105void uStepperServo::write(int angleArg)
106{
107 if ( angleArg < 0) angleArg = 0;
108 if ( angleArg > 180) angleArg = 180;
109 angle = angleArg;
110
111 float scale;
112 scale = (float)(this->max16 - this->min16);
113 scale = scale / 180.0;
114
115 // bleh, have to use longs to prevent overflow, could be tricky if always a 16MHz clock, but not true
116 // That 64L on the end is the TCNT0 prescaler, it will need to change if the clock's prescaler changes,
117 // but then there will likely be an overflow problem, so it will have to be handled by a human.
118 this->pulse = (uint16_t)(scale * (float)angle);
119 OCR4B = TIMERTOP - (this->min16 + pulse);
120}
121
uStepperServo()
Constructor for servo class.
void write(int angleArg)
Specify angle of servo motor.
uint16_t min16
Definition: uStepperServo.h:55
void setMinimumPulse(float us)
Sets the minimum pulse.
void setup(void)
void setMaximumPulse(float us)
Sets the maximum pulse.
uint16_t pulse
Definition: uStepperServo.h:53
uint16_t max16
Definition: uStepperServo.h:57
Function prototypes and definitions for the uStepper Servo library.
#define TICKSTOUS
Definition: uStepperServo.h:42
#define TIMERTOP
Definition: uStepperServo.h:40