Problem in coding

Discussion in 'Embedded Systems and Microcontrollers' started by raja.afiq, Jul 17, 2013.

  1. raja.afiq

    Thread Starter New Member

    Jul 17, 2013
    2
    1
    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 ( (Unknown Language)):
    1.  
    2. #include <p33FJ64MC802.h>
    3.  
    4. _FOSCSEL(FNOSC_FRCPLL)              //set clock for internal OSC with PLL
    5. _FOSC(OSCIOFNC_OFF & POSCMD_NONE)   //no clock output, external OSC disabled
    6. _FWDT(FWDTEN_OFF)                   //disable the watchdog timer
    7. _FICD(JTAGEN_OFF & ICS_PGD1);       //disable JTAG, enable debugging on PGx1 pins
    8.  
    9. void initpwm(void){
    10.     P1TCON = 0x8000; //free running, time base on, fcy 1:1, run in cpu idle mode
    11.     PWM1CON1bits.PMOD1 = 1;  //Select Independent Output PWM mode
    12.     PWM1CON1bits.PEN1H = 1;  //PWM1H control by PWM module
    13.     PWM1CON1bits.PEN1L = 0;  //PWM1L control as GPIO
    14.     PWM1CON2=0b0000000000000010;
    15.     P1DTCON1=0;
    16.     P1FLTACON=0;
    17.     P1TMR=0;
    18. }
    19.  
    20. int main (void){
    21.     OSCCON=0x22E0;
    22.     CLKDIV=0;
    23.     // setup internal clock for 80MHz (Fosc)
    24.     // Fcy=Fosc/2 (Fcy = 40MHz)
    25.     // 7.37/2*43=158.455/2=79.2275MHz
    26.     CLKDIVbits.PLLPRE=0;        // PLLPRE (N2) 0=/2
    27.     PLLFBD=41;                   // pll multiplier (M) = +2
    28.     CLKDIVbits.PLLPOST=0;       // PLLPOST (N1) 0=/2
    29.     initpwm();
    30.     LATBbits.LATB14 = 0;
    31.     TRISBbits.TRISB14 = 0;
    32.     P1TPER=1599;
    33.     //PTPER = (Fcy/(Fpwm*prescaler))-1
    34.     //PTPER = (40M/(25k*1))-1
    35.     //Fpwm is desire freq for motor.
    36.     while (1){
    37.         P1DC1=0;
    38.     }
    39. }
    40.  
     
Loading...