PWM : Example 2: Variable frequency and duty cycle PWM #938
Replies: 4 comments 1 reply
-
If you post your whole sketch, that gives you the error, and tell which attiny you are using, people can import your sketch and try it out. |
Beta Was this translation helpful? Give feedback.
-
The timer clocks are derived from the system clock. We don't get as many options for prescalers as we would like. Generally you get options a factor of 4 away, keep the pwm speed within a factor of 2 of 980Hz. There were two important differences between the modern tinyAVR timers and classic tiny85 timer:
megaTinyCore expected pwm speeds:
Target was minimum 490 Hz, under 1900 Hz - that's a frequency range that most MOSFETs can be PWMed at directly, though the top half of that range starts to get dicey if the fet beefy - but my only other option was to drop the prescale all the way down to 256, at which point the PWM would be slow enough to see a flicker. We use a PWM timer period of 255 ticks, meaning period = 254. If you do 256 ticks, not only is the millis math using a TCA much harder, it also ensures that analogWrite() cannot take a 0-255 argument and do what it promises: There are 257 states (on 100%, plus 0 through 255 256ths on) that you're trying to choose from which is impossible with 8 bits. So we took the very strong hint and did 254. TCBs are not used by mTC for generating PWM - they overlap in almost every case with other timers for PWM, plus they are spectacular millis timers when configured in a way that doesn't let them generate PWM. (We default TCA0 for millis on 0-series with only a single TCB, so that that tcb can be used for tone or servo; on 1-series we fob the task off on the unruly TCD0 by default because nobody is likely to take it over, even though it's the worst of the timers for millis, and on 2-series, since they have 2 TCBs, we use one of those as the default millis. |
Beta Was this translation helpful? Give feedback.
-
if 1/4th the 1225 Hz figure would be good, you're in luck! Bits 3:1 – CLKSEL[2:0] Clock Select
Using the defines that toolchain provides:
also demonstrates proper practice of the local variable when accessing a volatile register. The core only sets that register during initialization and during takeOverTCAn so the core shouln't stamp on your toes there. Our general policy on code that exists primarily to expose a peripheral should be in a library unless tight integration with the core is required (as with the ADC) - for example. Event, CCL, Opamp (DxCore) are all effectively independent libraries, whereas the enhanced ADC section isn't because it is intimately linked to normal ADC functionality. So TCA, TCB, and TCD all really ought to have their own library even if all it does is provide enums and and paraphrase a combination of the datasheet and common sense. Except as you start looking, it becomes unavoidably obvious that these are useless without tightly associated code and dear god on total they are incredibly complicated. So nobody has written a library to even do this minimal level of making them more user friendly. But just making minimalist wrapper libraries is a lot of work, Personally, I'd break them down like:
|
Beta Was this translation helpful? Give feedback.
-
Thanks, I have put the above code to the setup() and it works like a charm! |
Beta Was this translation helpful? Give feedback.
-
Hello all,
I am digging into the PWM on the series 1, so far so good, I can program the chips easily thanks to this core!
Used the basic analogWrite() and I am not happy with the freq. I get, for example, on tiny45, 85 it was below around 500Hz, but now, if the same is used , somehow the freq. is 1kHz, hence I am looking into the manual configuration, namely "taking over the TCA0".
Started with the basic example , such as Example 2: Variable frequency and duty cycle PWM, however, I get the compilation error with the following line:
PORTMUX.TCAROUTEA = (PORTMUX.TCAROUTEA & ~(PORTMUX_TCA0_gm)) | PORTMUX_TCA0_PORTC_gc;
Anybody faced the same issue ?
The idea is update the following code with the new PWM setup.
This code runs OK on the AT Tiny 3216 which I am programming via the Arduino UNO general UPDI ... !
And this is the error when trying to compile the Example 2: Variable frequency and duty cycle PWM :
Any ideas ?
Beta Was this translation helpful? Give feedback.
All reactions