The touch screen is a four-wire resistive type, and the driver chip uses the very common ADS7846. The typical application circuit diagram of ADS7846 is shown in the figure below.
In the author's application, pin7 and pin8 are directly connected to GND, that is, the auxiliary input channel is not used, and pin9 and pin10 are connected together, that is, VCC is used as the analog-to-digital conversion reference voltage source of ADS7846. The pull-up resistor connected to pin11 can be omitted, but the corresponding AVR input port pull-up resistor must be set to enable. Pin13 is used as a conversion end indication. The level of this pin can be judged to determine whether the conversion data can be read, or a simple delay method can be used to leave enough conversion time. Pin16, pin15, pin14, and pin12 are connected to the mega16 chip as a standard SPI slave interface.
ADS7846 supports 8-bit accuracy and 12-bit accuracy, that is, the touch resolution can reach 1/256 or 1/4096. The corresponding touch accuracy is selected according to the LCD with different resolutions. For example, a 128×64 LCD can use 8-bit precision, and a 320×240 LCD needs 12-bit precision. The collected data is read out twice, with 8-bit precision first getting the first 7 bits and then the last bit, and with 12-bit precision first getting the first 7 bits and then the last 5 bits.
The program segment is as follows: (the compiler uses ICCAVR)
/****************************************************
SPI Interface file
crystal: 8MHz
write by han, hanembed@126.com, http://embed.hanyuer.net
*******************************************************/
#include
#include
/*=======================================================
// function: initialize spi interface
// in: void
// retun: void
// date: 2005/8/10
========================================================*/
void spiinit(void)
{
DDRB = (1 << PB4) | (1 << PB5) | (1 << PB7); // MOSI and SCK port out
PORTB |= (1 << PB4);
SPCR = (1 << SPE) | (1 << MSTR) | (0 << SPR0); // enable spi,master mode, MCLK/4,spi 0 mode
}
/*============================================ ==
// function: send data form spi interface
// in: unsigned char real data
// retun: void
// date: 2005/8/10
==================================================*/
void sendspi(unsigned char data)
{
SPDR = data; // send data
while( !(SPSR & (1 << SPIF)) ); // wait data transfer end
}
/*======================================================
// function: receive data form spi interface
// in: void
// retun: unsigned char
// date: 2005/8/10
========================================= ========*/
unsigned char readspi(void)
{
return SPDR;
}
/******************************************************
touch data read file
crystal: 8MHz
write by han, hanembed@126.com, http://embed.hanyuer.net
*****************************************************/
#include
#include
#include "..incspi.h"
unsigned int positionx;
unsigned int positiony;
unsigned char flgtouch; iv_INT1 /*
==============================================
// function: initialize all used port
// in: void
// retun: void
// date: 2005/8/10
======================= ================================*/
void portini(void)
{
spiinit();
endspi();
DDRD & = ~(1 << PD3); // port input
PORTD |= (1 << PD3); // pull-up resistance
//MCUCR |= 1< GICR |= 1<< INT1; // extern interrupt 1 enable
flgtouch = 0;
}
/*==================================== ===============
// function: small delay
// in: unsigned char delay times
// retun: void
// date: 2005/8/10
================================ ===================*/
void smalldelay(unsigned char tmp)
{
unsigned char i;
while(tmp--)
{
for(i = 0; i < 250; i++)
{
NOP();
}
}
}
/*======================================== ==========
// function: read touch data
// in: void
// retun: void
// date: 2005/8/10
================================================== =*/
void keydown(void)
{
unsigned char tmp; // temporary data
smalldelay(20); // delay wait tranquilization
startspi(); // begin data transfer
smalldelay(1);
sendspi(0x90); // difference conversion , x data
smalldelay(2); // delay wait x conversion
sendspi(0x00);
tmp = readspi(); // first 7 bit x data
if(tmp == 0x7F) // error read
return;
positionx = tmp;
positionx <<= 5; // left shift 5 bit
sendspi(0xD0); // difference conversion, y data
tmp = readspi(); // last 5 bit x data
tmp >>= 3; // right shift 3 bit
positionx + = tmp; // real x data
smalldelay(2); // delay wait y conversion
sendspi(0x00);
tmp = readspi(); // first 7 bit y data
positiony = tmp;
positiony <<= 5;
sendspi(0x00); // only for read last y data
tmp = readspi();
tmp >>= 3;
positiony += tmp; // real y data
endspi();
}
After simple debugging, the author wrote a PC software to display the characters sliding on the touch screen. About four lines of Chinese characters can be written on an 8×5cm touch screen, as shown in the figure below:
The scattered points in the figure are caused by the lack of anti-interference filtering in the hardware and the failure to read the contact points repeatedly in the MCU program. Generally, the interference can be eliminated by reading twice and using the repeated data as the correct data.
Keywords:Atmega16
Reference address:Connection between ATmega16 and touch screen
In the author's application, pin7 and pin8 are directly connected to GND, that is, the auxiliary input channel is not used, and pin9 and pin10 are connected together, that is, VCC is used as the analog-to-digital conversion reference voltage source of ADS7846. The pull-up resistor connected to pin11 can be omitted, but the corresponding AVR input port pull-up resistor must be set to enable. Pin13 is used as a conversion end indication. The level of this pin can be judged to determine whether the conversion data can be read, or a simple delay method can be used to leave enough conversion time. Pin16, pin15, pin14, and pin12 are connected to the mega16 chip as a standard SPI slave interface.
ADS7846 supports 8-bit accuracy and 12-bit accuracy, that is, the touch resolution can reach 1/256 or 1/4096. The corresponding touch accuracy is selected according to the LCD with different resolutions. For example, a 128×64 LCD can use 8-bit precision, and a 320×240 LCD needs 12-bit precision. The collected data is read out twice, with 8-bit precision first getting the first 7 bits and then the last bit, and with 12-bit precision first getting the first 7 bits and then the last 5 bits.
The program segment is as follows: (the compiler uses ICCAVR)
/****************************************************
SPI Interface file
crystal: 8MHz
write by han, hanembed@126.com, http://embed.hanyuer.net
*******************************************************/
#include
#include
/*=======================================================
// function: initialize spi interface
// in: void
// retun: void
// date: 2005/8/10
========================================================*/
void spiinit(void)
{
DDRB = (1 << PB4) | (1 << PB5) | (1 << PB7); // MOSI and SCK port out
PORTB |= (1 << PB4);
SPCR = (1 << SPE) | (1 << MSTR) | (0 << SPR0); // enable spi,master mode, MCLK/4,spi 0 mode
}
/*============================================ ==
// function: send data form spi interface
// in: unsigned char real data
// retun: void
// date: 2005/8/10
==================================================*/
void sendspi(unsigned char data)
{
SPDR = data; // send data
while( !(SPSR & (1 << SPIF)) ); // wait data transfer end
}
/*======================================================
// function: receive data form spi interface
// in: void
// retun: unsigned char
// date: 2005/8/10
========================================= ========*/
unsigned char readspi(void)
{
return SPDR;
}
/******************************************************
touch data read file
crystal: 8MHz
write by han, hanembed@126.com, http://embed.hanyuer.net
*****************************************************/
#include
#include
#include "..incspi.h"
unsigned int positionx;
unsigned int positiony;
unsigned char flgtouch; iv_INT1 /*
==============================================
// function: initialize all used port
// in: void
// retun: void
// date: 2005/8/10
======================= ================================*/
void portini(void)
{
spiinit();
endspi();
DDRD & = ~(1 << PD3); // port input
PORTD |= (1 << PD3); // pull-up resistance
//MCUCR |= 1<
flgtouch = 0;
}
/*==================================== ===============
// function: small delay
// in: unsigned char delay times
// retun: void
// date: 2005/8/10
================================ ===================*/
void smalldelay(unsigned char tmp)
{
unsigned char i;
while(tmp--)
{
for(i = 0; i < 250; i++)
{
NOP();
}
}
}
/*======================================== ==========
// function: read touch data
// in: void
// retun: void
// date: 2005/8/10
================================================== =*/
void keydown(void)
{
unsigned char tmp; // temporary data
smalldelay(20); // delay wait tranquilization
startspi(); // begin data transfer
smalldelay(1);
sendspi(0x90); // difference conversion , x data
smalldelay(2); // delay wait x conversion
sendspi(0x00);
tmp = readspi(); // first 7 bit x data
if(tmp == 0x7F) // error read
return;
positionx = tmp;
positionx <<= 5; // left shift 5 bit
sendspi(0xD0); // difference conversion, y data
tmp = readspi(); // last 5 bit x data
tmp >>= 3; // right shift 3 bit
positionx + = tmp; // real x data
smalldelay(2); // delay wait y conversion
sendspi(0x00);
tmp = readspi(); // first 7 bit y data
positiony = tmp;
positiony <<= 5;
sendspi(0x00); // only for read last y data
tmp = readspi();
tmp >>= 3;
positiony += tmp; // real y data
endspi();
}
After simple debugging, the author wrote a PC software to display the characters sliding on the touch screen. About four lines of Chinese characters can be written on an 8×5cm touch screen, as shown in the figure below:
The scattered points in the figure are caused by the lack of anti-interference filtering in the hardware and the failure to read the contact points repeatedly in the MCU program. Generally, the interference can be eliminated by reading twice and using the repeated data as the correct data.
Previous article:Design of solar panel light source control system based on Atmega16
Next article:Research on key technologies of stepper motor drivers
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
Guess you like
- Qualcomm wins injunction, Chinese court bans sale of several iPhones
- RF microwave - Qorvo has a good interpretation of this material [gallium arsenide and gallium nitride]
- Looking for a sound amplification circuit
- [Flower carving hands-on] Interesting and fun music visualization series of small projects (26) - LED hypercube
- The domestic Gaoyun FPGA GWIN-4B development board cannot use the MSPI pin. Please help
- Shanghai is a cold and indifferent society now,,,,,I don't want to say more
- sp485 communication reception is 0x00
- Xunwei 4412 development board Qt network programming-UDP implementation of server and client
- AD Wiring "hitting a wall"! ? ?
- How to solve the three major problems of wireless video surveillance?