[PIC] PWM with pic24fj256da210

Status
Not open for further replies.

damiano

Newbie level 1
Joined
Jul 4, 2014
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
6
hi everybody!
i'm student and i'm at my first experience with pic. my target is to produce a PWM signal with an output compare.
i found an example but i don't understand how i can modified the duty cycle. this is the code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include "p24FJ256DA210.h"

_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & ICS_PGx2 & FWDTEN_OFF)
_CONFIG2(IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_OFF &  FNOSC_PRIPLL & POSCMOD_HS)

// Initialize I/O pins
void IOInit(void)
{
	 TRISDbits.TRISD6 = 1;
	 TRISDbits.TRISD0 = 0;
	 TRISDbits.TRISD13=1;
	 TRISAbits.TRISA7=1;
	 TRISDbits.TRISD7=1;
	 TRISAbits.TRISA6=0;
	 TRISAbits.TRISA3=0;
	 TRISAbits.TRISA2=0;
         TRISEbits.TRISE9=0;
         TRISBbits.TRISB5=0;
	 PORTA= 0X0000;
	 PORTD= 0x0000;
}

void PWM_init(void)

{
	OC1CON1 = 0x0000; // Turn off Output Compare 1 Module
	OC1R = 0x0000; // Initialize Compare Register1 with 0x0000
	OC1RS = 0x0000; // Initialize Secondary Compare Register1 with 0x0000
	OC1CON1bits.OCSIDL =0;
	OC1CON1bits.OCFLT=0;
	OC1CON1bits.OCTSEL=1;
	OC1CON1bits.OCM =0x06; // PWM mode on OCx;Fault pin Disable

         RPOR9bits.RP18R0=0;
         RPOR9bits.RP18R1=1;
         RPOR9bits.RP18R2=0;
         RPOR9bits.RP18R3=0;
         RPOR9bits.RP18R4=1;
         RPOR9bits.RP18R5=0;
}



void Timer_Init(void)

{
	T3CONbits.TON = 0;
	T3CONbits.TSIDL = 1;
	T3CONbits.TGATE=0;
	T3CONbits.TCKPS= 0;
	T3CONbits.TCS=0;
	PR3 = 0x0000;

	IPC1bits.T2IP = 1; // Setup Output Compare 1 interrupt priority
	IFS0bits.T2IF = 0; // Clear Output Compare 1 interrupt flag
	IEC0bits.T2IE = 1; // Enable Output Compare 1 interrupts
}



void __attribute__ ((__interrupt__)) _T3Interrupt(void)
{
	IFS0bits.T3IF = 0; //Clear Timer3 interrupt flag
}



void Delay(void)
{
	int i;
      for(i = 0; i < 0xFFFF; i++);

}


int main(void)


{
	IOInit();
	PWM_init();
	Timer_Init();

	while(1)

	{
		OC1R = 50;
		OC1RS = 50;

		PR3 = 100;

		T3CONbits.TON = 1; //Turn on Timer3
	}
	return 0;
}

somebody can help me?

thanks in advance
 
Last edited by a moderator:

once the PWM is running you load the OC1R register to change the duty cycle

e.g. this is a DC motor control using a PIC24FJ256GB110
Code:
// initialise PWM  - use  System Clock as the timer
void motorPWMinit(int dutyCycle)
{
        OC2CON1=0;
       OC2CON2=0;
       OC2CON2bits.SYNCSEL=0x1F;			// set trigger/sync source is this OCM module
       OC2RS=(int)(1600); 			 		// set PWM period
       OC2R =(int)(1599L*(dutyCycle)/100L); // duty cycle, period - 1 seems to work better
       OC2CON1bits.OCM=7;					// set Center-aligned PWM mode
       OC2CON1bits.OCTSEL=7;	    		// setect System Clock
 }


// set motor PWM - motorPWMinit must have aleady been called
void motorPWM(int dutyCycle)
{
       OC2R =(int)(1599L*(dutyCycle)/100L);
       //printf("motor duty cycle %d %d\n", dutyCycle, (int)(1600L*(dutyCycle)/100L));
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…