新聞中心

ATmega16與觸摸屏的連接

作者: 時(shí)間:2017-06-04 來(lái)源:網(wǎng)絡(luò) 收藏
是四線電阻式的,驅(qū)動(dòng)芯片采用了很常見(jiàn)的ADS7846。ADS7846的典型應(yīng)用電路圖如下圖所示。


在筆者的應(yīng)用中,pin7和pin8都直接連接到GND,即不使用輔助輸入通道,pin9和pin10連接在一起,即使用了VCC做為ADS7846的模數(shù)轉(zhuǎn)換參考電壓源。pin11所接的上拉電阻可以不要,但要設(shè)置相應(yīng)的AVR輸入端口上拉電阻使能。pin13做為轉(zhuǎn)換結(jié)束指示,可以通過(guò)判斷此腳電平來(lái)決定是否可以讀出轉(zhuǎn)換數(shù)據(jù),也可以簡(jiǎn)單的使用延時(shí)的方法來(lái)留夠轉(zhuǎn)換時(shí)間。pin16、pin15、pin14、pin12做為一個(gè)標(biāo)準(zhǔn)的SPI從機(jī)接口與mega16芯片相連接。

ADS7846支持8位精度和12位精度,即觸摸分辨率可以達(dá)到1/256或者1/4096,根據(jù)不同分辨率的LCD來(lái)選擇相應(yīng)的觸摸精度。比如128×64的LCD可以采用8位精度,320×240的LCD需要采用12位精度。采集后的數(shù)據(jù)分兩次讀出,8位精度的先得到前7位再得到最后一位,12位精度的先得到前7位再得到后5位。

程序段如下:(編譯器使用ICCAVR)



/********************************************************************
SPI Interface file
crystal: 8MHz
write by han, hanembed@126.com, http://embed.hanyuer.net
********************************************************************/

#include iom16v.h>
#include macros.h>

/*===================================================================
// 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 iom16v.h>
#include macros.h>
#include ..incspi.h

unsigned int positionx;
unsigned int positiony;
unsigned char flgtouch;

/*========================Extern Interrupt==========================*/
#pragma interrupt_handler keydown: 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 |= 1ISC11; // down edge enable
GICR |= 1INT1; // 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();
}

經(jīng)過(guò)簡(jiǎn)單調(diào)試,筆者編寫(xiě)了一個(gè)PC端軟件以顯示在上滑過(guò)的字符,一塊8×5cm的上約可以寫(xiě)四行漢字,如下圖所示:



圖中的若干零散點(diǎn)是由于硬件并沒(méi)有做抗干擾濾波,mcu程序中也沒(méi)有對(duì)接觸點(diǎn)進(jìn)行重復(fù)讀取所致,一般可使用讀取兩次,重復(fù)數(shù)據(jù)為正確數(shù)據(jù)的方法來(lái)排除干擾。



關(guān)鍵詞: Atmega16 觸摸屏

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉