/*********************************************************************
*
* 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; } }
Previous article:Getting Started with MPLAB X IDE Debug Simulation
Next article:【PIC32MZ】Light up LED
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Based on PSOC6 development board simulation I2C solution X-NUCLEO-IKS01A3 HTS221
- 50 ways to use TI CC6678 digital signal processor (DSP)
- 20 most advanced robots that are a little scary!
- How to choose the right inductor for RF circuits
- msp430f169 bluetooth fan adjustable speed program
- EEWORLD University ---- Working Principle of Diode
- Intel FPGA 2019 Engineer Application Video
- AD drawing PCB has many white lines after enlarging
- AD Ignore DRC with irregular slotting settings using pad stacking
- High frequency characteristics of resistors