Welcome! Log In Create A New Profile

Advanced

Need some help with writing code for PWM

Posted by BackEMF 
Need some help with writing code for PWM
August 23, 2014 10:31PM
I'm looking for someone who can write a clever PWM control based on Atmel Timer1 (Arduino Uno / Nano)

My coding skills are as good as non existing when it comes to this. I Have had a look at this several times but never got the code to do what i had in mind.

What I like to accomplish is to have a way to have 2 controls One for the Frequency and one for the Pulse Width and if in any way possible they should not influence eachother

When I for instance set a pulse width of 10uS then I should be able to change the frequency withouth the width changes and vice versa.

As input 2 analog input would be fine though 2 rotary encoders are also ok.

Pulse width should be as short as possible and the frequency as high as possible.

And if possible written and commented in a way that is easy to understand so that I eventually may discover the mistakes I made in the past. And also that others have an easy way to build uponth this base functionality.
Re: Need some help with writing code for PWM
August 24, 2014 04:13AM
See [github.com] . heater_init() sets up PWM, heater_set() changes it's value. FAST_PWM chooses between high- and low-frequency PWM.

Without the willingness to learn writing code it's probably pointless to design hardware, because hardware is the trivial part when designing something.

P.S.: the obvious other choice is to design hardware in a way it allows to re-use existing code.

Edited 1 time(s). Last edit at 08/24/2014 04:19AM by Traumflug.


Generation 7 Electronics Teacup Firmware RepRap DIY
     
Re: Need some help with writing code for PWM
August 24, 2014 07:18AM
Thanks,

Hmm i see the 644 has more timers on board.

BTW.

In my experiments i did this:


void setup() {
pinMode(9, OUTPUT); //OC1A
pinMode(10, OUTPUT); //OC1B
}

void loop() {


TCCR1A = bit (COM1A0) | bit (COM1B0); // | bit (WGM10) | bit (WGM11);
OCR1A = 50000; //count to 50001, relative to ZERO!

TCCR1B = bit (CS12) | bit (WGM12);// | bit (WGM13);

OCR1A = 50000; //count to 50001, relative to ZERO!

OCR1B = 10; //count to 10001, relative to ZERO!

}

But as you can see when i change the pulse width then the frequency also changes and vice versa.
I then mapped analog inputs to change the countervalues etc, but that method isn't what i'm after

i did that some time ago

I'll have a look at how you used fastpwm and if that works the same on the 328 chip.
Yea.. I tend to waste time on doing things the hard way hehehe i tried to take a shortcut with my question and yea i've cut the corner a bit rough there.:-)
VDX
Re: Need some help with writing code for PWM
August 24, 2014 05:36PM
... I'm using a timer- or external-interrupt for starting the ON time and then "delayMicroseconds(pulse_high);" with a variable smaller than the timer base for the pulse duration ... this is precise enough for pulsing frequencies of up to 200kHz.


Viktor
--------
Aufruf zum Projekt "Müll-freie Meere" - [reprap.org] -- Deutsche Facebook-Gruppe - [www.facebook.com]

Call for the project "garbage-free seas" - [reprap.org]
Re: Need some help with writing code for PWM
August 27, 2014 07:59PM
Where did you use that? I'd like to take a look at it. How much dutycycle can you still have at 200kHz?
Atmels datasheet is causing a lot of confusion when it comes to Timer1. Nickgammon has a nice tutorial series about timers and it helped me a lot, but still it's hard to get my head around how to actually make a PWM with more control over the actual pulse width and frequency. It looks like your method is something i need to have a closer look at.

Still the frequency changes when you change the pulsewitdh or am I wrong?
VDX
Re: Need some help with writing code for PWM
August 28, 2014 04:13AM
... I'm using it to control the pulse width for a laser, so no other code to perform.

The main routine is empty, so the Arduino only waits for an interrupt and then sets a pin HIGH, waits "delayMicroseconds(nnn)" (where 'nnn' is a value set at reset depending on an analog input), then clears the output pin again ... and waits for the next interrupt.

The smallest delay is 1 microsecond, so with all the interrupt service and pin-setting time it could be around 2ms or 500kHz fastest response.

With a constant frequency as interrupt-input this would result in a harmonic PWM frequency, as long the delay+service-time is smaller then the input step time.

But it's basically reacting on my CNC-controller, so the output 'frequency' is synchrone to the moving speeds ...


Viktor
--------
Aufruf zum Projekt "Müll-freie Meere" - [reprap.org] -- Deutsche Facebook-Gruppe - [www.facebook.com]

Call for the project "garbage-free seas" - [reprap.org]
Sorry, only registered users may post in this forum.

Click here to login