// // TOUCH PANEL and LCD TEST (DIGIT) // // 2022-Jun-15Apr-7 #include "18F26K83.H" #device ADC=12 #fuses RSTOSC_HFINTRC_64MHZ,PROTECT,NOMCLR,NOBROWNOUT,NOLVP #use delay(internal=32MHz) #use fast_io(A) #use fast_io(B) #use fast_io(C) #use fast_io(E) #define LCD_SC_RS PIN_C3 // LCD PORT DEFINITION RS #define LCD_SC_E PIN_C2 // LCD PORT DEFINITION E #define LCD_SC_D4 PIN_C4 // LCD PORT DEFINITION DB4 #define LCD_SC_D5 PIN_C5 // LCD PORT DEFINITION DB5 #define LCD_SC_D6 PIN_C6 // LCD PORT DEFINITION DB6 #define LCD_SC_D7 PIN_C7 // LCD PORT DEFINITION DB6 #define TP_UP_EDGE PIN_A2 #define TP_DOWN_EDGE PIN_A0 #define TP_LEFT_EDGE PIN_A3 #define TP_RIGHT_EDGE PIN_A1 #define TP_DETECT_X (0) #define TP_DETECT_Y (1) #include "lcdlib.c" // LCD control routines int16 get_ad() // input AD single time { int1 done; int16 value; read_adc(ADC_START_ONLY); done = adc_done(); while(!done) done=adc_done(); value=read_adc(ADC_READ_ONLY); return(value); } int16 get_ad16() // input AD 16 times and average { int16 v; int i; v=0; for(i=0;i<16;i++) v+=get_ad(); v= v >> 4; return(v); } // TOUCH PANEL: GET POSITION // int16 touch_get_x() { int16 pos; output_high(TP_RIGHT_EDGE); output_low(TP_LEFT_EDGE); output_drive(TP_RIGHT_EDGE); output_drive(TP_LEFT_EDGE); set_adc_channel(TP_DETECT_X); delay_us(20); pos=get_ad16(); output_float(TP_RIGHT_EDGE); output_float(TP_LEFT_EDGE); return(pos); } int16 touch_get_y() { int16 pos; output_high(TP_DOWN_EDGE); output_low(TP_UP_EDGE); output_drive(TP_DOWN_EDGE); output_drive(TP_UP_EDGE); set_adc_channel(TP_DETECT_Y); delay_us(20); pos=get_ad16(); output_float(TP_DOWN_EDGE); output_float(TP_UP_EDGE); return(pos); } // io_select() I/O definision for PIC // void io_select() { set_tris_a(0xff);set_tris_b(0xff);set_tris_c(0xff); // all port input output_drive(LCD_SC_RS); output_drive(LCD_SC_E); // LCD CONTROL output_drive(LCD_SC_D4); output_drive(LCD_SC_D5); // LCD DATA output_drive(LCD_SC_D6); output_drive(LCD_SC_D7); // LCD DATA setup_adc(ADC_CLOCK_DIV_64 | ADC_TAD_MUL_16); setup_adc_ports(sAN0 | sAN1); output_float(TP_UP_EDGE); output_float(TP_DOWN_EDGE); output_float(TP_RIGHT_EDGE); output_float(TP_LEFT_EDGE); } void main() { int16 x,y; setup_oscillator(OSC_HFINTRC_8MHZ); // 内部クロック 8MHz (SLOW START UP) delay_ms(10); setup_oscillator(OSC_HFINTRC_32MHZ); // 内部クロック 32MHz delay_ms(500); // wait for POWER UP io_select(); // IO definition lcd_init(); // INITIALIZE LCD lcd_home(); lcd_position(0,0); printf(lcd_char,"DIGIT TOUCH PANEL POSITION %4ldX OKIRAKU",0); lcd_position(0,1); printf(lcd_char,"digit touch panel position %4ldy okiraku",0); while(1){ x = touch_get_x(); y = touch_get_y(); lcd_position(27,0); printf(lcd_char,"%4ld",x); lcd_position(27,1); printf(lcd_char,"%4ld",y); } } // end of file