// Called from TX interrupt. Read from transmit message queue and // output byte by byte to the transmit buffer void receiveDataFromISR() { BaseType_t xTaskWokenByReceive = pdFALSE; char readChar; // Temporarily disable the transmit interrupt PLIB_INT_SourceDisable(INT_ID_0, INT_SOURCE_USART_1_TRANSMIT); // While the queue has available messages, read bytes and transmit while (uxQueueMessagesWaitingFromISR(sendData.transmitQ_CD) != 0){ while (xQueueReceiveFromISR(sendData.transmitQ_CD, (void *) &readChar, &xTaskWokenByReceive) ) { // A character was received. Transmit the character now. if (sendData.testCount % BREAK_MESSAGE_DIV != 2) // Error simulation constant (missing byte) PLIB_USART_TransmitterByteSend(USART_ID_1, readChar); // Duplicate data error simulation constant if (sendData.testCount % ADD_MESSAGE_DIV == 1) PLIB_USART_TransmitterByteSend(USART_ID_1, readChar); // If removing the character from the queue woke the task that was // posting onto the queue cTaskWokenByReceive will have been set to // pdTRUE. No matter how many times this loop iterates only one // task will be woken. sendData.testCount++; } } }
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; } }