PIC32MX250F128B简单PWM输出

/*********************************************************************
 *
 *      PIC32MX PWM demo
 *
 *********************************************************************
 * FileName:        pwm demo.c
 * Dependencies:    plib.h
 *
 * Processor:       PIC32MX
 *
 * Complier:        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 <plib.h>

#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)
            pwm = 0;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值