Bit of Background: I am currently working with my ATX Power Supply and trying to re-purpose it to use its voltage outputs (3.3V, 5V and 12V). But I would like to have a PWM feature with my power supply when I complete it. For this purpose I am thinking to use Attiny85 and use it's fast PWM mode to generate what I need. I need a PWM signal whose duty cycle and frequency can be varied through the use of potentionmeter.

Problem:Link to datasheet Going through the datasheet, I figured out that it is possible to vary the duty cycle as I want through the manipulation of Output Compare Register (OCR). But I can only vary frequency in pre-determined values. Varying the clock prescale factor does the basic job. But what I want is PWM with frequency I can adjust with a potentiometer as I want (within the supported range of course).

Possible way for solution from my study: I came to the CTC mode in which I can vary the frequency by changing the values in OCR. But this would limit the duty cycle varying feature. So, I wonder if there is any way to combine these two modes.I came to this thread where the question deals with varying frequency only but not the duty cycle.

I can do the basic operation of generating the PWM with fixed frequency. Although, I went through the registers involved in generating PWM, I can't scrap any idea to generate the PWM I wish for.

This is basically what my primary problem is. I would love some recommendations from this community about what I could add to the features of my power supply. Thanks for the help.

  • 2
    \$\begingroup\$ If I am following you, you want to use one pot to change both the frequency and duty cycle? Have you considered doing the PWM yourself with an IO instead of using the built-in PWM? It seems you could read the pot with an ADC, then use that value to set the freq. and duty cycle in your PWM code using any algorithm you like (limited by the resolution of the ADC and clock prescaler). \$\endgroup\$ Aug 18 '20 at 17:16
  • 1
    \$\begingroup\$ I would try using an interrupt every timer overflow to change the values of ICR, OCRA and OCRB to reflect the new values you read via ADC on the two pots. \$\endgroup\$ Aug 18 '20 at 17:40
  • 1
    \$\begingroup\$ @Giga-Byte No, don't give up on it. You mentioned above you are stuck with it. (There are much better chips with much better timer mechanisms in them [the B7 timer system in the MSP430, for example.] But that doesn't mean you have to use them -- especially when you can't.) I'm just suggesting that you need some "sit-down" time while you work through the details. If you don't have good peripherals and you are forced to bit-bang as a result, then sit down and examine the "worst case" situation and see if it is achievable. If you use C for this, I don't think the results will be good. That's all. \$\endgroup\$
    – jonk
    Aug 18 '20 at 18:19
  • 1
    \$\begingroup\$ @Giga-Byte If you feel that there are good peripherals, then study them very carefully and work out a way to use them. Again, grab up the worst case scenario you can imagine (often, the highest frequency and when you are moving smoothly between varying duty cycles within it) and on paper work out exactly what you can do, how much variability in the timing of it you are forced to deal with, and ask yourself if that's good enough. Also, can you make changes in the peripheral in such a way that the output "appears" the way you want it to appear as changes are made - glitch free for example? \$\endgroup\$
    – jonk
    Aug 18 '20 at 18:22
  • 1
    \$\begingroup\$ @Giga-Byte Can you accept "oddball" glitches as you scroll around with your knobs? Etc. You really, really need to sit down and use paper. No computer. No simulations. No libraries. No anything. Just you and your brain and the datasheet and your knowledge of instructions and peripherals. If you cannot work it out on paper, then you'll never get it right in code. You need to fully understand every single detail before you write any code, unless that code is to help you answer a question you have that isn't answered in the datasheet. \$\endgroup\$
    – jonk
    Aug 18 '20 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.