void mTouchCapPhy_TickInit( void ) { TMR4 = 0; //If the Low power demo is not enabled #ifndef LOW_POWER_DEMO_ENABLE PR4 = TIMER_PERIOD; T4CON = TIMER_ON | TIMER_SOURCE_INTERNAL | GATED_TIME_DISABLED | TIMER_16BIT_MODE | TIMER4_PRESCALER; //If Low Power demo is enabled #else //If the Primary oscillator is switched to FRC for the Low Power Demo then load PR4 with //the period value which is calculated for FRC // The time period is selected such that the functions mTouchCapPhy_ReadCTMU() and mTouchCapPhy_AverageData() // should be executed within the time period //Clock_Switch_Enable_Flag -- this will indicate that clock is switched from Primary to FRC if(Clock_Switch_Enable_Flag) { PR4=TIMER_PERIOD_WITH_FRC; //Timer value when FRC is used for Timer4 interrupt; T4CON = TIMER_ON | TIMER_SOURCE_INTERNAL | GATED_TIME_DISABLED | TIMER_16BIT_MODE | TIMER4_PRESCALER; Timer4_Period_Clock_Switch_Flag =HIGH; //To indicate that the timer4 init for FRC clock is complete } //If the Primary Oscillator is used else { PR4 = TIMER_PERIOD; T4CON = TIMER_ON | TIMER_SOURCE_INTERNAL | GATED_TIME_DISABLED | TIMER_16BIT_MODE | TIMER4_PRESCALER; } #endif //end of #ifndef LOW_POWER_DEMO_ENABLE Set_ScanTimer_IF_Bit_State(DISABLE); //Clear flag Set_ScanTimer_IE_Bit_State(ENABLE); //Enable interrupt Set_ScanTimer_ON_Bit_State(ENABLE) ; //Run timer }
void __ISR(_TIMER_4_VECTOR, IPL1SOFT) mTouchCapTmr_Tmr4ISR(void) #endif { void (*ptr_fcn)(void); /* init local wiht the callback function ptr */ ptr_fcn = (void (*) ())T4CallBackISR; if (ptr_fcn != 0) { // call the callback isr (*ptr_fcn)(); } Set_ScanTimer_IF_Bit_State(DISABLE); //Clear interrupt flag if ( EmTouchDataReady == TRUE ) { Set_ScanTimer_IE_Bit_State(DISABLE); // Disable interrupt Set_ScanTimer_ON_Bit_State(DISABLE); // Stop timer 4 } }
/****************************************************************************** * Function: void InitTimer(void) * * PreCondition: None * * Input: unsigned int time * * Output: None * * Side Effects: None * * Overview: Set the period for timer 4 depending on input param time * * Note: Stops and restarts the timer when setting the new period *****************************************************************************/ void mTouchCapTmr_InitTimer4(void) { // set up the timer interrupt with a priority of 1 Set_ScanTimer_Priority(1); // interrupt subpriority is 0 Set_ScanTimer_Subpriority(0); // clear the interrupt flag Set_ScanTimer_IF_Bit_State(0); // initialize the callback pointer with the value passed as argument T4CallBackISR = TIMERCALLBACKFUNC; /* enable the interrupt */ Set_ScanTimer_IE_Bit_State(1); TMR4 = 0; PR4 = TIMER4_PERIOD; // restart the timer Set_ScanTimer_ON_Bit_State(1); return; }
int main(void) { #if defined( DUMP_DIAGNOSTICS_VIA_UART ) char ButtonMeasString[133]; # if defined( UART_DUMP_RAW_COUNTS ) || \ defined( UART_DUMP_ALL_COUNTS ) UINT16 iHF_Read; # endif//defined( UART_DUMP_RAW_COUNTS )... #endif//defined( DUMP_DIAGNOSTICS_VIA_UART ) UINT16 CurrentButtonStatus, LastButtonStatus, CurrentButtonAsserts = 0, CurrentButtonMeasurements[MAX_ADC_CHANNELS], CurrentAveragedMeasurements[MAX_ADC_CHANNELS]; UINT16 CurrentWeights[MAX_ADC_CHANNELS]; CFGCONbits.JTAGEN = 0; // Disable JTAG // Initalize global interrupt enable INTEnableSystemMultiVectoredInt(); // Configure the device for maximum performance // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option. SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); #if defined( DUMP_DIAGNOSTICS_VIA_UART ) // Setup UART2 for transmission of button data. // Taken from C:\Program Files\Microchip\mplabc32\v1.12\examples\plib_examples\uart\uart_basic UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART2, GetPeripheralClock(), 115200 ); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX )); // RPS setup to bring U1TX out on pin 33 (RB4) to Switch 2 on board PPSOutput(4,RPC4,U2TX); #endif//defined( DUMP_DIAGNOSTICS_VIA_UART ) // Setup data structures for using Direct Keys mTouchCapApp_MatrixKeys_Create(); /* This is mandatory function call to initilize physical layer. Call this function before any application layer code. */ mTouchCapAPI_Init(); // Initial startup demo of board LEDs. PORTB = PORTB_ROW_ALL | PORTB_COL_ALL; //Turn on all LEDs PORTC = PORTC_ROW_ALL | PORTC_COL_ALL; DelayMs(1000); mTouchCapLED_PowerUpSequence(); // turn on each LED in sliding motion PORTB = PORTB_ROW_ALL | PORTB_COL_ALL; //Turn on all LEDs PORTC = PORTC_ROW_ALL | PORTC_COL_ALL; DelayMs(1000); PORTB = PORTB_ROW_NIL | PORTB_COL_NIL; //Turn off all LEDs PORTC = PORTC_ROW_NIL | PORTC_COL_NIL; // Start button measurements Set_ScanTimer_IE_Bit_State(ENABLE); //Enable interrupt Set_ScanTimer_ON_Bit_State(ENABLE); //Run timer // Setup for detection/interpretation LastButtonStatus = 0; while(1) { if(EmTouchDataReady == 1) //This flag is set by Timer ISR when all channels have been read { // Calculate new button values and return the measurement updates mTouchCapPhy_UpdateData(CurrentButtonMeasurements, CurrentAveragedMeasurements, CurrentWeights, TRUE, FILTER_METHOD_USECHANNEL); // Update button and LED status and return button status for use with mTouch GUI on PC CurrentButtonStatus = mTouchCapApp_MatrixKeys_Demo(); // Check for button asserts: If buttons were pressed before, but none pressed now if ( LastButtonStatus > 0 && CurrentButtonStatus == 0 ) { CurrentButtonAsserts = LastButtonStatus; //Buttons have fired. } else { // Commented out to provide memory of last button asserted. //ButtonAsserts = 0; } /* Determining Channel Load Order: Button Layout: Column: Col 1 Col 2 Col 3 Col 4 AN0 AN1 AN4 AN5 Row 1: AN8 Btn 0 Btn 1 Btn 2 Btn 3 Row 2: AN7 Btn 4 Btn 5 Btn 6 Btn 7 Row 3: AN6 Btn 8 Btn 9 Btn10 Btn11 Resulting load Order (AN8,AN0), (AN8,AN1), (AN8,AN4), (AN8,AN5), (AN7,AN0), (AN7,AN1), (AN7,AN4), (AN7,AN5), (AN6,AN0), (AN6,AN1), (AN6,AN4), (AN6,AN5), Removing channels already loaded: (AN8,AN0), (---,AN1), (---,AN4), (---,AN5), (AN7,---), (---,---), (---,---), (---,---), (AN6,---), (---,---), (---,---), (---,---), So the channels are loaded into memory in this order: Channel Load: #0, #1, #2, #3, #4, #5, #6 ADC Channel: AN8, AN0, AN1, AN4, AN5, AN13, AN12 Button Func: Row1, Col1, Col2, Col3, Col4, Row2, Row3 Sorting to output rows in order followed by columns: Channel Load: #0, #5, #6, #1, #2, #3, #4, Button Func: Row1, Row2, Row3, Col1, Col2, Col3, Col4, This order is used below to output measurements to the mTouch GUI or to a hyperterminal for later analysis. */ # if defined( UART_DUMP_RAW_COUNTS ) for ( iHF_Read = 0; iHF_Read < NUM_HF_READS; iHF_Read++ ) # elif defined( UART_DUMP_ALL_COUNTS ) for ( iHF_Read = 0; iHF_Read < 3*NUM_HF_READS; iHF_Read++ ) # endif # if defined( UART_DUMP_RAW_COUNTS ) || defined( UART_DUMP_ALL_COUNTS ) { sprintf(ButtonMeasString, "%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d\r\n", CurrentButtonStatus,CurrentButtonAsserts, CurRawData[iHF_Read][0],CurRawData[iHF_Read][5],CurRawData[iHF_Read][6], 0, CurRawData[iHF_Read][1],CurRawData[iHF_Read][2],CurRawData[iHF_Read][3],CurRawData[iHF_Read][4], 0,0,0,0, 0,0,0,0, iHF_Read+1); SendDataBuffer(ButtonMeasString, strlen(ButtonMeasString) ); } # endif #if defined( DUMP_DIAGNOSTICS_VIA_UART ) // Format report string sprintf(ButtonMeasString, "%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d,%05d,%05d,%05d\r\n", CurrentButtonStatus, CurrentButtonAsserts, 0, // Zero for slider value CurrentButtonMeasurements[0], //Row 1: AN14: Load #0 CurrentButtonMeasurements[5], //Row 2: AN13: Load #5 CurrentButtonMeasurements[6], //Row 3: AN12: Load #6 0, //Placeholder for unused row CurrentButtonMeasurements[1], //Col 1: AN8: Load #1 CurrentButtonMeasurements[2], //Col 2: AN9: Load #2 CurrentButtonMeasurements[3], //Col 3: AN10: Load #3 CurrentButtonMeasurements[4]);//Col 4: AN11: Load #4 // Send report string back to mTouch GUI via UART #2 SendDataBuffer(ButtonMeasString, strlen(ButtonMeasString) ); # endif//defined( DUMP_DIAGNOSTICS_VIA_UART ) LastButtonStatus = CurrentButtonStatus; //Remember button status EmTouchDataReady = 0; //clear flag Set_ScanTimer_IE_Bit_State(ENABLE); //Enable interrupt Set_ScanTimer_ON_Bit_State(ENABLE); //Run timer } // end if(EmTouchDataReady) } //end while (1) } // end main()
int main(void) { #if defined( DUMP_DIAGNOSTICS_VIA_UART ) char ButtonMeasString[133]; # if defined( UART_DUMP_RAW_COUNTS ) || \ defined( UART_DUMP_ALL_COUNTS ) UINT16 iHF_Read; # endif//defined( UART_DUMP_RAW_COUNTS )... #endif//defined( DUMP_DIAGNOSTICS_VIA_UART ) UINT16 iButton; UINT16 CurrentButtonStatus, LastButtonStatus, CurrentButtonAsserts = 0, CurrentButtonMeasurements[MAX_ADC_CHANNELS], CurrentAveragedMeasurements[MAX_ADC_CHANNELS]; UINT16 CurrentWeights[MAX_ADC_CHANNELS]; UINT16 WeightSum, RawSliderValue = 0, WeightDeviation; INT16 SliderValue = 0, CorrectedSliderValue = 0; static INT16 LastSliderValue = 0; DDPCONbits.JTAGEN = 0; // Disable JTAG // Initalize global interrupt enable INTEnableSystemMultiVectoredInt(); // Configure the device for maximum performance // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option. SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); #if defined( DUMP_DIAGNOSTICS_VIA_UART ) // Setup UART2 for transmission of button data. // Taken from C:\Program Files\Microchip\mplabc32\v1.12\examples\plib_examples\uart\uart_basic UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART2, GetPeripheralClock(), 115200); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); #endif//defined( DUMP_DIAGNOSTICS_VIA_UART ) // Setup data structures for using Direct Keys mTouchCapApp_DirectKeys_Create(); /* This is mandatory function call to initilize physical layer. Call this function before any application layer code. */ mTouchCapAPI_Init(); // Run "Chaser" LED sequence on slider LEDs to announce start of application mTouchCapLED_PowerUpSequence(); #if defined( TICKLE_LED1 ) TRISFCLR = 1<<0; #endif//defined( TICKLE_LED1 ) // Start button measurements Set_ScanTimer_IE_Bit_State(ENABLE); //Enable interrupt Set_ScanTimer_ON_Bit_State(ENABLE); //Run timer #if defined( TICKLE_LED1 ) TRISFCLR = 1<<0; // Use pin 5 on J2 (RF0) #endif// defined( TICKLE_LED1 ) // Setup for detection/interpretation LastButtonStatus = 0; while(1) { if(EmTouchDataReady == 1) //This flag is set by Timer ISR when all channels have been read { // Calculate new button values and return the measurement updates mTouchCapPhy_UpdateData(CurrentButtonMeasurements, CurrentAveragedMeasurements, CurrentWeights, TRUE, FILTER_METHOD_USECHANNEL); // Update button and LED status and return button status for use with mTouch GUI on PC CurrentButtonStatus = mTouchCapApp_DirectKeys_Demo(); // Check for button asserts: If buttons were pressed before, but none pressed now if ( LastButtonStatus > 0 && CurrentButtonStatus == 0 ) { CurrentButtonAsserts = LastButtonStatus; //Buttons have fired. } else { // Commented out to provide memory of last button asserted. //ButtonAsserts = 0; } if ( CurrentButtonStatus > 0 ) // Something is happening on the buttons { // Calculate slider value for buttons as weighted average of weight deviation from max WeightSum = 0; RawSliderValue = 0; for ( iButton = 0; iButton < NUM_DIRECT_KEYS ; iButton++ ) { WeightDeviation = 256 - CurrentWeights[iButton]; WeightSum += WeightDeviation; RawSliderValue += WeightDeviation*(iButton+1); } RawSliderValue = (256*(RawSliderValue/NUM_DIRECT_KEYS))/WeightSum; CorrectedSliderValue = NUM_DIRECT_KEYS*((INT16)RawSliderValue-(256/NUM_DIRECT_KEYS))/(NUM_DIRECT_KEYS-1); // Correct for 1st button CorrectedSliderValue = (CorrectedSliderValue < 0 ) ? 0 : CorrectedSliderValue; CorrectedSliderValue = (CorrectedSliderValue > 256) ? 256 : CorrectedSliderValue; } else // Nothing is happening, put slider to sleep { RawSliderValue = 0; CorrectedSliderValue = 0; } SliderValue = ( CorrectedSliderValue + ((1<<2) - 1)*LastSliderValue )>>2; LastSliderValue = SliderValue; # if defined( UART_DUMP_RAW_COUNTS ) for ( iHF_Read = 0; iHF_Read < NUM_HF_READS; iHF_Read++ ) # elif defined( UART_DUMP_ALL_COUNTS ) for ( iHF_Read = 0; iHF_Read < 3*NUM_HF_READS; iHF_Read++ ) # endif # if defined( UART_DUMP_RAW_COUNTS ) || defined( UART_DUMP_ALL_COUNTS ) { sprintf(ButtonMeasString, "%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d\r\n", CurrentButtonStatus, CurrentButtonAsserts, SliderValue, CurRawData[iHF_Read][0],CurRawData[iHF_Read][1],CurRawData[iHF_Read][2],CurRawData[iHF_Read][3], CurRawData[iHF_Read][4],CurRawData[iHF_Read][5],CurRawData[iHF_Read][6],CurRawData[iHF_Read][7], 0,0,0,0, iHF_Read+1); SendDataBuffer(ButtonMeasString, strlen(ButtonMeasString) ); } # endif # if defined( DUMP_DIAGNOSTICS_VIA_UART ) sprintf(ButtonMeasString, "%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d,%05d,%05d,%05d," "%05d,%05d,%05d\r\n", CurrentButtonStatus, CurrentButtonAsserts, SliderValue, CurrentButtonMeasurements[0], CurrentButtonMeasurements[1], CurrentButtonMeasurements[2], CurrentButtonMeasurements[3], CurrentButtonMeasurements[4], CurrentButtonMeasurements[5], CurrentButtonMeasurements[6], CurrentButtonMeasurements[7], RawSliderValue, CorrectedSliderValue, SliderValue); SendDataBuffer(ButtonMeasString, strlen(ButtonMeasString) ); // Diagnostics for Buttons 1-6, with current and averaged button voltages // sprintf( ButtonMeasString, // "%05d,%05d,%05d," // "%05d,%05d,%05d,%05d," // "%05d,%05d,%05d,%05d," // "%05d,%05d,%05d,%05d" // "\r\n", // CurrentButtonStatus, CurrentButtonAsserts, SliderValue, // CurrentButtonMeasurements[0],CurrentAveragedMeasurements[0], // CurrentButtonMeasurements[1],CurrentAveragedMeasurements[1], // CurrentButtonMeasurements[2],CurrentAveragedMeasurements[2], // CurrentButtonMeasurements[3],CurrentAveragedMeasurements[3], // CurrentButtonMeasurements[4],CurrentAveragedMeasurements[4], // CurrentButtonMeasurements[5],CurrentAveragedMeasurements[5]); // SendDataBuffer(ButtonMeasString, strlen(ButtonMeasString) ); # endif//defined( DUMP_DIAGNOSTICS_VIA_UART ) LastButtonStatus = CurrentButtonStatus; //Remember button status EmTouchDataReady = 0; //clear flag Set_ScanTimer_IE_Bit_State(ENABLE); //Enable interrupt Set_ScanTimer_ON_Bit_State(ENABLE); //Run timer } // end if(EmTouchDataReady) } //end while (1)