我写了个触摸屏的程序,触摸芯片用的ADS7846.
不过经常死机。
源程序如下:
/*
Copyright (c) 2011,½ ËÕÄÏͨÈËÏÈÒ½ÁƿƼ¼ÓÐÏÞ¹«Ë¾Ñз¢²¿
All rights reserved.
ÎļþÃû³Æ£ºADS7846.C
µ±Ç°°æ±¾ 1.0
×÷Õߣº³Â³¤±ø
Íê³ÉÈÕÆÚ£º2011Äê10ÔÂ7ºÅ
*/
#include <STC12C5A60S2.H>
#include <absacc.h>
#include <intrins.h>
#include <ADS7846.H>
#define uint unsigned int
#define uchar unsigned char
extern uchar X,Y;
extern uchar X_Coordinate[4],Y_Coordinate[4];
/*************************************
**º¯ÊýÃû³Æ£ºdelay
**¹¦ÄÜÃèÊö£ºÑÓʱ
**ÊäÈ룺 i
**Êä³ö£ºÎÞ
**·µ»ØÖµ£ºÎÞ
**È«¾Ö±äÁ¿£ºÎÞ
**µ÷ÓÃÄ£¿é£ºÎÞ
**
**×÷Õߣº³Â³¤±ø
**ÈÕÆÚ£º2011-09-19
***************************************/
void delay(uchar i)
{
while(i--);
}
/*************************************
**º¯ÊýÃû³Æ£ºstart
**¹¦ÄÜÃèÊö£º´¥ÃþÆÁÆô¶¯ÐźÅ
**ÊäÈ룺 ÎÞ
**Êä³ö£ºÎÞ
**·µ»ØÖµ£ºÎÞ
**È«¾Ö±äÁ¿£ºÎÞ
**µ÷ÓÃÄ£¿é£ºÎÞ
**
**×÷Õߣº³Â³¤±ø
**ÈÕÆÚ£º2011-09-19
***************************************/
void start(void) //SPI¿ªÊ¼
{
DCLK=0;
CS=1;
DIN=1;
DCLK=1;
CS=0;
}
/*************************************
**º¯ÊýÃû³Æ£ºWriteCharTo7846
**¹¦ÄÜÃèÊö£ºÏò´¥ÃþÆÁдÊý¾Ý
**ÊäÈ룺 num
**Êä³ö£ºÎÞ
**·µ»ØÖµ£ºÎÞ
**È«¾Ö±äÁ¿£ºÎÞ
**µ÷ÓÃÄ£¿é£ºÎÞ
**
**×÷Õߣº³Â³¤±ø
**ÈÕÆÚ£º2011-09-19
***************************************/
void WriteCharTo7846(uchar num) //SPIдÊý¾Ý
{
uchar count=0;
DCLK=0;
for(count=0;count<8;count++)
{
if((num&0x80)==0x80)
DIN=1;
else
DIN=0;
//DIN=CY;/////////////////???
DCLK=0; _nop_();_nop_();_nop_(); //ÉÏÉýÑØÓÐЧ
DCLK=1; _nop_();_nop_();_nop_();
num<<=1;
}
}
/*************************************
**º¯ÊýÃû³Æ£ºReadFromCharFrom7846
**¹¦ÄÜÃèÊö£º´Ó´¥ÃþÆÁ¶ÁÊý¾Ý
**ÊäÈ룺 ÎÞ
**Êä³ö£ºÎÞ
**·µ»ØÖµ£º´Ó´¥ÃþÆÁ¶Áµ½µÄÖµ
**È«¾Ö±äÁ¿£ºÎÞ
**µ÷ÓÃÄ£¿é£ºÎÞ
**
**×÷Õߣº³Â³¤±ø
**ÈÕÆÚ£º2011-09-19
***************************************/
uchar ReadFromCharFrom7846() //SPI ¶ÁÊý¾Ý
{
uchar count=0;
uchar Num=0;
for(count=0;count<8;count++)
{
Num<<=1;
DCLK=1; _nop_();_nop_();_nop_(); //ϽµÑØÓÐЧ
DCLK=0; _nop_();_nop_();_nop_();
if(DOUT) Num++;
}
return(Num);
}
/*************************************
**º¯ÊýÃû³Æ£ºAD_XY
**¹¦ÄÜÃèÊö£º¶Á´¥ÃþÆÁ°´¼ü×ø±ê
**ÊäÈ룺 i
**Êä³ö£ºÎÞ
**·µ»ØÖµ£ºÎÞ
**È«¾Ö±äÁ¿£ºX_Coordinate[6],Y_Coordinate[6]
**µ÷ÓÃÄ£¿é£ºdelay,start,WriteCharTo7846,ReadFromCharFrom7846
**
**×÷Õߣº³Â³¤±ø
**ÈÕÆÚ£º2011-09-19
***************************************/
void AD_XY(uchar i)
{ //do
//{
delay(20000); //ÖжϺóÑÓʱÒÔÏû³ý¶¶¶¯£¬Ê¹µÃ²ÉÑùÊý¾Ý¸ü׼ȷ
start(); //Æô¶¯SPI
//while(BUSY); //Èç¹ûBUSYÐźŲ»ºÃʹ¿ÉÒÔɾ³ý²»ÓÃ
delay(2);
WriteCharTo7846(0x98); //ËÍ¿ØÖÆ×Ö 10010000 ¼´Óòî·Ö·½Ê½¶ÁX×ø±ê ÏêϸÇë¼ûÓйØ×ÊÁÏ
//while(BUSY); //Èç¹ûBUSYÐźŲ»ºÃʹ¿ÉÒÔɾ³ý²»ÓÃ
delay(2);
DCLK=1; _nop_();_nop_();_nop_();_nop_();
DCLK=0; _nop_();_nop_();_nop_();_nop_();
X_Coordinate[i]=ReadFromCharFrom7846(); //¶ÁXÖá×ø±ê
WriteCharTo7846(0xD8); //ËÍ¿ØÖÆ×Ö 11010000 ¼´Óòî·Ö·½Ê½¶ÁY×ø±ê ÏêϸÇë¼ûÓйØ×ÊÁÏ
delay(2);
DCLK=1; _nop_();_nop_();_nop_();_nop_();
DCLK=0; _nop_();_nop_();_nop_();_nop_();
Y_Coordinate[i]=ReadFromCharFrom7846(); //¶ÁYÖá×ø±ê
//X=(X>>4)&0XFF;
//Y=(Y>>4)&0XFF;
CS=1;
//}while((X<14)||(X>238)||(Y<26)||(Y>218));
}
请高手看看,哪儿有问题?谢谢! |