Skip to content

Commit 5f86608

Browse files
committed
HW PWM up to [email protected] without breaking micros(), millis() and DigiUSB
Nice to control brushed motors without audible noise
1 parent 126c496 commit 5f86608

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

hardware/digispark/cores/tiny/wiring.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,22 @@
4848
#define MILLISTIMER_OVF_vect MILLISTIMER_(OVF_vect)
4949

5050

51+
#define MS_TIMER_TICK_EVERY_X_CYCLES 1 /* Shall be a within 1, 8, 64, 256 or 1024. (default = 64) If set to 1, HW PWM is around [email protected] with Digispark */
52+
5153
#if F_CPU >= 3000000L
54+
#if !defined(MS_TIMER_TICK_EVERY_X_CYCLES)
5255
#define MillisTimer_Prescale_Index MillisTimer_(Prescale_Value_64)
5356
#define MillisTimer_Prescale_Value (64)
5457
#define ToneTimer_Prescale_Index ToneTimer_(Prescale_Value_64)
5558
#define ToneTimer_Prescale_Value (64)
59+
#else
60+
#define Prescaler_Value(Val) PRESCALER_VALUE(Val)
61+
#define PRESCALER_VALUE(Val) Prescale_Value_##Val
62+
#define MillisTimer_Prescale_Index MillisTimer_(Prescaler_Value(MS_TIMER_TICK_EVERY_X_CYCLES))
63+
#define MillisTimer_Prescale_Value (MS_TIMER_TICK_EVERY_X_CYCLES)
64+
#define ToneTimer_Prescale_Index ToneTimer_(Prescaler_Value(MS_TIMER_TICK_EVERY_X_CYCLES))
65+
#define ToneTimer_Prescale_Value (MS_TIMER_TICK_EVERY_X_CYCLES)
66+
#endif
5667
#else
5768
#define MillisTimer_Prescale_Index MillisTimer_(Prescale_Value_8)
5869
#define MillisTimer_Prescale_Value (8)
@@ -140,7 +151,11 @@ unsigned long micros()
140151

141152
SREG = oldSREG;
142153

154+
#if (MillisTimer_Prescale_Value >= clockCyclesPerMicrosecond())
143155
return ((m << 8) + t) * (MillisTimer_Prescale_Value / clockCyclesPerMicrosecond());
156+
#else
157+
return ((m << 8) + t) / (clockCyclesPerMicrosecond() / MillisTimer_Prescale_Value);
158+
#endif
144159
}
145160

146161
void delay(unsigned long ms)

0 commit comments

Comments
 (0)