// This ISR will execute whenever Timer1 has a compare match. // it kicks off the periodic execution of user code and performs I/O // Min period: 10msec due to X,Y switch time for touchscreen void __attribute__((interrupt, auto_psv)) _T1Interrupt(void) { IFS0bits.T1IF = 0; // clear interrupt flag if(start == 1) deadline_miss++; if (select == X_DIM) { // DONE: read 5 samples from X-dimension and set Xpos as the median int i = 0; for (i = 0; i<N; i ++){ xVal[i] = smooth(touch_adc(1), TOUCH_MAX_X, TOUCH_MIN_X,xPrevVal); // xVal[i] = touch_adc(1);//cap(touch_adc(1), TOUCH_MAX_X, TOUCH_MIN_X); } Xpos = median(xVal,N); xPrevVal = Xpos; touch_select_dim(Y_DIM); select = Y_DIM; } else { // DONE: read 5 samples from Y-dimension and set Ypos as the median int i = 0; for (i = 0; i<N; i ++){ yVal[i] = smooth(touch_adc(2), TOUCH_MAX_Y, TOUCH_MIN_Y,yPrevVal); // yVal[i] = touch_adc(2);//cap(touch_adc(2), TOUCH_MAX_Y, TOUCH_MIN_Y); } Ypos = median(yVal,N); yPrevVal = Ypos; touch_select_dim(X_DIM); select = X_DIM; } start = 1; }
void main(){ //Init LCD __C30_UART=1; lcd_initialize(); lcd_clear(); touch_init(); CLEARBIT(T1CONbits.TON); // Disable Timer CLEARBIT(T1CONbits.TCS); // Select internal instruction cycle clock CLEARBIT(T1CONbits.TGATE); // Disable Gated Timer mode TMR1 = 0x00; // Clear timer register T1CONbits.TCKPS = 0b10; // Select 1:64 Prescaler PR1 = 2000; // Load the period value IPC0bits.T1IP = 0x01; // Set Timer1 Interrupt Priority Level CLEARBIT(IFS0bits.T1IF); // Clear Timer1 Interrupt Flag SETBIT(IEC0bits.T1IE); // Enable Timer1 interrupt while(1) { int i = 0; touch_select_dim(0); lock = 1; TMR1 = 0x00; SETBIT(T1CONbits.TON); // Start Timer while(lock); for (i = 0; i < 5; i++) { xs[i] = touch_adc(); } // read y touch_select_dim(1); lock = 1; TMR1 = 0x00; SETBIT(T1CONbits.TON); // Start Timer while(lock); for (i = 0; i < 5; i++) { ys[i] = touch_adc(); } qsort(xs, 5, sizeof(uint16_t), cmpfunc); qsort(ys, 5, sizeof(uint16_t), cmpfunc); lcd_locate(0,0); lcd_printf("x position: "); lcd_locate(0,0); lcd_printf("x position: %d", xs[2]); lcd_locate(0,1); lcd_printf("y position: "); lcd_locate(0,1); lcd_printf("y position: %d", ys[2]); } }