// ***************************************************************************** // ***************************************************************************** // Section: Instance 0 static driver functions // ***************************************************************************** // ***************************************************************************** void DRV_TMR0_Initialize(void) { /* Initialize Timer Instance0 */ /* Disable Timer */ PLIB_TMR_Stop(TMR_ID_2); /* Select clock source */ PLIB_TMR_ClockSourceSelect(TMR_ID_2, TMR_CLOCK_SOURCE_PERIPHERAL_CLOCK); /* Select prescalar value */ PLIB_TMR_PrescaleSelect(TMR_ID_2, TMR_PRESCALE_VALUE_256); /* Enable 16 bit mode */ PLIB_TMR_Mode16BitEnable(TMR_ID_2); /* Clear counter */ PLIB_TMR_Counter16BitClear(TMR_ID_2); /*Set period */ PLIB_TMR_Period16BitSet(TMR_ID_2, 0); /* Setup Interrupt */ PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_TIMER_2); PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_T2, INT_PRIORITY_LEVEL1); PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_T2, INT_SUBPRIORITY_LEVEL0); }
void PWM2_SetValues( TMR_PRESCALE pwmPreScale , int pwmCycle , int pwmStart1 , int pwmStop1 , int pwmStart2 , int pwmStop2 ) { if ((pwmPreScale == TMR_PRESCALE_VALUE_1) & (pwmCycle < 400)) { PWM2_doInt = false; } if ((pwmPreScale != PWM2_PreScale) | !PWM2_doInt) { // total update // Disable Interrupt / Clear Flag PLIB_INT_SourceDisable(APP_INT_ID, APP_PWM2_TMR_INT_SOURCE); PLIB_INT_SourceFlagClear(APP_INT_ID, APP_PWM2_TMR_INT_SOURCE); // Stop the timer PLIB_TMR_Stop(APP_PWM2_TMR_ID); // Disable OC's PLIB_OC_Disable(APP_PWM2_OC1_ID); #ifdef APP_PWM2_OC2_ID PLIB_OC_Disable(APP_PWM2_OC2_ID); #endif // ifdef APP_PWM2_OC2_ID PWM2_PreScale = pwmPreScale; PWM2_Cycle = pwmCycle; PWM2_Start1 = pwmStart1; PWM2_Stop1 = pwmStop1; PWM2_Start2 = pwmStart2; PWM2_Stop2 = pwmStop2; // Set the prescaler, and set the clock source as internal PLIB_TMR_PrescaleSelect(APP_PWM2_TMR_ID, pwmPreScale); // Clear the timer PLIB_TMR_Counter16BitClear(APP_PWM2_TMR_ID); // Load the period register PLIB_TMR_Period16BitSet(APP_PWM2_TMR_ID, pwmCycle); // OC1 Init // Set buffer(primary compare) value PLIB_OC_Buffer16BitSet(APP_PWM2_OC1_ID, pwmStart1); // Set pulse width(secondary compare) value PLIB_OC_PulseWidth16BitSet(APP_PWM2_OC1_ID, pwmStop1); #ifdef APP_PWM2_OC2_ID // OC2 Init // Set buffer(primary compare) value PLIB_OC_Buffer16BitSet(APP_PWM2_OC2_ID, pwmStart2); // Set pulse width(secondary compare) value PLIB_OC_PulseWidth16BitSet(APP_PWM2_OC2_ID, pwmStop2); #endif // ifdef APP_PWM2_OC2_ID // Enable OC 1 PLIB_OC_Enable(APP_PWM2_OC1_ID); #ifdef APP_PWM2_OC2_ID // Enable OC 2 PLIB_OC_Enable(APP_PWM2_OC2_ID); #endif // ifdef APP_PWM2_OC2_ID if ((PWM2_PreScale == TMR_PRESCALE_VALUE_1) & (PWM2_Cycle < 400)) { PWM2_doInt = false; } else { PWM2_doInt = true; // Reenable Interrupt PLIB_INT_SourceEnable(APP_INT_ID, APP_PWM2_TMR_INT_SOURCE); } // Start the timer PLIB_TMR_Start(APP_PWM2_TMR_ID); } else { // continuos update PWM2_Cycle = pwmCycle; PWM2_Start1 = pwmStart1; PWM2_Stop1 = pwmStop1; PWM2_Start2 = pwmStart2; PWM2_Stop2 = pwmStop2; } }
void PWM_Initialize(void) { // Timer Init // Stop the timers PLIB_TMR_Stop(APP_PWM_TMR_ID); #ifdef APP_PWM2_OC1_ID PLIB_TMR_Stop(APP_PWM2_TMR_ID); #endif //ifdef APP_PWM2_OC1_ID // Disable OC's PLIB_OC_Disable(APP_PWM_OC1_ID); #ifdef APP_PWM_OC2_ID PLIB_OC_Disable(APP_PWM_OC2_ID); #endif // ifdef APP_PWM_OC2_ID #ifdef APP_PWM_OC3_ID PLIB_OC_Disable(APP_PWM_OC3_ID); #endif // ifdef APP_PWM_OC3_ID #ifdef APP_PWM_OC4_ID PLIB_OC_Disable(APP_PWM_OC4_ID); #endif // ifdef APP_PWM_OC4_ID #ifdef APP_PWM2_OC1_ID PLIB_OC_Disable(APP_PWM2_OC1_ID); #endif // ifdef APP_PWM2_OC1_ID #ifdef APP_PWM2_OC2_ID PLIB_OC_Disable(APP_PWM2_OC2_ID); #endif // ifdef APP_PWM2_OC2_ID // Set the prescaler, and set the clock source as internal PLIB_TMR_ClockSourceSelect(APP_PWM_TMR_ID, TMR_CLOCK_SOURCE_PERIPHERAL_CLOCK); PLIB_TMR_PrescaleSelect(APP_PWM_TMR_ID, APP_PWM_TMR_PRESCALE); PWM_PreScale = APP_PWM_TMR_PRESCALE; // Clear the timer PLIB_TMR_Counter16BitClear(APP_PWM_TMR_ID); // Load the period register PLIB_TMR_Period16BitSet(APP_PWM_TMR_ID, APP_PWM_TMR_INIT); PWM_Cycle = APP_PWM_TMR_INIT; #ifdef APP_PWM2_OC1_ID PLIB_TMR_ClockSourceSelect(APP_PWM2_TMR_ID, TMR_CLOCK_SOURCE_PERIPHERAL_CLOCK); PLIB_TMR_PrescaleSelect(APP_PWM2_TMR_ID, APP_PWM2_TMR_PRESCALE); PWM2_PreScale = APP_PWM2_TMR_PRESCALE; // Clear the timer PLIB_TMR_Counter16BitClear(APP_PWM2_TMR_ID); // Load the period register PLIB_TMR_Period16BitSet(APP_PWM2_TMR_ID, APP_PWM2_TMR_INIT); PWM2_Cycle = APP_PWM2_TMR_INIT; #endif // ifdef APP_PWM2_OC1_ID // OC1 Init // port inits PLIB_PORTS_PinClear(APP_PWM_OC1_PORTS_ID, APP_PWM_OC1_PORT_CHANNEL, APP_PWM_OC1_PIN); PLIB_PORTS_PinDirectionOutputSet(APP_PWM_OC1_PORTS_ID, APP_PWM_OC1_PORT_CHANNEL, APP_PWM_OC1_PIN); APP_PWM_OC1_Mode; PLIB_PORTS_RemapOutput(APP_PWM_OC1_PORTS_ID, APP_PWM_OC1_Function, APP_PWM_OC1_PPSOut); //Select timer base PLIB_OC_TimerSelect(APP_PWM_OC1_ID, APP_PWM_OC_TMR_BASE); // Select compare mode PLIB_OC_ModeSelect(APP_PWM_OC1_ID, OC_DUAL_COMPARE_CONTINUOUS_PULSE_MODE); // Set buffer size to 16-bits PLIB_OC_BufferSizeSelect(APP_PWM_OC1_ID, OC_BUFFER_SIZE_16BIT); // Set buffer(primary compare) value PLIB_OC_Buffer16BitSet(APP_PWM_OC1_ID, APP_PWM_OC1_On); PWM_Start1 = APP_PWM_OC1_On; // Set pulse width(secondary compare) value PLIB_OC_PulseWidth16BitSet(APP_PWM_OC1_ID, APP_PWM_OC1_Off); PWM_Stop1 = APP_PWM_OC1_Off; #ifdef APP_PWM_OC2_ID // OC2 Init // port inits PLIB_PORTS_PinClear(APP_PWM_OC2_PORTS_ID, APP_PWM_OC2_PORT_CHANNEL, APP_PWM_OC2_PIN); PLIB_PORTS_PinDirectionOutputSet(APP_PWM_OC2_PORTS_ID, APP_PWM_OC2_PORT_CHANNEL, APP_PWM_OC2_PIN); APP_PWM_OC2_Mode; PLIB_PORTS_RemapOutput(APP_PWM_OC2_PORTS_ID, APP_PWM_OC2_Function, APP_PWM_OC2_PPSOut); //Select timer base PLIB_OC_TimerSelect(APP_PWM_OC2_ID, APP_PWM_OC_TMR_BASE); // Select compare mode PLIB_OC_ModeSelect(APP_PWM_OC2_ID, OC_DUAL_COMPARE_CONTINUOUS_PULSE_MODE); // Set buffer size to 16-bits PLIB_OC_BufferSizeSelect(APP_PWM_OC2_ID, OC_BUFFER_SIZE_16BIT); // Set buffer(primary compare) value PLIB_OC_Buffer16BitSet(APP_PWM_OC2_ID, APP_PWM_OC2_On); PWM_Start2 = APP_PWM_OC2_On; // Set pulse width(secondary compare) value PLIB_OC_PulseWidth16BitSet(APP_PWM_OC2_ID, APP_PWM_OC2_Off); PWM_Stop2 = APP_PWM_OC2_Off; #endif // ifdef APP_PWM_OC2_ID #ifdef APP_PWM_OC3_ID // OC3 Init // port inits PLIB_PORTS_PinClear(APP_PWM_OC3_PORTS_ID, APP_PWM_OC3_PORT_CHANNEL, APP_PWM_OC3_PIN); PLIB_PORTS_PinDirectionOutputSet(APP_PWM_OC3_PORTS_ID, APP_PWM_OC3_PORT_CHANNEL, APP_PWM_OC3_PIN); APP_PWM_OC3_Mode; PLIB_PORTS_RemapOutput(APP_PWM_OC3_PORTS_ID, APP_PWM_OC3_Function, APP_PWM_OC3_PPSOut); //Select timer base PLIB_OC_TimerSelect(APP_PWM_OC3_ID, APP_PWM_OC_TMR_BASE); // Select compare mode PLIB_OC_ModeSelect(APP_PWM_OC3_ID, OC_DUAL_COMPARE_CONTINUOUS_PULSE_MODE); // Set buffer size to 16-bits PLIB_OC_BufferSizeSelect(APP_PWM_OC3_ID, OC_BUFFER_SIZE_16BIT); // Set buffer(primary compare) value PLIB_OC_Buffer16BitSet(APP_PWM_OC3_ID, APP_PWM_OC3_On); PWM_Start3 = APP_PWM_OC3_On; // Set pulse width(secondary compare) value PLIB_OC_PulseWidth16BitSet(APP_PWM_OC3_ID, APP_PWM_OC3_Off); PWM_Stop3 = APP_PWM_OC3_Off; #endif // ifdef APP_PWM_OC3_ID #ifdef APP_PWM_OC4_ID // OC4 Init // port inits PLIB_PORTS_PinClear(APP_PWM_OC4_PORTS_ID, APP_PWM_OC4_PORT_CHANNEL, APP_PWM_OC4_PIN); PLIB_PORTS_PinDirectionOutputSet(APP_PWM_OC4_PORTS_ID, APP_PWM_OC4_PORT_CHANNEL, APP_PWM_OC4_PIN); APP_PWM_OC4_Mode; PLIB_PORTS_RemapOutput(APP_PWM_OC4_PORTS_ID, APP_PWM_OC4_Function, APP_PWM_OC4_PPSOut); //Select timer base PLIB_OC_TimerSelect(APP_PWM_OC4_ID, APP_PWM_OC_TMR_BASE); // Select compare mode PLIB_OC_ModeSelect(APP_PWM_OC4_ID, OC_DUAL_COMPARE_CONTINUOUS_PULSE_MODE); // Set buffer size to 16-bits PLIB_OC_BufferSizeSelect(APP_PWM_OC4_ID, OC_BUFFER_SIZE_16BIT); // Set buffer(primary compare) value PLIB_OC_Buffer16BitSet(APP_PWM_OC4_ID, APP_PWM_OC4_On); PWM_Start4 = APP_PWM_OC4_On; // Set pulse width(secondary compare) value PLIB_OC_PulseWidth16BitSet(APP_PWM_OC4_ID, APP_PWM_OC4_Off); PWM_Stop4 = APP_PWM_OC4_Off; #endif // ifdef APP_PWM_OC4_ID #ifdef APP_PWM2_OC1_ID // PWM2 / OC1 Init // port inits PLIB_PORTS_PinClear(APP_PWM2_OC1_PORTS_ID, APP_PWM2_OC1_PORT_CHANNEL, APP_PWM2_OC1_PIN); PLIB_PORTS_PinDirectionOutputSet(APP_PWM2_OC1_PORTS_ID, APP_PWM2_OC1_PORT_CHANNEL, APP_PWM2_OC1_PIN); APP_PWM2_OC1_Mode; PLIB_PORTS_RemapOutput(APP_PWM2_OC1_PORTS_ID, APP_PWM2_OC1_Function, APP_PWM2_OC1_PPSOut); //Select timer base PLIB_OC_TimerSelect(APP_PWM2_OC1_ID, APP_PWM2_OC_TMR_BASE); // Select compare mode PLIB_OC_ModeSelect(APP_PWM2_OC1_ID, OC_DUAL_COMPARE_CONTINUOUS_PULSE_MODE); // Set buffer size to 16-bits PLIB_OC_BufferSizeSelect(APP_PWM2_OC1_ID, OC_BUFFER_SIZE_16BIT); // Set buffer(primary compare) value PLIB_OC_Buffer16BitSet(APP_PWM2_OC1_ID, APP_PWM2_OC1_On); PWM2_Start1 = APP_PWM2_OC1_On; // Set pulse width(secondary compare) value PLIB_OC_PulseWidth16BitSet(APP_PWM2_OC1_ID, APP_PWM2_OC1_Off); PWM2_Stop1 = APP_PWM2_OC1_Off; #endif // ifdef APP_PWM2_OC1_ID #ifdef APP_PWM2_OC2_ID // PWM2 / OC2 Init // port inits PLIB_PORTS_PinClear(APP_PWM2_OC2_PORTS_ID, APP_PWM2_OC2_PORT_CHANNEL, APP_PWM2_OC2_PIN); PLIB_PORTS_PinDirectionOutputSet(APP_PWM2_OC2_PORTS_ID, APP_PWM2_OC2_PORT_CHANNEL, APP_PWM2_OC2_PIN); APP_PWM2_OC2_Mode; PLIB_PORTS_RemapOutput(APP_PWM2_OC2_PORTS_ID, APP_PWM2_OC2_Function, APP_PWM2_OC2_PPSOut); //Select timer base PLIB_OC_TimerSelect(APP_PWM2_OC1_ID, APP_PWM2_OC_TMR_BASE); // Select compare mode PLIB_OC_ModeSelect(APP_PWM2_OC2_ID, OC_DUAL_COMPARE_CONTINUOUS_PULSE_MODE); // Set buffer size to 16-bits PLIB_OC_BufferSizeSelect(APP_PWM2_OC2_ID, OC_BUFFER_SIZE_16BIT); // Set buffer(primary compare) value PLIB_OC_Buffer16BitSet(APP_PWM2_OC2_ID, APP_PWM2_OC2_On); PWM2_Start2 = APP_PWM2_OC2_On; // Set pulse width(secondary compare) value PLIB_OC_PulseWidth16BitSet(APP_PWM2_OC2_ID, APP_PWM2_OC2_Off); PWM2_Stop2 = APP_PWM2_OC2_Off; #endif // ifdef APP_PWM2_OC2_ID }
inline void DRV_TMR0_Stop(void) { /* Stop Timer*/ PLIB_TMR_Stop(TMR_ID_1); }