int main(void) { /* Disable JTAG to free up PORTA pins */ DDPCONbits.JTAGEN = 0; /* Set the lower 8 bits of PORTA as output (for Explorer-16 LEDs), clear the bits to ensure there is no mismatch when they are toggled */ PLIB_PORTS_DirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_A, (PORTS_DATA_MASK)0x00FF); PLIB_PORTS_Clear(PORTS_ID_0, PORT_CHANNEL_A, (PORTS_DATA_MASK)0x00FF); /* Enable the DMA module */ PLIB_DMA_Enable(DMA_ID_0); /* Channel is continuously enabled */ PLIB_DMA_ChannelXAutoEnable(DMA_ID_0, DMA_CHANNEL_0); /* Set the source and destinaton addresses (addresses are converted from virtual to physical) */ PLIB_DMA_ChannelXSourceStartAddressSet(DMA_ID_0, DMA_CHANNEL_0, (uint32_t)LED_pattern); PLIB_DMA_ChannelXDestinationStartAddressSet(DMA_ID_0, DMA_CHANNEL_0, (uint32_t)&LATA); /* Set the source and destination sizes */ PLIB_DMA_ChannelXSourceSizeSet(DMA_ID_0, DMA_CHANNEL_0, sizeof(LED_pattern)); PLIB_DMA_ChannelXDestinationSizeSet(DMA_ID_0, DMA_CHANNEL_0, 1); /* Set the number of bytes per transfer */ PLIB_DMA_ChannelXCellSizeSet(DMA_ID_0, DMA_CHANNEL_0, 1); /* DMA transfer to start on Timer 1 interrupt */ PLIB_DMA_ChannelXTriggerEnable(DMA_ID_0, DMA_CHANNEL_0, DMA_CHANNEL_TRIGGER_TRANSFER_START); PLIB_DMA_ChannelXStartIRQSet(DMA_ID_0, DMA_CHANNEL_0, DMA_TRIGGER_TIMER_1); /* Setup Timer 1 to trigger an interrupt 10 times a second */ PLIB_TMR_ClockSourceSelect(TMR_ID_1, TMR_CLOCK_SOURCE_PERIPHERAL_CLOCK); PLIB_TMR_PrescaleSelect(TMR_ID_1, TMR_PRESCALE_VALUE_256); PLIB_TMR_Counter16BitClear(TMR_ID_1); PLIB_TMR_Period16BitSet(TMR_ID_1, 3906); /* Enable the Timer 1 interrupt source, set its priority level to 2, set its subpriority level to 0 */ PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_TIMER_1); PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_T1, INT_PRIORITY_LEVEL2); PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_T1, INT_SUBPRIORITY_LEVEL0); /* Enable multi-vectored interrupts, enable the generation of interrupts to the CPU */ PLIB_INT_MultiVectorSelect(INT_ID_0); PLIB_INT_Enable(INT_ID_0); /* Enable DMA channel 0 */ PLIB_DMA_ChannelXEnable(DMA_ID_0, DMA_CHANNEL_0); /* Start Timer 1 */ PLIB_TMR_Start(TMR_ID_1); /* Stuck in this loop, waiting for interrupts to occur */ while (1); /* Program should not go here during normal operation */ return EXIT_FAILURE; }
// ***************************************************************************** // ***************************************************************************** // 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_1); /* 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, 2104); }
// ***************************************************************************** // ***************************************************************************** // 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, 25000); /* 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); }
// ***************************************************************************** // ***************************************************************************** // Section: Instance 1 static driver functions // ***************************************************************************** // ***************************************************************************** void DRV_TMR1_Initialize(void) { /* Initialize Timer Instance1 */ /* Disable Timer */ PLIB_TMR_Stop(TMR_ID_4); /* Select clock source */ PLIB_TMR_ClockSourceSelect(TMR_ID_4, TMR_CLOCK_SOURCE_EXTERNAL_INPUT_PIN); /* Select prescalar value */ PLIB_TMR_PrescaleSelect(TMR_ID_4, TMR_PRESCALE_VALUE_1); /* Enable 16 bit mode */ PLIB_TMR_Mode16BitEnable(TMR_ID_4); /* Clear counter */ PLIB_TMR_Counter16BitClear(TMR_ID_4); /*Set period */ PLIB_TMR_Period16BitSet(TMR_ID_4, 5); /* Setup Interrupt */ PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_TIMER_4); PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_T4, INT_PRIORITY_LEVEL1); PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_T4, 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_CounterClear(void) { /* Clear 16-bit counter value */ PLIB_TMR_Counter16BitClear(TMR_ID_2); }