Skip to content

Commit 74a12d0

Browse files
Adminiuscmaglie
authored andcommitted
changes PWM resolution to 16bit for all timers
PWM frequency 732 Hz
1 parent b849525 commit 74a12d0

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

cores/arduino/wiring_analog.c

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ static void syncDAC() {
4242
}
4343

4444
// Wait for synchronization of registers between the clock domains
45-
static __inline__ void syncTC_8(Tc* TCx) __attribute__((always_inline, unused));
46-
static void syncTC_8(Tc* TCx) {
47-
while (TCx->COUNT8.STATUS.bit.SYNCBUSY);
45+
static __inline__ void syncTC_16(Tc* TCx) __attribute__((always_inline, unused));
46+
static void syncTC_16(Tc* TCx) {
47+
while (TCx->COUNT16.STATUS.bit.SYNCBUSY);
4848
}
4949

5050
// Wait for synchronization of registers between the clock domains
@@ -212,7 +212,7 @@ void analogWrite(uint32_t pin, uint32_t value)
212212

213213
if ((attr & PIN_ATTR_PWM) == PIN_ATTR_PWM)
214214
{
215-
value = mapResolution(value, _writeResolution, 8);
215+
value = mapResolution(value, _writeResolution, 16);
216216

217217
uint32_t tcNum = GetTCNumber(pinDesc.ulPWMChannel);
218218
uint8_t tcChannel = GetTCChannelNumber(pinDesc.ulPWMChannel);
@@ -254,37 +254,31 @@ void analogWrite(uint32_t pin, uint32_t value)
254254
// -- Configure TC
255255
Tc* TCx = (Tc*) GetTC(pinDesc.ulPWMChannel);
256256
// Disable TCx
257-
TCx->COUNT8.CTRLA.bit.ENABLE = 0;
258-
syncTC_8(TCx);
259-
// Set Timer counter Mode to 8 bits, normal PWM, prescaler 1/256
260-
TCx->COUNT8.CTRLA.reg |= TC_CTRLA_MODE_COUNT8 | TC_CTRLA_WAVEGEN_NPWM | TC_CTRLA_PRESCALER_DIV256;
261-
syncTC_8(TCx);
257+
TCx->COUNT16.CTRLA.bit.ENABLE = 0;
258+
syncTC_16(TCx);
259+
// Set Timer counter Mode to 16 bits, normal PWM
260+
TCx->COUNT16.CTRLA.reg |= TC_CTRLA_MODE_COUNT16 | TC_CTRLA_WAVEGEN_NPWM;
261+
syncTC_16(TCx);
262262
// Set the initial value
263-
TCx->COUNT8.CC[tcChannel].reg = (uint8_t) value;
264-
syncTC_8(TCx);
265-
// Set PER to maximum counter value (resolution : 0xFF)
266-
TCx->COUNT8.PER.reg = 0xFF;
267-
syncTC_8(TCx);
263+
TCx->COUNT16.CC[tcChannel].reg = (uint32_t) value;
264+
syncTC_16(TCx);
268265
// Enable TCx
269-
TCx->COUNT8.CTRLA.bit.ENABLE = 1;
270-
syncTC_8(TCx);
266+
TCx->COUNT16.CTRLA.bit.ENABLE = 1;
267+
syncTC_16(TCx);
271268
} else {
272269
// -- Configure TCC
273270
Tcc* TCCx = (Tcc*) GetTC(pinDesc.ulPWMChannel);
274271
// Disable TCCx
275272
TCCx->CTRLA.bit.ENABLE = 0;
276273
syncTCC(TCCx);
277-
// Set prescaler to 1/256
278-
TCCx->CTRLA.reg |= TCC_CTRLA_PRESCALER_DIV256;
279-
syncTCC(TCCx);
280-
// Set TCx as normal PWM
274+
// Set TCCx as normal PWM
281275
TCCx->WAVE.reg |= TCC_WAVE_WAVEGEN_NPWM;
282276
syncTCC(TCCx);
283277
// Set the initial value
284278
TCCx->CC[tcChannel].reg = (uint32_t) value;
285279
syncTCC(TCCx);
286-
// Set PER to maximum counter value (resolution : 0xFF)
287-
TCCx->PER.reg = 0xFF;
280+
// Set PER to maximum counter value (resolution : 0xFFFF)
281+
TCCx->PER.reg = 0xFFFF;
288282
syncTCC(TCCx);
289283
// Enable TCCx
290284
TCCx->CTRLA.bit.ENABLE = 1;
@@ -293,8 +287,8 @@ void analogWrite(uint32_t pin, uint32_t value)
293287
} else {
294288
if (tcNum >= TCC_INST_NUM) {
295289
Tc* TCx = (Tc*) GetTC(pinDesc.ulPWMChannel);
296-
TCx->COUNT8.CC[tcChannel].reg = (uint8_t) value;
297-
syncTC_8(TCx);
290+
TCx->COUNT16.CC[tcChannel].reg = (uint32_t) value;
291+
syncTC_16(TCx);
298292
} else {
299293
Tcc* TCCx = (Tcc*) GetTC(pinDesc.ulPWMChannel);
300294
TCCx->CTRLBSET.bit.LUPD = 1;

0 commit comments

Comments
 (0)