Open Kin9L opened 3 years ago
// (CRYSTAL_FREQ / 1000 / TICKS_PER_CYCLE * PERIOD / PRESCALE_FACTOR )
// (CYCLES_PER_PERIOD / MAX_SERVOS)
// (1000 1000 / CRYSTAL_FREQ PRESCALE_FACTOR * TICKS_PER_CYCLE)
// (CRYSTAL_FREQ / 1000 / 1000 / PRESCALE_FACTOR)
Can you should me these macro define in your header, if you change the crystal frequence, you need to modify them The method to calc has been remarked above the variable definition.
Timer2Servo.h
// CYCLES_PER_PERIOD // (CRYSTAL_FREQ / 1000 / TICKS_PER_CYCLE * PERIOD / PRESCALE_FACTOR )
// (CYCLES_PER_PERIOD / MAX_SERVOS)
// (1000 1000 / CRYSTAL_FREQ PRESCALE_FACTOR * TICKS_PER_CYCLE)
// (CRYSTAL_FREQ / 1000 / 1000 / PRESCALE_FACTOR)
Timer2ServoPwm.cpp // compensation ticks to trim adjust for digitalWrite delays
// trim to prevent OCRA interruption miss
// cycles revise for period of 20ms
// trim to prevent OCRA interruption miss for period revise
// ticks revise for period of 20ms
// trim to not miss the first servo interruption of the next period
I changed these macro defines in Timer2ServoPwm.h. Besides, I also change macro defines in Timer2ServoPwm.cpp. Now, it seems to work now. But I hadn't used the oscilloscope to measure its accuracy. If you have the oscilloscope, you can test it.
Timer2Servo.h
define CRYSTAL_FREQ 8000000 // 16M
define TICKS_PER_CYCLE 256
define MAX_SERVOS 7 // 20ms / 2.5ms = 8
define INVALID_SERVO 255
// CYCLES_PER_PERIOD // (CRYSTAL_FREQ / 1000 / TICKS_PER_CYCLE * PERIOD / PRESCALE_FACTOR )
define CYCLES_PER_PERIOD 78 // 16M: 156.25
// (CYCLES_PER_PERIOD / MAX_SERVOS)
define CYCLES_PER_SERVO 10
// (1000 1000 / CRYSTAL_FREQ PRESCALE_FACTOR * TICKS_PER_CYCLE)
define TIME_PER_CYCLE 256 // unit: us 16
// (CRYSTAL_FREQ / 1000 / 1000 / PRESCALE_FACTOR)
define TICKS_PER_MICROSECOND 1
Timer2ServoPwm.cpp // compensation ticks to trim adjust for digitalWrite delays
define TRIM_PULSE_TICK 3
// trim to prevent OCRA interruption miss
define TRIM_TICKS 16 // + 8
// cycles revise for period of 20ms
define PERIOD_REVISE_CYCLES 1 // 10
// trim to prevent OCRA interruption miss for period revise
define PERIOD_REVISE_TRIM 16
// ticks revise for period of 20ms
define PERIOD_REVISE_TICKS 10 // 230
// trim to not miss the first servo interruption of the next period
define TCNT2_TRIM 5
I changed these macro defines in Timer2ServoPwm.h. Besides, I also change macro defines in Timer2ServoPwm.cpp. Now, it seems to work now. But I hadn't used the oscilloscope to measure its accuracy. If you have the oscilloscope, you can test it.
hah, congratulations you've made it yourself. I feel sorry that my program don't support easy change frequence now, for the reason of accuracy, thank you for your experience.
Hi, I am trying to run this library with 8Mhz crystal. I have been studying it for several days. But I always failed to run with 8Mhz crystal, can you give me some suggestions?