atp798 / Timer2ServoPwm

Arduino servo driver library base on timer2
MIT License
5 stars 3 forks source link

Timer2ServoPwm for Arduino Pro Mini 3.3v/8Mhz #1

Open Kin9L opened 3 years ago

Kin9L commented 3 years ago

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?

atp798 commented 3 years ago

define CRYSTAL_FREQ 16000000 // 16M

define TICKS_PER_CYCLE 256

define MAX_SERVOS 7 // 20ms / 2.5ms = 8

define INVALID_SERVO 255

// (CRYSTAL_FREQ / 1000 / TICKS_PER_CYCLE * PERIOD / PRESCALE_FACTOR )

define CYCLES_PER_PERIOD 156 // 16M: 156.25

// (CYCLES_PER_PERIOD / MAX_SERVOS)

define CYCLES_PER_SERVO 20

// (1000 1000 / CRYSTAL_FREQ PRESCALE_FACTOR * TICKS_PER_CYCLE)

define TIME_PER_CYCLE 128 // unit: us 16

// (CRYSTAL_FREQ / 1000 / 1000 / PRESCALE_FACTOR)

define TICKS_PER_MICROSECOND 2

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.

Kin9L commented 3 years ago

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.

atp798 commented 3 years ago

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.