Go Down

Topic: PWM help required (Read 213 times) previous topic - next topic

User_name_1

My application needs me to generate a 32 KHz PWM whereas the Arduino Mega generates 490 Hz (pins 4 and 13: 980 Hz). How would I go about changing the frequency of the PWM?

Once that is set and I type in a value for analogWrite(), a PWM corresponding to that duty cycle will be generated. I will be working with the analog value 128 to begin with.

But I want to generate 16 pulses within the Ton (it's okay if these pulses continue in the Toff also) for my application. If anyone has experience in this, all help will be appreciated.

DrAzzy

You would have to take over a timer (timers 1, 3, 4, and 5 can all be used - each one with different pins, see datasheet to see which is which - the pins are marked OCRnx where n is the timer number, and x is A/B/C channel) and reconfigure it to run at that frequency, and create a different analogWrite to set the duty cycle, since it won't be out of 255 anymore.


32khz = 32,000 per second, divide 16MHz (clock speed of mega) by that, you get 500 - you need 500 cycles of the system clock per pwm cycle.

So to achieve that, let's set the WGM to 14 (fast PWM with ICRn as TOP), set ICRn to 499 (it starts at 0, so 499 gives 500 cycles), and the prescaler to 1.

so WGM is 0b1110, CS is 0b001,

thus in setup we want to (say using timer1)

TCCR1A=(0<<WGM10)|(1<<WGM11);
TCCR1B=(1<<WGM12)|(1<<WGM13)|(0<<CS12)|(0<<CS11)|(1<<CS10);
ICR1=499;
OCR1A=249; //50%duty cycle
OCR1B=249; //50%duty cycle
OCR1C=249; //50%duty cycle

bitwise or  with (0<<CS12) doesn't do anything, of course, but it's there to remind you that you're setting the values of those bits to zero. Note that we haven't turned on PWM yet

So now let's throw together some quick functions to work with this reconfigured timer:

called with 3 boolean's, one for channel A,B,C (check pinout chart to find which pins have OCR1x on them), sets the COM1x1 bit to connect the output pin to the timer.

void setPWMEnabled(bool chanA, bool chanB, bool chanC){
TCCR1A=(1<<WGM11)|(chanA<<COM1A1)|(chanB<<COM1B1)|(chanC<<COM1C1);
}

and set the duty cycle; this will be an argument between 0 and 499.

setDutyCycleA(int duty) {
OCR1A=duty;
}

Something like that. Code untested, but if wrong, it's pretty close to working. See chapter 17 of the datasheet on Timer1.


I have no idea what you are asking in the second part, though.







ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

J-M-L

have you looked there ?

the first hit for me is "Secrets of Arduino PWM" - definitely worth reading.
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

6v6gt

My application needs me to generate a 32 KHz PWM whereas the Arduino Mega generates 490 Hz (pins 4 and 13: 980 Hz). How would I go about changing the frequency of the PWM?

Once that is set and I type in a value for analogWrite(), a PWM corresponding to that duty cycle will be generated. I will be working with the analog value 128 to begin with.

But I want to generate 16 pulses within the Ton (it's okay if these pulses continue in the Toff also) for my application. If anyone has experience in this, all help will be appreciated.
The first part looks well covered, but for the second part, do you mean that you want to create a carrier frequency of up to 512 kHz , which you will then modulate with the up to 32 kHz signal ? Or what do you mean by the 16 pulses ?

pert

I've merged your cross-posts @User_name_1.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

UKHeliBob

More multiple posts merged

Stop scattering posts about the same thing all over the forum or you risk being banned
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

User_name_1

Sorry about that, I hadn't gone through the guidelines then

I've removed the other posts and will be more careful going ahead

But the second part of my issue hasn't been solved yet, so if someone can help me with that, that would be great

What I am aiming at is that within the duration of Ton (corresponding to analogWrite value 128), I want the square wave to toggle ON OFF and generate a pulse (the time period would be Ton/16)

I hope it is clearer now


aarg

But I want to generate 16 pulses within the Ton (it's okay if these pulses continue in the Toff also) for my application. If anyone has experience in this, all help will be appreciated.
Are you allowed to use external hardware? You could generate both 32kHz and 512kHz on different timers and then use an external logic gate to combine them.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

cattledog

But I want to generate 16 pulses within the Ton(it's okay if these pulses continue in the Toff also) for my application. If anyone has experience in this, all help will be appreciated.
See reply #6 in Nick Gammons Tutorial on Timers for an example of how to use two timers. One timer to generate a carrier frequency and a second to turn it on and off.

Go Up