Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

problem in coding use dsPIC33F64MC802

Status
Not open for further replies.

raja.afiq

Newbie level 3
Newbie level 3
Joined
Jul 17, 2013
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
20
i using C to coding it. using mplab x and xc16 as complier. i make a coding to control pwm where i want to set it to 0% pwm. i don't know where is my wrong in my coding. can someone help me with this and tell me what is the problem so i can learn form this. i just new for this microcontroller.

Code:
#include <p33FJ64MC802.h>

_FOSCSEL(FNOSC_FRCPLL)              //set clock for internal OSC with PLL
_FOSC(OSCIOFNC_OFF & POSCMD_NONE)   //no clock output, external OSC disabled
_FWDT(FWDTEN_OFF)                   //disable the watchdog timer
_FICD(JTAGEN_OFF & ICS_PGD1);       //disable JTAG, enable debugging on PGx1 pins

void initpwm(void){
    P1TCON = 0x8000; //free running, time base on, fcy 1:1, run in cpu idle mode
    PWM1CON1bits.PMOD1 = 1;  //Select Independent Output PWM mode
    PWM1CON1bits.PEN1H = 1;  //PWM1H control by PWM module
    PWM1CON1bits.PEN1L = 0;  //PWM1L control as GPIO
    PWM1CON2=0b0000000000000010;
    P1DTCON1=0;
    P1FLTACON=0;
    P1TMR=0;
}

int main (void){
    OSCCON=0x22E0;
    CLKDIV=0;
    // setup internal clock for 80MHz (Fosc) 
    // Fcy=Fosc/2 (Fcy = 40MHz)
    // 7.37/2*43=158.455/2=79.2275MHz
    CLKDIVbits.PLLPRE=0;        // PLLPRE (N2) 0=/2
    PLLFBD=41;                   // pll multiplier (M) = +2
    CLKDIVbits.PLLPOST=0;       // PLLPOST (N1) 0=/2
    initpwm();
    LATBbits.LATB14 = 0;
    TRISBbits.TRISB14 = 0;
    P1TPER=1599;
    //PTPER = (Fcy/(Fpwm*prescaler))-1
    //PTPER = (40M/(25k*1))-1
    //Fpwm is desire freq for motor.
    while (1){
        P1DC1=0;
    }
}
 

Hi, Here is a chunk of code that I used for generating PWM (dsPIC33fj256GP710). OC1RS and OC1R sets the duty cycle desired. You have to do a bit of mathematical calculation to do that. Formula I used for calculating PWM Duty cycle (timer 3 based ) is "OC1R = OC1RS = PR3 - (PR3 * OC3DutyCyc * 0.01);" where OC3DutyCycle is in %age
Code:
T3CON = 0x8000;         // enable TMR3, prescale 1:1, internal clock
    PR3 = PWMperiod;        // set the period in ms
    DutyCyc = PR3/2;        //  50% Duty cycle     
    _T3IF = 0;              // clear interrupt flag
    _T3IE = 1;              // enable TMR3 interrupt

    // 3. set the initial duty cycles
    OC1R = OC1RS = Offset;  // 
    OC2R = OC2RS = Offset;  // 

    // 4. activate the PWM modules 
    OC1CON = 0x000E;        // CH1 and CH2 in PWM mode, TMR3 based
    OC2CON = 0x000E;

Enjoy!
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top