// StartUp TMR/OC module of MCU void PWM_StartUp(void) { // Enable OC 1 PLIB_OC_Enable(APP_PWM_OC1_ID); #ifdef APP_PWM_OC2_ID // Enable OC 2 PLIB_OC_Enable(APP_PWM_OC2_ID); #endif // ifdef APP_PWM_OC2_ID #ifdef APP_PWM_OC3_ID // Enable OC 3 PLIB_OC_Enable(APP_PWM_OC3_ID); #endif // ifdef APP_PWM_OC3_ID #ifdef APP_PWM_OC4_ID // Enable OC 4 PLIB_OC_Enable(APP_PWM_OC4_ID); #endif // ifdef APP_PWM_OC4_ID // Start the PWM timer PLIB_TMR_Start(APP_PWM_TMR_ID); #ifdef APP_PWM2_OC1_ID // Enable PWM2 OC 1 PLIB_OC_Enable(APP_PWM2_OC1_ID); #ifdef APP_PWM2_OC2_ID // Enable PWM2 OC 2 PLIB_OC_Enable(APP_PWM2_OC2_ID); #endif // ifdef APP_PWM2_OC2_ID // Start the PWM2 timer PLIB_TMR_Start(APP_PWM2_TMR_ID); #endif // ifdef APP_PWM2_OC1_ID }
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; }
int main(void) { /* PBclk as the timer source, prescaler is 256 (PBclk / 256), enable timer 23 (timer 2 + timer 3, 32 bit time), clear the counter, set timer period */ PLIB_TMR_ClockSourceSelect(TMR_ID_2, TMR_CLOCK_SOURCE_PERIPHERAL_CLOCK); PLIB_TMR_PrescaleSelect(TMR_ID_2, TMR_PRESCALE_VALUE_256); PLIB_TMR_Mode32BitEnable(TMR_ID_2); PLIB_TMR_Counter32BitClear(TMR_ID_2); PLIB_TMR_Period32BitSet(TMR_ID_2, PERIOD); /* Setup input capture to capture only rising edges, captures start on first rising edge, use timer 2, use a 32 bit buffer for storing the captured timer value, enable the input capture 1 module */ PLIB_IC_ModeSelect(IC_ID_1, IC_INPUT_CAPTURE_RISING_EDGE_MODE); PLIB_IC_FirstCaptureEdgeSelect(IC_ID_1, IC_EDGE_RISING); PLIB_IC_TimerSelect(IC_ID_1, IC_TIMER_TMR2); PLIB_IC_BufferSizeSelect(IC_ID_1, IC_BUFFER_SIZE_32BIT); PLIB_IC_Enable(IC_ID_1); /* Enable the input capture 1 interrupt source, set its priority level to 2, set its subpriority level to 0 */ PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_INPUT_CAPTURE_1); PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_IC1, INT_PRIORITY_LEVEL2); PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_IC1, 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); /* Start the timer */ PLIB_TMR_Start(TMR_ID_2); /* Waiting for input capture event (stuck in this loop) */ while (1); /* Program should not go here during normal operation */ return EXIT_FAILURE; }
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; } }
inline void DRV_TMR0_Start(void) { /* Start Timer*/ PLIB_TMR_Start(TMR_ID_1); }
void APP_Tasks ( void ) { /* Check the application's current state. */ switch ( appData.state ) { /* Application's initial state. */ case APP_STATE_INIT: { tmrInterruptHandle = DRV_TMR_Open(DRV_TMR_INDEX_0, DRV_IO_INTENT_EXCLUSIVE); if (DRV_HANDLE_INVALID == tmrInterruptHandle) { // Unable to open the driver while(1); } // based on 80MHz Tpb and 256 PS should be 8192Hz frequency DRV_TMR_AlarmRegister(tmrInterruptHandle, 38, true, 0, TimerInterruptCallback); // change state appData.state = APP_STATE_SETUP_OC; break; } case APP_STATE_SETUP_OC: { DRV_OC0_Enable(); DRV_OC0_Start(); // setup the Timer for OC1 module PLIB_TMR_PrescaleSelect(TMR_ID_2, TMR_PRESCALE_VALUE_1); PLIB_TMR_Period16BitSet(TMR_ID_2, 9765); // start Timer DRV_TMR_Start(tmrInterruptHandle); PLIB_TMR_Start(TMR_ID_2); // change state appData.state = APP_STATE_IDLE; break; } case APP_STATE_IDLE: { break; } /* TODO: implement your application state machine.*/ /* The default state should never be executed. */ default: { /* TODO: Handle error in application's state machine. */ break; } } }