PWM code for dsPIC33EV256GM102 not working using PWM module

Discussion in 'Embedded Systems and Microcontrollers' started by dandrous, Sep 29, 2016.

  1. dandrous

    Thread Starter New Member

    Mar 2, 2015
    22
    0
    The program builds successfully,but the pwm output is not there


    here is the code

    Code (Text):
    1. /////////////header file
    2.  
    3. #include <xc.h>
    4. #pragma config BWRP = OFF               // Boot Segment Write-Protect Bit (Boot Segment may be written)
    5. #pragma config BSS = DISABLED           // Boot Segment Code-Protect Level bits (No Protection (other than BWRP))
    6. #pragma config BSS2 = OFF               // Boot Segment Control Bit (No Boot Segment)
    7. #pragma config GWRP = OFF               // General Segment Write-Protect Bit (General Segment may be written)
    8. #pragma config GSS = DISABLED           // General Segment Code-Protect Level bits (No Protection (other than GWRP))
    9. #pragma config CWRP = OFF               // Configuration Segment Write-Protect Bit (Configuration Segment may be written)
    10. #pragma config CSS = DISABLED           // Configuration Segment Code-Protect Level bits (No Protection (other than CWRP))
    11. #pragma config AIVTDIS = DISABLE        // Alternate Interrupt Vector Table Disable Bit  (Disable Alternate Vector Table)
    12.  
    13. // FBSLIM
    14. #pragma config BSLIM = 0x1FFF           // Boot Segment Code Flash Page Address Limit Bits (Boot Segment Flash Page Address Limit (0-0x1FFF))
    15.  
    16. // FOSCSEL
    17. #pragma config FNOSC = FRCDIVN          // Initial oscillator Source Selection Bits (Internal Fast RC (FRC) Oscillator with postscaler)
    18. #pragma config IESO = ON                // Two Speed Oscillator Start-Up Bit (Start up device with FRC,then automatically switch to user selected oscillator source)
    19.  
    20. // FOSC
    21. #pragma config POSCMD = NONE            // Primary Oscillator Mode Select Bits (Primary Oscillator disabled)
    22. #pragma config OSCIOFNC = OFF           // OSC2 Pin I/O Function Enable Bit (OSC2 is clock output)
    23. #pragma config IOL1WAY = ON             // Peripheral Pin Select Configuration Bit (Allow Only One reconfiguration)
    24. #pragma config FCKSM = CSDCMD           // Clock Switching Mode Bits (Both Clock Switching and Fail-safe Clock Monitor are disabled)
    25. #pragma config PLLKEN = ON              // PLL Lock Enable Bit (Clock switch to PLL source will wait until the PLL lock signal is valid)
    26.  
    27. // FWDT
    28. #pragma config WDTPOST = PS32768        // Watchdog Timer Postscaler Bits (1:32,768)
    29. #pragma config WDTPRE = PR128           // Watchdog Timer Prescaler Bit (1:128)
    30. #pragma config FWDTEN = ON              // Watchdog Timer Enable Bits (WDT Enabled)
    31. #pragma config WINDIS = OFF             // Watchdog Timer Window Enable Bit (Watchdog timer in Non-Window Mode)
    32. #pragma config WDTWIN = WIN25           // Watchdog Window Select Bits (WDT Window is 25% of WDT period)
    33.  
    34. // FPOR
    35. #pragma config BOREN0 = ON              // Brown Out Reset Detection Bit (BOR is Enabled)
    36.  
    37. // FICD
    38. #pragma config ICS = PGD1               // ICD Communication Channel Select Bits (Communicate on PGEC1 and PGED1)
    39.  
    40. // FDMTINTVL
    41. #pragma config DMTIVTL = 0xFFFF         // Lower 16 Bits of 32 Bit DMT Window Interval (Lower 16 bits of 32 bit DMT window interval (0-0xFFFF))
    42.  
    43. // FDMTINTVH
    44. #pragma config DMTIVTH = 0xFFFF         // Upper 16 Bits of 32 Bit DMT Window Interval (Upper 16 bits of 32 bit DMT window interval (0-0xFFFF))
    45.  
    46. // FDMTCNTL
    47. #pragma config DMTCNTL = 0xFFFF         // Lower 16 Bits of 32 Bit DMT Instruction Count Time-Out Value (Lower 16 bits of 32 bit DMT instruction count time-out value (0-0xFFFF))
    48.  
    49. // FDMTCNTH
    50. #pragma config DMTCNTH = 0xFFFF         // Upper 16 Bits of 32 Bit DMT Instruction Count Time-Out Value (Upper 16 bits of 32 bit DMT instruction count time-out value (0-0xFFFF))
    51.  
    52. // FDMT
    53. #pragma config DMTEN = ENABLE           // Dead Man Timer Enable Bit (Dead Man Timer is Enabled and cannot be disabled by software)
    54.  
    55. // FDEVOPT
    56. #pragma config PWMLOCK = ON             // PWM Lock Enable Bit (Certain PWM registers may only be written after key sequence)
    57. #pragma config ALTI2C1 = OFF            // Alternate I2C1 Pins Selection Bit (I2C1 mapped to SDA1/SCL1 pins)
    58.  
    59. // FALTREG
    60. #pragma config CTXT1 = NONE             // Interrupt Priority Level (IPL) Selection Bits For Alternate Working Register Set 1 (Not Assigned)
    61. #pragma config CTXT2 = NONE             // Interrupt Priority Level (IPL) Selection Bits For Alternate Working Register Set 2 (Not Assigned)
    62.  
    63. // #pragma config statements should precede project file includes.
    64. // Use project enums instead of #define for ON and OFF.
    65.  
    66. #include <xc.h>
    67. #define _XTAL_FREQ 8000000
    68.  
    69.  
    70.  
    71. /////////////////main file
    72. #include <stdio.h>
    73. #include <stdlib.h>
    74. #include "newfile.h"
    75.  
    76. /*
    77. *
    78. */
    79.  
    80. void init_PWM()
    81. {
    82.     /* ~~~~~~~~~~~~~~~~~~~~~~ PWM1 Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */  
    83.     /* PWM1 I/O Control Register register */
    84.     IOCON1bits.PENH      = 1;   /* PWM1H is controlled by PWM module */
    85.     IOCON1bits.PENL      = 1;   /* PWM1L is controlled by PWM module */
    86.     IOCON1bits.PMOD      = 0;   /* Select Complementary Output PWM mode */
    87.  
    88.     /* Load PDC1 register with initial Duty Cycle value */
    89.     PDC1 = 640;                 /* Duty Cycle = PDC2*1.05nsec = 672nsec */
    90.  
    91.     /* Load PDTR1 and ALTDTR2 register with preset dead time value */
    92.     DTR1    = 64;               /* Deadtime setting */
    93.                                 /* Deadtime = DTR1*1.05 = 67.2nsec */
    94.     ALTDTR1 = 64;               /* Deadtime setting */
    95.                                 /* Deadtime = ALTDTR1*1.05 = 67.2nsec */
    96.     /* PHASE1 register */
    97.     PHASE1 = 0;                 /* No phase shift */
    98.  
    99.     /* ~~~~~~~~~~~~~~~~~~~~~~ PWM2 Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */  
    100.     /* PWM2 I/O Control Register register */
    101.     IOCON2bits.PENH      = 1;   /* PWM2H is controlled by PWM module */
    102.     IOCON2bits.PENL      = 1;   /* PWM2L is controlled by PWM module */
    103.     IOCON2bits.PMOD      = 0;   /* Select Complementary Output PWM mode */
    104.  
    105.     /* Load PDC2 register with initial Duty Cycle value */
    106.     PDC2 = 640;                 /* Duty Cycle = PDC2*1.05nsec = 672nsec */
    107.  
    108.    
    109.     /* Load PDTR2 and ALTDTR2 register with preset dead time value */
    110.     DTR2    = 64;               /* Deadtime setting */
    111.                                 /* Deadtime = DTR2*1.05 = 67.2nsec */
    112.     ALTDTR2 = 64;               /* Deadtime setting */
    113.                                 /* Deadtime = ALTDTR2*1.05 = 67.2nsec */
    114.     /* PHASE2 register */
    115.     PHASE2 = 240;               /* approximately 250 nsec phase shift */
    116.                                 /* Phase shift = PHASE2*1.05nsec = 252nsec */
    117.  
    118. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
    119.     /* Configure PTPER register to produce 400kHz PWM frequency */
    120.     PTPER = 2380;               /* PWM Period @30 MIPS, 2.5usec == 400kHz */
    121.                                 /* Period = PTPER*1.05nsec = 2.499usec
    122.  
    123.     /* PWM Time Base Control Register */
    124.     PTCONbits.PTEN       = 1;   /* Enable the PWM Module */
    125.  
    126.  
    127. }
    128.  
    129. /*****************************************************************************/
    130. /*                             MAIN ROUTINE                                  */
    131. /*****************************************************************************/
    132. int main()
    133. {
    134.     init_PWM();
    135.    
    136.     while(1);                   /* Infinite Loop */
    137. }
    138.  
    139.  
    140.  
    141.  
    142.  
     
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,811
    362
    what do you think the code is trying to do?
     
    dandrous likes this.
  3. dandrous

    Thread Starter New Member

    Mar 2, 2015
    22
    0
    i
    i need a pwm output using pwm module .
     
Loading...