"PWM" control of digitalWrite

Hello All,

Say I am essentially bit banging an output, turning digitalWrite on and off at specific intervals creating 600hz on the output. I would use analogWrite but the mega I'm using only produces ~490hz or ~ 1000hz(the closest in range but still not what I need) hence the bit banging.

So anyway, I have a function which toggles the circuit(led) on/ff at 600hz at 100% duty cycle using no delays, only millis/micros. Could I create a function that we will call lets say void outA() and have it call(at varying "duty cycles" void outB() which toggles the led circuit. I'm sure this is clear as mud so need be I will try to clarify. Thanks,

Bill

If you study the Atmel datasheet you will see that you can change the frequency used for PWM.

...R

Billdefish: So anyway, I have a function which toggles the circuit(led) on/ff at 600hz at 100% duty cycle using no delays, only millis/micros. Could I create a function that we will call lets say void outA() and have it call(at varying "duty cycles" void outB() which toggles the led circuit.

Sure. Take your "function which toggles the circuit(led) on/ff at 600hz at 100% duty cycle" and name it "outA()". Take the part that toggles the LED circuit and put that in a separate function called "outB()". Modify outA() to allow you to vary the duty cycle. Call outA() very frequently.

When you say "600hz at 100% duty cycle" do you mean: On for 1/600th of a second and off for no time at all (600Hz, 100% duty cycle) On for 1/600th of a second and off for 1/600th of a second (300Hz, 50% duty cycle) On for 1/1200th of a second and off for 1/1200th of a second (600Hz, 50% duty cycle)

as said @Robin2, frequency PWM you are controlled via registers and you can set it within certain values ​​. Both are on the datasheet as you set it as calculate it.

To emulate a software PWM is quite simple , you have to use a timer that will call the ISR in comparison , in the same isr will set the ' OCR respectively for times on and off . So when you want to use frequencies vary widely .

I 'm working on an algorithm to 20 in a single PWM timer .

Robin2,

I assumed the frequency available on the avr's were set by the scalars, at around 122,490, 1000, etc as measured on my scope. Now with that said, I did find a site with some useful info on how to set the freq during setup but, like I said, they are "preset" and depending on the scalar used they mimic the aforementioned freq.

johnwasser, I think you hit the nail on the head. That makes sense. I currently have on and off set the same. So by varying the the on time in outA() it will change the duty cycle. Will that change the frequency as well or only the duty cycle.

I am operating a hydraulic pressure control solenoid(controlled bleed pressure control) and the recommended freq is ~600hz with prescribed duty cycle between 60%-0% during normal operation and would prefer to stay within these bounds.

vbextreme,

My personal preference is not to use interrupts but no real reasoning behind it beside lack of experience with them. I've managed pretty well without them thus far but I'm sure eventually I will find a need/use for them.

Thank you all for giving a minute of your time,

Bill

Billdefish: My personal preference is not to use interrupts but no real reasoning behind it beside lack of experience with them. I've managed pretty well without them thus far but I'm sure eventually I will find a need/use for them.

I'm usually the guy that's saying, "there's no way you need an interrupt to do this". But in this case I'm quite the opposite. I don't think anything could possibly more warrant using an interrupt.

Billdefish: I assumed the frequency available on the avr's were set by the scalars, at around 122,490, 1000, etc as measured on my scope. Now with that said, I did find a site with some useful info on how to set the freq during setup but,

Have you studied the Atmel datasheet?

...R

I would be interested to know why you need 600Hz for LEDs and why 490Hz will not do?

@Robin2,

No, I haven't studied the datasheet like I should have. I have skimmed over it, guess I will have a look again with more attention.

@stowite,

The end project is not to control leds but to control a solenoid. The freq is suggested by the manufacturer for optimum performance. I could probably get by with using 490hz but I would like to stay within their range. It's not a safety critical issue per se, but it could be detrimental to the system which it will operate in.

Delta_G,

Could you provide me an example using interrupts? As I mentioned I have no experience with them so any nudge in the "right" direction is greatly appreciated.

Thank you all,

Bill

This instructable seems informative. Let us know if you hit any snags.

http://www.instructables.com/id/Arduino-Timer-Interrupts/

And don't be afraid to consult the Holy Datasheet.

Billdefish: No, I haven't studied the datasheet like I should have. I have skimmed over it, guess I will have a look again with more attention.

When you need to stray from the standard Arduino way of doing things careful study of the datasheet is essential IMHO.

...R