void delayusec(uint32_t usectime) { int i = 0; USECTIMERFLAG = false; PLIB_TMR_Stop(TMR_ID_4); DRV_TMR0_CounterClear(); PLIB_TMR_Period32BitSet(TMR_ID_4, usectime * (SYS_CLK_BUS_PERIPHERAL_1 / 1000000)); DRV_TMR0_Start(); while (1) { if (USECTIMERFLAG == 0) { } else { return; } }; /* while (!PLIB_INT_SourceFlagGet(INT_ID_0, INT_SOURCE_TIMER_5)); PLIB_INT_SourceFlagClear(INT_ID_0, INT_SOURCE_TIMER_5);*/ }
// ***************************************************************************** // ***************************************************************************** // Section: Instance 0 static driver functions // ***************************************************************************** // ***************************************************************************** void DRV_TMR0_Initialize(void) { /* Initialize Timer Instance0 */ /* Disable Timer */ PLIB_TMR_Stop(TMR_ID_1); /* Select clock source */ PLIB_TMR_ClockSourceSelect(TMR_ID_1, TMR_CLOCK_SOURCE_PERIPHERAL_CLOCK); /* Select prescalar value */ PLIB_TMR_PrescaleSelect(TMR_ID_1, TMR_PRESCALE_VALUE_256); /* Enable 32 bit mode */ PLIB_TMR_Mode32BitEnable(TMR_ID_1); /* Clear counter */ PLIB_TMR_Counter32BitClear(TMR_ID_1); /*Set period */ PLIB_TMR_Period32BitSet(TMR_ID_1, 1); /* Setup Interrupt */ PLIB_INT_SourceEnable(INT_ID_0, INT_SOURCE_TIMER_1); PLIB_INT_VectorPrioritySet(INT_ID_0, INT_VECTOR_T1, INT_PRIORITY_LEVEL1); PLIB_INT_VectorSubPrioritySet(INT_ID_0, INT_VECTOR_T1, INT_SUBPRIORITY_LEVEL0); }
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; }