Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PIC32 PWM not working?
21-12-2015, 08:18 AM, (This post was last modified: 23-12-2015, 01:37 AM by jwzumwalt.)
#1
PIC32 PWM not working?
I have attempted to get PWM working on the PIC32.
Listed below is a test program that I used connected up
to my o-scope. Despite different values for the frequency,
the frequency does not change.

UPDATE: The analogWrite() function does not allow the frequency to be set.
If you need frequency control, you must use the PWM library.
I updated the Wiki with this information.


Code:
// Olimex Pic32 Micro dev brd w/Pinguino.cc (Ver11) IDE
u8 LED = 10; // LED connected to digital pin 10
u16 count;

void setup() {
 PWM.setFrequency(10000);
 PWM.setDutyCycle( 10, 500 );
}

void loop() {
 analogWrite(LED, 500);
}
----------------
Thanks,
j
wzumwalt
 (\__/)
 (='.'=)
 (")_(")
Reply
21-12-2015, 12:53 PM, (This post was last modified: 21-12-2015, 12:53 PM by pingotg.)
#2
RE: PIC32 PWM not working?
Which board?

Also OS? Pinguino version?

John
Reply
21-12-2015, 08:01 PM, (This post was last modified: 21-12-2015, 08:02 PM by jwzumwalt.)
#3
RE: PIC32 PWM not working?
(21-12-2015, 12:53 PM)pingotg Wrote: Which board?

Also OS? Pinguino version?

John

// Olimex Pic32 Micro dev brd w/Pinguino.cc (Ver11) IDE

I am watching this on my o-scope. The analogWrite width can be
changed but I have not found any way to adjust the frequency.
The frequency for analogWrite seems to be stuck on about 170khz.
I am interested in using it for variable servo control.
----------------
Thanks,
j
wzumwalt
 (\__/)
 (='.'=)
 (")_(")
Reply
21-12-2015, 09:41 PM,
#4
RE: PIC32 PWM not working?
I wonder if it's because you have no delay or the like in your loop?

Maye it's restarting as fast as it can, which would be tens of thousands of times per second - thus the 170kHz.

John
Reply
22-12-2015, 03:24 AM, (This post was last modified: 22-12-2015, 03:28 AM by jwzumwalt.)
#5
RE: PIC32 PWM not working?
(21-12-2015, 09:41 PM)pingotg Wrote: I wonder if it's because you have no delay or the like in your loop?

Maye it's restarting as fast as it can, which would be tens of thousands of times per second - thus the 170kHz.

John

I am not familiar with the analogWrite() (because there is no Pinguino docs),
but a guess the real question is: is the analogWrite frequency hardwired or is it
suppose to be able to be set?

All of this can be done with the PWM.x library but I am trying to familiarize
myself with each Pinguino command and update the Wiki with examples.
For all I know, analogWrite is not intended to be set...
----------------
Thanks,
j
wzumwalt
 (\__/)
 (='.'=)
 (")_(")
Reply
22-12-2015, 10:26 AM, (This post was last modified: 22-12-2015, 10:57 AM by pingotg.)
#6
RE: PIC32 PWM not working?
Try a delay in loop. I think it will work.

Then look at the code to see why...

John
Reply
23-12-2015, 01:36 AM,
#7
RE: PIC32 PWM not working?
(22-12-2015, 10:26 AM)pingotg Wrote: Try a delay in loop.  I think it will work.

Then look at the code to see why...

John

Thanks for the help.

I found out the analogWrite() function does not allow the frequency to be set.
If you need frequency control, you have to use the PWM library or a delay
as you have mentioned.

I updated the Wiki with this information.
----------------
Thanks,
j
wzumwalt
 (\__/)
 (='.'=)
 (")_(")
Reply
23-12-2015, 02:18 AM, (This post was last modified: 23-12-2015, 02:26 AM by pingotg.)
#8
RE: PIC32 PWM not working?
What??? Your code as posted doesn't try to set the frequency with analogWrite.

With the delay did you get the frequency you set?

The fault with your code with no delay was that by repeatedly calling analogWrite you kept aborting the chip's attempt to do what you wanted.

I think the wiki may now be wrong except with an empty loop (which real programs won't have) that does the aborting as described.

John
Reply
23-12-2015, 03:36 AM, (This post was last modified: 23-12-2015, 03:41 AM by jwzumwalt.)
#9
RE: PIC32 PWM not working?
I think we are having miss-communication. The analogWrite is pulse width ONLY - no frequency setting. That was what my entire question was about - "Can the frequency be set?" the answer is "No". I eventually found the answer in the Arduino documentation. For Arduinos, the frequency is hardcoded to 450-900hz (depending on the chip).

Therefore, it is really better suited for voltage control and not pwm as is needed by a servo. The pwm is the best choice for a servo or stepper motor.
----------------
Thanks,
j
wzumwalt
 (\__/)
 (='.'=)
 (")_(")
Reply
23-12-2015, 11:37 AM, (This post was last modified: 23-12-2015, 11:39 AM by pingotg.)
#10
RE: PIC32 PWM not working?
Your code tries to set the frequency but then has a bad tight loop.

With the delay in loop, does the PWM.setFrequency work?

Isn't the 170kHz simply due to your lack of a delay? Your code tries to get 10kHz; does it get that when you have the delay (I suppose of at least a few ms)?

IOW, doesn't everyone wanting to use PWM.setFrequency need a delay? Or, to avoid calling analogWrite often e.g. using something like:

static uchar done = 0;
if (!done) {
analogWrite(...);
done = TRUE;
}

John
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)