Go Down

Topic: PWM frequency library (Read 82058 times) previous topic - next topic

pwmWriteHR is a good solution, I think.
I'll try that as soon as possible.

I'm ok it's a bit hard to deal with resolution. But things that was not possible, now are, so...

Thanks for taking on your time, it's very helpful.
Mgth : ArduixPL http://www.mgth.fr

ksshhs

#16
Aug 21, 2012, 10:35 am Last Edit: Aug 21, 2012, 10:58 am by ksshhs Reason: 1
Thanks for your reply runnerup, although I will have to call for your help once again as I am new to Arduino but the deadline for my project is fast approaching!! I am trying my best but am still getting compile errors, so I was hoping you could take a look at the source code I have written and with the knowledge that I am using an Arduino Uno and possibly you could spot what is going wrong!

Code: [Select]
# include "PWM.h"

//Initialize Motor 1
int InA1 = 8;
int InB1 = 12;
int PWM1 = 10;
int32_t frequency = 20000;

void setup()


 
   InitTimersSafe();
   bool success = SetPinFrequencySafe(PWM1, frequency);
 
   if(success){
   pinMode (InA1, OUTPUT);
   pinMode (InB1, OUTPUT);
   pinMode (PWM1, OUTPUT);}

}

void loop ()
{
 
 
  {//Motor Start Delay
       delay(1000);
       
   //Motor Direction
       digitalWrite(InA1, HIGH);
       digitalWrite(InB1, LOW); }
     
   
   //Motor Acceleration Control
       {for(int fadeValue = 0 ; fadeValue <= 76; fadeValue +=5){
         pwmWrite(PWM1, fadeValue);
       
         delay(100000); }
       }
       
   //Motor Run Time       
       delay(10000);
       
   //Motor Decceleration Control
       {for(int fadeValue = 76; fadeValue >= 0; fadeValue -=5){
          pwmWrite(PWM1, fadeValue);
         
          delay(100000);
       }
       } 
         
   //Motor Stop Time
       delay(10000);
 
}
 


Here is the error I am now getting. I have installed v4.0.



Many thanks!

dxw00d

Code: [Select]
# include "PWM.h"
There shouldn't be a space between the '#' and include.

runnerup

#18
Aug 21, 2012, 02:50 pm Last Edit: Aug 21, 2012, 03:28 pm by runnerup Reason: 1
dxw00d got it, you are using the preprocessor incorrectly, but I see another bug in your code.
Pin 10 cannot be used for pwm in this library. That means:
Code: [Select]

if(success) //always skips this block, success is always false
{
   pinMode (InA1, OUTPUT);
   pinMode (InB1, OUTPUT);
   pinMode (PWM1, OUTPUT);
}//you have no else statement, you can't catch or deal with the error. Your program will continue on without dealing with this issue.


It is an avoidable property with Arduino's 8 bit timers. You can use pins 2, 3, 6, 7, 8, 11, 12, 44, 45, and 46. Pins 10 and 4 are lost for pwm output (they still work for digital input and output). In your case, since you use delay functions and the safe version of InitTimers(), you should avoid pin 5, since it is connected to timer 0.

also, look at the line

Code: [Select]

delay(100000);

that's an additional 100 seconds per iteration. Are you sure you want to do that?
And just one more thing. It appears you use a lot of curly brackets "{ ... }" where you don't need them. Generally they are used when going into scope, in the case of an if...else and for loop. Although the C language lets you put them in needless places, they will only confuse you and other readers (and potentially create local variable bugs). Your setup() and loop() have the same meaning as the following:

Code: [Select]

void setup()
{

InitTimersSafe();
bool success = SetPinFrequencySafe(PWM1, frequency);

if(success){
pinMode (InA1, OUTPUT);
pinMode (InB1, OUTPUT);
pinMode (PWM1, OUTPUT);
}

}

void loop ()
{
//Motor Start Delay
delay(1000);

//Motor Direction
digitalWrite(InA1, HIGH);
digitalWrite(InB1, LOW);


//Motor Acceleration Control
for(int fadeValue = 0 ; fadeValue <= 76; fadeValue +=5){
pwmWrite(PWM1, fadeValue);
delay(100000);
}

//Motor Run Time
delay(10000);

//Motor Decceleration Control
for(int fadeValue = 76; fadeValue >= 0; fadeValue -=5){
pwmWrite(PWM1, fadeValue);
delay(100000);
        } 
         
   //Motor Stop Time
       delay(10000);
}


now which one is easier to read? The only differences are changes to indentation to match the scope, removing useless brackets, and moving the necessary closing brackets on to the next line (note: the indentation does look a bit exaggerated I was using a different editor that deals with tabs differently).
Hopefully this helped, good luck on your project.

rothnic

#19
Sep 23, 2012, 07:30 pm Last Edit: Sep 23, 2012, 09:31 pm by rothnic Reason: 1
This library looks to be a huge help, really appreciate the time. It really is silly that something doesn't exist like this with the stock set of libraries.

I have one issue I'm having getting started with it, that I could use some help on. I'm using a Nano v3, so basically the same thing as an Uno. Pins 5 and 6 associated with Timer 0, Pins 9 and 10 associated with Timer 1, Pins 3 and 11 associated with Timer 2.

I'm building a circuit that will manually control a Honda Insight's hybrid system. This is controlled by 3 control lines, with 2 of them being PWM signals with varying duty cycles. 1 needs to be 20kHz, and the other 2kHz. Therefor, the plan is to use Timer 1 and Timer 2.

When looking for a successful Frequency setting on the Timers, I believe I need to use Pins 10 and 3 on the respective timers. If I use 11 instead of 3, I do not get success for setting the frequency. The issue comes when I make the call to check the resolution of each pin.

Pin 10 (set at 20kHz) returns a resolution of 8.65
Pin 3 (set at 2kHz) returns a resolution of 0.00

If I reverse the pins:

Pin 10 (2kHz) returns a resolution 11.97
Pin 3 (20kHz) returns a resolution of 0.00

What would cause the 0 resolution on Timer 2?

EDIT:
Just checked and Timer 2 provides a frequency of 55555Hz after setting it at 20kHz. I played around with it and when setting it around 15kHz, it outputs almost correctly, except it returns slightly higher than set. Whenever I set it any higher than 15.7kHz, it starts reporting over double of what the set value is. For example, at 15.7kHz, it returns 31620Hz.

A pastebin of the output from your resolution example, with the pin changed to pin 3 and output from Timer2: http://pastebin.com/8vgj6ZBn

I was able to get your frequency method to report 20k, by setting this up manually:

Code: [Select]

  TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(WGM22) | _BV(CS21);
  OCR2A = 50;
  OCR2B = 25;


The resolution is still reported as 0, but reports possible duties of 51.

runnerup

I was able to reproduce the frequency errors for timer 2 and timer 1 (55555 when set to 20000 and 31620 and 15.7K) in Atmel Studio. Sounds like a truncation issue. I was not able to reproduce the resolution errors. I ran the code test code on my Uno and Atmel Studio it looks clean.

I might have something tomorrow afternoon (east coast USA time) for the frequency problem, I will keep looking into the resolution problem.

rothnic


I was able to reproduce the frequency errors for timer 2 and timer 1 (55555 when set to 20000 and 31620 and 15.7K) in Atmel Studio. Sounds like a truncation issue. I was not able to reproduce the resolution errors. I ran the code test code on my Uno and Atmel Studio it looks clean.

I might have something tomorrow afternoon (east coast USA time) for the frequency problem, I will keep looking into the resolution problem.


Thanks, yeah not sure what is going on with the resolution reporting. I'll keep messing with it.

I did find a nice little free logic analyzer that hooks up to an arduino to test this stuff out. I confirmed that I'm seeing 2kHz on Timer1(Pin 10), and 55.556kHz on Timer2(Pin 3). http://i.imgur.com/aRGS8.png

If anyone comes across this and wants to check their PWM signals out, try this: http://letsmakerobots.com/node/31422 . I'm running it no problems with my Mega, but needed to use version 0.9.5 of the analyzer for it to work with the Arduino plugin.

runnerup

#22
Sep 24, 2012, 04:00 am Last Edit: Sep 24, 2012, 04:02 am by runnerup Reason: 1
Yay integer division!
...ehem

I found the bug, I will publish the patch tomorrow afternoon after I have more time to test it.
All 8 bit timers will be set to higher than expected frequencies when set to high frequencies (go figure). I haven't actually checked myself, but according to rothnic the threshold is somewhere between 15KHz and 16KHz.
Due to the nature of the this particular bug, it should not affect PWM duty, only frequency.
Again, I have no idea what is causing that bad resolution reading, my Mega and Uno have no trouble with it.

rothnic


Yay integer division!
...ehem

I found the bug, I will publish the patch tomorrow afternoon after I have more time to test it.
All 8 bit timers will be set to higher than expected frequencies when set to high frequencies (go figure). I haven't actually checked myself, but according to rothnic the threshold is somewhere between 15KHz and 16KHz.
Due to the nature of the this particular bug, it should not affect PWM duty, only frequency.
Again, I have no idea what is causing that bad resolution reading, my Mega and Uno have no trouble with it.


Sounds good.

Right now I have the 2kHz frequency working ok, but there is an issue with the duty cycle. This is on the 16bit Timer, so with it set at 2kHz it returns 4000 from the Timer1_GetTop call. But, if I do a pwmWrite(10, 2000), I get a Duty cycle of 81.6% in this logic analyzer. This makes me doubt the hardware a bit.

I guess in the end I can get it working with a little trial and error, worst case. Looks like pwmWrite(10, 5000) gets me pretty close to 50%.

runnerup

Quote
if I do a pwmWrite(10, 2000), I get a Duty cycle of 81.6% in this logic analyzer


That's because the acceptable range for pwmWrite() is 0 - 255, or one unsigned 8 bit integer. You are truncating that int many times over with that second parameter and getting a junk duty cycle as a result.
pwmWrite() parameters are meant to mimic analogWrite() parameters. If you need more resolution, use pwmWriteHR(). It is described earlier in this thread.

This fix should solve your frequency issues, I still have no answer for the test program resolution issue. My Mega, my Uno, and Atmel studio simulators have no problem with it.

The latest version of the library as of this post is v .05. You can download it here:
https://code.google.com/p/arduino-pwm-frequency-library/downloads/list

rothnic

#25
Sep 24, 2012, 11:31 pm Last Edit: Sep 24, 2012, 11:45 pm by rothnic Reason: 1

Quote
if I do a pwmWrite(10, 2000), I get a Duty cycle of 81.6% in this logic analyzer


That's because the acceptable range for pwmWrite() is 0 - 255, or one unsigned 8 bit integer. You are truncating that int many times over with that second parameter and getting a junk duty cycle as a result.
pwmWrite() parameters are meant to mimic analogWrite() parameters. If you need more resolution, use pwmWriteHR(). It is described earlier in this thread.

This fix should solve your frequency issues, I still have no answer for the test program resolution issue. My Mega, my Uno, and Atmel studio simulators have no problem with it.

The latest version of the library as of this post is v .05. You can download it here:
https://code.google.com/p/arduino-pwm-frequency-library/downloads/list


Ahh, yeah I was thinking about that while I was at work. Forgot to use the HR version. Didn't realize it was 0-255 for lower resolution frequencies as well. I'll try it out and add some results here in a bit.

Thanks again for your efforts.

Update:
Still have the 0 resolution problem on Timer2, and after looking at your code I assume there has to be some offset mismatch specific to this nano for some reason. No big deal though since I am getting the output I need: http://i.imgur.com/iwFcl.png . Both signals behaving as expected.

sinnaro

#26
Nov 27, 2012, 12:08 am Last Edit: Nov 27, 2012, 01:31 am by sinnaro Reason: 1
For those that get here looking for how to change PWM frequency for the Due, you should look here for a solution: http://arduino.cc/forum/index.php/topic,131323.msg989728.html#msg989728

Basically, only pins 6-9 on the Due board are actual HWPWM and the other pins labeled as PWM are timer pins.

You can change the frequency of the PWM pins 6-9 with the following (changes the frequency for all the pins):
Code: [Select]
PWMC_ConfigureClocks(whatever_freq_you_want * 255 , 0, VARIANT_MCK)

I'm not sure how/if individual pin frequencies can be changed.

The same can be accomplished by changing
Code: [Select]
#define PWM_FREQUENCY 1000 for the pins 6-9 and
Code: [Select]
#define TC_FREQUENCY        1000 for the other PWM timer pins. These constants are defined in 'variant.h'.

Hope this helps someone along the road.

Thank you runnerup for this nice piece of work!

I have some questions for you, since I really need to expand this library for the Micro/Leonardo.
So that is what I will do, but maybe you can help me in the right direction.


  • In your estimation, can I better build of of ATimerDefs or BTimerDefs?

  • Do you have any idea how I could implement the difference in structure of the new 8 - 10 - 16 bit pins and fast PWM?

  • I want to change some of the timer--pin assignments for my project. Is that something that you think will work correctly with the construction you used for the library?

  • I will find my way in the end, but can you give me some places to start looking for the registers and stuff?



Any hint is welcome.

Many thanks in advance!
Christophe aka Petit Jean

mxw8950

Thank you so much for this library as it has been fantastic in helping me get a pump driver up and running.

I have a question regarding timers using the 328. I have noticed that the library functions do not allow you to set the frequency on TIMER2A and was wondering if there was a specific reason why the library would not allow you to set this timer as I can't really find a default use for TIMER2A that would interfere with any other arduino function.

Thanks,
-matt

Hi
I'm not quite clear as to these statements:
Quote
Has pin based (timer agnostic) functions
Has functions for getting and setting frequency at the timer level and pin level


I want to use your library to up the frequency of the PWM on my motor control project (motors are noisy) on a Uno. It is a twin motor control so I use pins 5, 6 for one and pins 9,10 for other.
Pins 5, 6 are timer0 and I don't want to mess with that timer.
So the next step would be to change it to pins 3, 11 to use the other timer. This would mean hacking my shield.
And that is why I don't understand the above. Does this mean I can map the PWM output to any pin?

I haven't used your library yet, just want to clear this first.

Thanks.

Go Up