PWM WITH PIC32MX460F512L

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
sosoebot
Posts: 1
Joined: Fri Jun 08, 2018 11:16 am

PWM WITH PIC32MX460F512L

Post by sosoebot » Fri Jun 08, 2018 11:25 am

I am trying to Generate PWM signal using PIC32MX460F512L. I want to use it to drive a motor, I am using a frequency of 10KHz. I have written the code but I tested it and it is not working. Please i would appreciate if someone can advice me on how to proceed.

Code: Select all

#include <plib.h>
#include <stdio.h>
#include <stdint.h>
#include "Cerebot.h"
#include "Lcd.h"
#include "config.h"

void initTimer();
void initOC();


int main(void)
{
	Init_Cerebot32MX(); 
	initTimer();
	initOC();

	_CP0_SET_COUNT(0);			// Delay 4 Seconds
	while(_CP0_GET_COUNT()< 4*40000000)
	{
		;
	}
	
	OC1RS = 500;				// Set Duty Cycle to 50%
	TRISG = 0x0000;				// Set PORTG as output

	while(1)
	{
		PORTGbits.RG12 = 0;			// Set pin RG12 as output

	}

} //end main

void initTimer()
{
	T2CONbits.TCKPS = 1; 		// Timer 2 Prescaler of 1:8
	PR2 = 999;					// Period = (PR2+1)*N*12.5ns = 100us
	TMR2 = 0;					// Initial TMR2 count is 0
	T2CONbits.ON = 1;			// Turn on Timer 2
}

void initOC()
{
	OC1CONbits.OCM = 0b110;		// PWM Mode without fault pin
	OC1RS = 250;				// Duty Cycle = OC1RS/(PR2+1) = 25%
	OC1R = 250;					// Initialize before turning OC1 on
	OC1CONbits.ON = 1; 			// Turn on OC1
}

User avatar
phalanx
Non-SFE Guru
Posts: 1962
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PWM WITH PIC32MX460F512L

Post by phalanx » Tue Jun 12, 2018 5:26 am

Off the top of my head, I dont remember if the output compare peripheral overrides the pin direction settings but you may want to try changing your OC1 pin to an output and see if that makes a difference.

-Bill

User avatar
phalanx
Non-SFE Guru
Posts: 1962
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PWM WITH PIC32MX460F512L

Post by phalanx » Tue Jun 12, 2018 5:29 am

Nevermind. I found the section in the family reference manual that says the compare module overrides the TRIS register. I'll keep digging when I free up some time.

-Bill

User avatar
phalanx
Non-SFE Guru
Posts: 1962
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PWM WITH PIC32MX460F512L

Post by phalanx » Tue Jun 12, 2018 6:09 am

Another thing I noticed is your prescaler setting for Timer2 is actually 1:2, not 1:8 like you are expecting. If you were using Timer1, your TCKPS bits would be correct but for Timer2 TCKPS needs to be 0b011 for a 1:8 prescaler. Right now your PWM frequency would be 40KHz.

When you say your PWM is not working, what exactly are you seeing?

-Bill

Post Reply