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)
;
}
Previous article:51 program to drive 1602 LCD display
Next article:Digital clock implemented by single chip microcomputer
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- New wireless broadband technologies in the 5G era
- Design and implementation of temperature control system based on MSP430
- [Erha Image Recognition Artificial Intelligence Vision Sensor] Evaluation 2: Built-in 7 functions, face recognition and other tests
- 【Xianji HPM6750】Development process using Embedded Studio
- 【TouchGFX Design】Decomposition of the generated project directory structure and recommendation of two C++ introductory books
- LIS25BA package and evaluation board files
- L298N output voltage problem
- KiCad KiCost GOOD
- Support Xilinx/Altera/Lattice JTAG and UART
- R329 development board series tutorial | Running aipu program on real machine