PIC32MX250F128B Simple PWM Output

Publisher:码农侠Latest update time:2020-04-27 Source: eefocusKeywords:PIC32MX250F128B Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*********************************************************************

 *

 *      PIC32MX PWM demo

 *

 *********************************************************************

 * FileName:        pwm demo.c

 * Dependencies:    plib.h

 *

 * Processor:       PIC32MX

 *

 * Compiler: MPLAB XC32

 * MPLAB-X IDE

 * Company:         xx

 *

 * Author:          Perry.Peng@xx.com

 *

 * The purpose of this example code is to demonstrate the PIC32MX

 * peripheral library macros and functions supporting the Timer

 * module and its various features.

 * 

 * Features demonstrated:

 *    - Port pin configurations

 *    - 32bit Timer

 *    - OCx pins

 *

 * Description:

 *      This program blinks the lower four LEDs on the Explorer-16

 *      demo board.

 *

 * Notes:

 *    - PIC32MX 2xx PIMS are unconnected to the Explorer-16 LEDs.

 *      They must be soldered to the test points on top of the PIM

 *      for proper functionality. The README file contains a list

 *      of the connections that need to be made.

 ********************************************************************/

#include

 

#if defined (__32MX250F128B__)

// Configuration Bit settings

 

// #pragma config PMDL1WAY         = OFF           // Peripheral Module Disable Configuration

// #pragma config IOL1WAY          = OFF           // Peripheral Pin Select Configuration

#pragma config FUSBIDIO         = OFF           // USB USID Selection

#pragma config FVBUSONIO        = OFF           // USB VBUS ON Selection

// #pragma config UPLLIDIV         = DIV_4         // USB PLL Input Divider

// #pragma config UPLLEN           = OFF           // USB PLL Enable

 

/* Oscillator Selection Bits

    FNOSC = FRC Fast RC Osc (FRC)

    FNOSC = FRCPLL Fast RC Osc with PLL

    FNOSC = PRI Primary Osc (XT,HS,EC)

    FNOSC = PRIPLL Primary Osc w/PLL (XT+,HS+,EC+PLL)

    FNOSC = SOSC Low Power Secondary Osc (SOSC)

    FNOSC = LPRC Low Power RC Osc (LPRC)

    FNOSC = FRCDIV16 Fast RC Osc w/Div-by-16 (FRC/16)

    FNOSC = FRCDIV Fast RC Osc w/Div-by-N (FRCDIV)

 */

#pragma config FNOSC            = FRCPLL       // Internal Fast RC oscillator (8 MHz) w/ PLL

 

#pragma config FPLLIDIV         = DIV_2         // PLL Input Divider

#pragma config FPLLMUL          = MUL_24        // PLL Multiplier

#pragma config FPLLODIV         = DIV_2         // System PLL Output Clock Divider

 

#define SYS_FREQ (48000000L)                    // = (8MHz / FPLLIDIV * FPLLMUL / FPLLODIV)

 

// PBCLK = 48 MHz (SYSCLK / FPBDIV)

#pragma config FPBDIV           = DIV_1         // Peripheral Clock Divisor

 

/* Primary Oscillator Configuration

    POSCMOD = EC External clock mode

    POSCMOD = XT XT osc mode

    POSCMOD = HS HS osc mode

    POSCMOD = OFF Primary osc disabled

 */

#pragma config POSCMOD          = OFF

#pragma config IESO             = ON            // Internal/External Switch Over

#pragma config OSCIOFNC         = OFF           // CLKO Output Signal Active on the OSCO Pin

#pragma config FSOSCEN          = OFF           // Secondary Oscillator Enable

 

/* Clock Switching and Monitor Selection

    FCKSM = CSECME Clock Switch Enable, FSCM Enabled

    FCKSM = CSECMD Clock Switch Enable, FSCM Disabled

    FCKSM = CSDCMD Clock Switch Disable, FSCM Disabled

 */

//#pragma config FCKSM            = CSDCMD

 

#pragma config FWDTEN           = OFF           // Watchdog Timer Enable

#pragma config WINDIS           = OFF           // Watchdog Timer Window Enable

 

/* Watchdog Timer Window Size

    FWDTWINSZ = WINSZ_75 Window Size is 75%

    FWDTWINSZ = WINSZ_50 Window Size is 50%

    FWDTWINSZ = WINSZ_37 Window Size is 37.5%

    FWDTWINSZ = WISZ_25 Window Size is 25%

 */

#pragma config FWDTWINSZ        = WINSZ_75

 

#pragma config CP               = OFF           // Code Protect

#pragma config BWP              = OFF           // Boot Flash Write Protect

#pragma config PWP              = OFF           // Program Flash Write Protect

 

#pragma config JTAGEN           = OFF           // JTAG Enable

#pragma config DEBUG            = OFF           // Background Debugger Enable bit

// #pragma config ICESEL        = ICS_PGx1           // ICE/ICD Comm Channel Select

 

#endif

 

int main(void)

{

    unsigned int i = 1, z, pwm = 0;

 

    // Configure the device for maximum performance but do not change the PBDIV

    // Given the options, this function will change the flash wait states, RAM

    // wait state and enable prefetch cache but will not change the PBDIV.

    // The PBDIV value is already set via the pragma FPBDIV option above.

    SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

 

    

    PPSOutput(2, RPA1, OC2);  // Set OC2 to RPA1 with peripheral pin select

    PPSOutput(3, RPA2, OC4);  // Set OC3 to RPA2 with peripheral pin select

    PPSOutput(4, RPA3, OC3);  // Set OC4 to RPA3 with peripheral pin select

 

    /* Enable OC | 32 bit Mode  | Timer2 is selected | Continuous O/P   | OC Pin High , S Compare value, Compare value*/

    OpenOC2(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);

    OpenOC3(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);

    OpenOC4(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);

    /* Open Timer2 with Period register value */

    OpenTimer2(T2_ON | T2_PS_1_1 | T2_SOURCE_INT, 0x100);

 

    // Now blink LEDs ON/OFF forever.

    while (1)

    {

        // Insert some delay

        for (z = 0; z < 1024 * 256; z++)

            ;

        SetDCOC2PWM(pwm++); // Write new duty cycle

        SetDCOC3PWM(pwm++); // Write new duty cycle

        SetDCOC4PWM(pwm++); // Write new duty cycle

        if (pwm > 255)

            mold = 0;

    }

}

Keywords:PIC32MX250F128B Reference address:PIC32MX250F128B Simple PWM Output

Previous article:Getting Started with MPLAB X IDE Debug Simulation
Next article:【PIC32MZ】Light up LED

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号