ADS7843/XPT2046 touch screen chip 8051 MCU driver

Publisher:电子艺术大师Latest update time:2017-01-03 Source: eefocusKeywords:ADS7843 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The size of my color screen is 240*320~

Following is the code


//common.h

#ifndef __COMMON_H__

#define __COMMON_H__


#include

#include


typedef unsigned int uint;

typedef unsigned char uchar;


#endif // !__COMMON_H__


//TouchPad.c

#define __TOUCHPAD_C__

#include "TouchPad.h"

//My screen interface

//#include "gdi.h"


sbit TP_CLK = P1^4; //clock

sbit TP_CS = P1^5; //Chip select

sbit TP_IN = P1^6; //Data input

//sbit TP_BUSY = P1^3; //I don't use the busy flag

sbit TP_OUT = P1^7; //data output

sbit TP_IRQ = P3^2; //Notify MCU via INT0


/*

 * Function: Check whether the touch screen is in interruption and call the detection function in the interruption

 * Parameters: (none)

 * Return: 0-No, non-0: the touch screen is pressed

 */

uchar tp_isirq(void)

{

    return !(uchar)TP_IRQ;

}


// Enable external interrupt 1 and notify MCU through external interrupt

void tp_init(void)

{

    EX0 = 1;

    IT0 = 1;

    EA = 1;

    TP_CS = 1;

    TP_CLK = 1;

    TP_IN = 1;

    TP_CLK = 1;

}

/* Function: write a byte in SPI mode

 * Parameters: ch: uchar: byte value

 * Returns: (None)

 */

void tp_write(uchar ch)

{

    uchar k;

    TP_CLK = 0;

    for(k=0; k<8; k++){

        ch <<= 1;

        TP_IN = CY;

        TP_CLK = 0;

        _nop_();

        TP_CLK = 1;

        _nop_();

    }

}

/*

 * Function: SPI mode read function

 * Parameters: (none)

 * Return value: 12 bits of valid data

 */

uint tp_read(void)

{

    uchar k;

    uint ret = 0;

    for(k=0; k<12; k++){

        ret <<= 1;

        TP_CLK = 1;

        _nop_();

        TP_CLK = 0;

        _nop_();

        if(TP_OUT)

            ret++;

    }

    return ret;

}


/*

 * Function: Get X, Y coordinates, but no comparison sampling

 * Parameter: struct tp_point structure pointer

 * Returns: (None)

 * Note: Do not call externally, there will be errors

 */

void tp_get_pt(struct tp_point* pt)

{

    TP_CS = 0;

    tp_write(0x90); //Read Y

    TP_CLK = 1;

    _nop_();

    TP_CLK = 0;

    _nop_();

    pt->y = tp_read();

    tp_write(0xd0); //Read X

    TP_CLK = 1;

    _nop_();

    TP_CLK = 0;

    _nop_();

    pt->x = tp_read();

    TP_CS = 1;

}


/*

 * Function: Get the touch screen coordinate function

 * Parameter: int type x, y pointer

 * return value:

 * Return 0 - sampling failed

 * Returns non-zero valid samples

*/

int tp_get(int* px, int* py)

{

    struct tp_point pt1;

    struct tp_point pt2;

    int x,y;

    //Twice the value and sample

    tp_get_pt(&pt1);

    tp_get_pt(&pt2);

    x = pt1.x-pt2.x;

    y = pt1.y-pt2.y;

    //The error is not too big, then it is valid

    if(x<2 && y<2)

    {

        //This calculation is not clear yet.

        *px = 240-(pt1.x-330)*10/142;

        *py = 320-(pt1.y-380)/11;

        return 1;

    }

    return 0;

}


//TouchPad.h

/*

    File:TouchPad.h

    Description: Touch screen related support files

    Text:ts=4, sw=4, sts=4

*/



#ifndef __TOUCHPAD_H__

#define __TOUCHPAD_H__

#include "common.h"

//common.h

/*

typedef unsigned char uchar;

typedef unsigned int uint;

#include

*/



int tp_get(int* px, int* py); //Get the current coordinate point, the return value is not zero if it is valid

void tp_init(void); //touch screen initialization function

uchar tp_isirq(void); //Judge whether the touch screen is still in interruption


#ifdef __TOUCHPAD_C__

//Internal structure, function

struct tp_point{

    int x;

    int y;

};


void tp_get_pt(struct tp_point* pt);

uint tp_read(void);

void tp_write(uchar ch);


#undef __TOUCHPAD_C__

#endif




#endif // !__TOUCHPAD_H__


//Example usage

//main.c

#include "common.h"

#include "TouchPad.h"


//The touch screen notifies the MCU to sample via external interrupt 0

//Should be written in the main module

void int0(void) interrupt 0

{

    int x=0,y=0;

    EA = 0;

    while(1){

        if(tp_isirq()){

            if(!tp_get(&x, &y))     

                continue;   

            //x,y are the coordinates obtained

            //set_pixel is my screen drawing function

            //Change to your own usage method

            //set_pixel(x,y, 0xaaaa);

            //set_pixel(x+1,y, 0xaaaa);

            //set_pixel(x,y+1, 0xaaaa);

            //set_pixel(x+1,y+1, 0xaaaa);

        }else{

            break;

        }

    }

    EA = 1;

}

        



void main(void)

{

    //delay_ms(200);//self-implementation

    tp_init();

    while(1)

        ;

}



Keywords:ADS7843 Reference address:ADS7843/XPT2046 touch screen chip 8051 MCU driver

Previous article:51 program to drive 1602 LCD display
Next article:Digital clock implemented by single chip microcomputer

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号