Connection between ATmega16 and touch screen

Publisher:火箭飞人Latest update time:2014-09-23 Source: eefocusKeywords:Atmega16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
       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

Previous article:Design of solar panel light source control system based on Atmega16
Next article:Research on key technologies of stepper motor drivers

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号