Akhil Mohan
Member level 2
- Joined
- May 13, 2011
- Messages
- 53
- Helped
- 2
- Reputation
- 4
- Reaction score
- 2
- Trophy points
- 1,288
- Activity points
- 1,731
Hello all,
I am trying to display the touch positions from a resistive touch screen on a 16x2 LCD using PIC18LF4550. I got a sample code from http://www.pyroelectro.com/tutorials/touchscreen_interface_resistive/software.html
I have made few modifications so that I can operate with 18LF4550. I am using HITECH PICC18-PRO compiler. I am not able to understand the code fully esp the importance of the functions void change_led(int); and the Update routine. On power up, it displays X : and Y: and nothing else. I am finding it difficult to trouble shoot and please help me to understand the code and to make it work
The hardware connections are,
The code follows,
Thanks in Advance
Akhil
I am trying to display the touch positions from a resistive touch screen on a 16x2 LCD using PIC18LF4550. I got a sample code from http://www.pyroelectro.com/tutorials/touchscreen_interface_resistive/software.html
I have made few modifications so that I can operate with 18LF4550. I am using HITECH PICC18-PRO compiler. I am not able to understand the code fully esp the importance of the functions void change_led(int); and the Update routine. On power up, it displays X : and Y: and nothing else. I am finding it difficult to trouble shoot and please help me to understand the code and to make it work
The hardware connections are,
The code follows,
Code:
#include <p18f4550.h>
#include <delays.h>
#include <htc.h>
#include <stdlib.h>
#include <ctype.h>
#define rs LATCbits.LATC0
#define rw LATCbits.LATC1
#define en LATCbits.LATC2
#define _XTAL_FREQ 20000000
__CONFIG (1, FOSC_HS);
__CONFIG (2, WDTDIS & PWRTEN & BOREN & LVPDIS);
void prnt(unsigned int);
void commd(unsigned int);
void lcd_init(void);
void change_led(int);
void Delay10KTCYx(unsigned char unit);
void Delay1KTCYx(unsigned char unit);
void main(void)
{
int i=0;
int result_x=0;
int result_y=0;
char x_axis[6];
char y_axis[6];
nRBPU = 1;
//ADCON1 = 0b00000000;
TRISA = 0x00;
TRISB = 0x0F;
TRISC = 0x00;
TRISD = 0x00;
PORTA = 0x03;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
Delay10KTCYx(200);
lcd_init();
prnt('X');
prnt(':');
commd(0b11000000); //Move 2nd Next Line
prnt('Y');
prnt(':');
commd(0b10000011);
while(1)
{
//Set PORTA To Inputs/High Impedance
TRISAbits.TRISA0 = 1;
TRISAbits.TRISA1 = 1;
//Set Lower 2 Bits to High Impedance
TRISBbits.TRISB0 = 1;
TRISBbits.TRISB3 = 1;
//Set Higher 2 Bits to Output
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB2 = 0;
PORTBbits.RB0 = 0;
//Provide GND To X-axis Of Touch Screen
PORTBbits.RB1 = 0;
//Provide Power To X-axis Of Touch Screen
PORTBbits.RB2 = 1;
PORTBbits.RB3 = 0;
// configure A/D convertor
ADCON0 = 0x05;
ADCON1 = 0x00;
ADCON2 = 0x82;
Delay10KTCYx( 5 ); // Delay for 50TCY
GO_nDONE = 1; // Code for starting A/D conversion
while (GO_nDONE != 0); // Wait until the A/D conversion is over
result_y = ADRESH; // Read result
//ADON = 0; // ADC is disabled
//Set PORTA To Inputs/High Impedance
TRISAbits.TRISA0 = 1;
TRISAbits.TRISA1 = 1;
//Set Lower 2 Bits As Outputs
TRISBbits.TRISB0 = 0;
TRISBbits.TRISB3 = 0;
//Set Higher 2 Bits to High Impedance
TRISBbits.TRISB1 = 1;
TRISBbits.TRISB2 = 1;
//Provide Power To Y-axis Of Touch Screen
PORTBbits.RB0 = 0;
PORTBbits.RB1 = 0;
PORTBbits.RB2 = 0;
//Provide Ground To Y-axis Of Touch Screen
PORTBbits.RB3 = 1;
ADCON0 = 0x01;
ADCON1 = 0x00;
ADCON2 = 0x82;
Delay10KTCYx( 5 ); // Delay for 50TCY
GO_nDONE = 1; // Code for starting A/D conversion
while (GO_nDONE != 0); // Wait until the A/D conversion is over
result_x = ADRESH; // Read result
//ADON = 0; // ADC is disabled
//Clear Everything In Port C
PORTB = 0x00;
//All High Impedance Outputs
TRISBbits.TRISB0 = 1;
TRISBbits.TRISB1 = 1;
TRISBbits.TRISB2 = 1;
TRISBbits.TRISB3 = 1;
//Don't Display If X or Y Axis Not Active
if( (result_x > 80 && result_x < 900 ) )
{
/******** X-Axis Update ALL *********/
itoa( result_x, x_axis,8);
for(i=0;i<4;i++)
{
//Update Display
if(isalnum(x_axis[i]))
{
prnt(x_axis[i]);
}
else
{
prnt(0x20);
}
}
commd(0b11000011); //Go To 2nd Line 4th Character
/******** Y-Axis Update ALL *********/
itoa( result_y, y_axis,8);
for(i=0;i<4;i++)
{
//Update Display
if(isalnum(y_axis[i]))
{
prnt(y_axis[i]);
}
else
{
prnt(0x20);
}
}
//Short Delay
Delay10KTCYx(10);
//Back To First Line, 4th Character
commd(0b10000011);
}
else
{
Delay10KTCYx(10);
prnt(0x20);
prnt(0x20);
prnt(0x20);
prnt(0x20);
commd(0b11000011);
prnt(0x20);
prnt(0x20);
prnt(0x20);
prnt(0x20);
commd(0b10000011);
}
}
}
void change_led(int result)
{
if(result > 30 && result < 90){
PORTBbits.RB0 = 1;
PORTBbits.RB1 = 0;
}
else if(result > 100 && result < 140){
PORTBbits.RB0 = 1;
PORTBbits.RB1 = 1;
}
if(result > 150 && result < 190){
PORTBbits.RB0 = 0;
PORTBbits.RB1 = 1;
}
if(result > 200 && result < 250)
{
PORTBbits.RB0 = 0;
PORTBbits.RB1 = 0;
}
}
void lcd_init(void)
{
en = 0;
rs = 0;
rw = 0;
Delay10KTCYx( 10 ); // 20ms delay
commd(0x38); // Set interface to 8-bit
Delay10KTCYx( 5 ); // 10ms delay
commd( 0x38 ); // Set interface to 8-bit
Delay10KTCYx( 1 ); // 2ms delay
commd( 0x38 ); // Set interface to 8-bit
Delay10KTCYx( 1 ); // 2ms delay
commd( 0x28 ); // Set interface to 4-bit
commd( 0x2C ); // Function Set
commd(0x0C); // Display On and Cursor Off
commd(0x01); // Clear display screen
Delay10KTCYx( 5 ); // 10ms delay
commd(0x06); // Increment cursor
}
//4-bit print interface
void prnt(unsigned int character)
{
PORTD=character >> 4;
rs = 1;
rw = 0;
en = 1;
en = 0;
PORTD=character & 0x0f;
rs = 1;
rw = 0;
en = 1;
en = 0;
Delay1KTCYx(1);
}
//4-bit instruction interface
void commd(unsigned int commd)
{
PORTD=commd >> 4;
rs = 0;
rw = 0;
en = 1;
en = 0;
PORTD=commd &0x0f;
rs = 0;
rw = 0;
en = 1;
en = 0;
Delay1KTCYx(1);
}
Thanks in Advance
Akhil