Ads7843/xpt2046 touch screen chip 8051 microcontroller driver

Source: Internet
Author: User

The size of my color screen is 240*320 ~
The following code is used:

//common.h#ifndef __COMMON_H__#define __COMMON_H__#include <reg52.h>#include <intrins.h>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; // select sbit tp_in = p1 ^ 6; // data input // sbit tp_busy = p1 ^ 3; // I have not used the busy sign sbit tp_out = p1 ^ 7; // data output sbit tp_irq = P3 ^ 2; // use int0 to notify the MCU/** function: checks whether the touch screen is interrupted, and calls the detection * parameter during the interruption: (none) * return: 0-None, not 0: the touch screen is pressed */uchar tp_isirq (void) {return! (Uchar) tp_irq;} // enable external interrupt 1. The mcuvoid 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 * parameter: CH: uchar: byte value * return: (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 read function * parameter: (none) * return value: 12-bit 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: obtains the coordinates of X and Y, but does not compare sampling * parameter: struct pointer of struct tp_point * return :( none) * Note: Do not call outside, error */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: Obtain the touch screen Coordinate Function * parameter: int X, Y pointer * return value: * return 0-sampling failed * return non-0-valid sampling */INT tp_get (int * PX, int * Py) {struct tp_point pt1; struct tp_point pt2; int X, Y; // sample tp_get_pt (& pt1); tp_get_pt (& pt2); X = pt1.x-pt2.x; y = pt1.y-pt2.y; // if the error is not large, the calculation is still unclear, * 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 support documentation time: 2012-11-02 Author: girls don't cry Q--Q: 191035066 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 <reg52.h> */INT tp_get (int * PX, int * Py); // obtain the current coordinate point, the return value is not zero. Valid void tp_init (void); // The ar tp_isirq (void) function of the touch screen initialization function ); // determine whether the touch screen is still interrupted # ifdef _ touchpad_c _ // internal structure, function stru CT 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 __
// Usage example // main. C # include "Common. H "# include" touchpad. H "// the touch screen notifies the MCU of sampling by means of external interruption 0 // It 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 is the obtained coordinate // set_pixel is the function of my screen painting point // change it to your own usage // set_pixel (X, Y, 0 xaaaa ); // set_pixel (x + 1, Y, 0 xaaaa); // set_pixel (X, Y + 1, 0 xaaaa); // set_pixel (x + 1, Y + 1, 0 xaaaa);} else {break;} Ea = 1;} void main (void) {// delay_ms (200); // implement tp_init () by yourself (); while (1 );}

Girl don't cry (QQ: 191035066) @ 2012-11-26 14:40:46 http://www.cnblogs.com/nbsofer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.