📄 ads7846.c
字号:
#include <string.h>
#include "ecrsys.h"
#include "ads7846.h"
#include "ftype.h"
#include "data.h"
#include "font.h"
#include "tft_disp.h"
void Insert_Byte(char,char);
static GUI_HID_STATE TS_State;
static TS_LOCT_DEF Ts_MinMax;
#if (MODEL_VER == ROYAL_9INCH)
const char Ts_Key_Cvt_Tbl[100] =
{
1,11,21,31,41,51,61,71,81,91,
2,12,22,32,42,52,62,72,82,92,
3,13,23,33,43,53,63,73,83,93,
4,14,24,34,44,54,64,74,84,94,
5,15,25,35,45,55,65,75,85,95,
6,16,26,36,46,56,66,76,86,96,
7,17,27,37,47,57,67,77,87,97,
8,18,28,38,48,58,68,78,88,98,
9,19,29,39,49,59,69,79,89,99,
10,20,30,40,50,60,70,80,90,100
};
#else
const char Ts_Key_Cvt_Tbl[130] =
{
1,14,27,40,53,66,79,92, 105,118,
2,15,28,41,54,67,80,93, 106,119,
3,16,29,42,55,68,81,94, 107,120,
4,17,30,43,56,69,82,95, 108,121,
5,18,31,44,57,70,83,96, 109,122,
6,19,32,45,58,71,84,97, 110,123,
7,20,33,46,59,72,85,98, 111,124,
8,21,34,47,60,73,86,99, 112,125,
9,22,35,48,61,74,87,100,113,126,
10,23,36,49,62,75,88,101,114,127,
11,24,37,50,63,76,89,102,115,128,
12,25,38,51,64,77,90,103,116,129,
13,26,39,52,65,78,91,104,117,130
};
#endif
/*******************************************************************************
* Whether the interrupt is generated
* Old and new method can be selected
*******************************************************************************/
#if 0
// #define GetDInt() ADS7846_INT_PIN
#define GetDInt() ADS7846_INT_PIN
#else
INT8U GetDInt(void)
{
return(0);
}
// {
// if (!ADS7846_INT_PIN)
// return(0);
// else
// return(1);
// }
#endif
/*SIORDY*/
/*Check whether the ADS7846 is busy*/
INT8U GetBusy(void)
{
if (!ADS7846_BUSY)
return(0);
else
return(1);
}
/*Send command to ADS7846*/
void Ads7846_SendCommand(INT8U ucCommand)
{
INT8U i,data;
data=ucCommand;
for (i = 0; i < 8; i++)
{
//send command from the DIN port
if(data&0x80)
{
ADS7846_DIN = 1;
}
else
{
ADS7846_DIN = 0;
}
data<<=1;
//CLK HIGH
SetClkHigh();
Ads7846_Delay();
//CLK LOW
SetClkLow();
SetDInLow();
Ads7846_Delay();
}
}
/*Get data from ADS7846 according to the command*/
int Ads7846_Get(INT8U ucCommand)
{
INT8U i;
int ulData = 0;
if(GetDInt()) /* If not genertated the interrupt, return directly*/
{
return -1;
}
else
{
}
// SetDIntLow();
SetClkLow();
Ads7846_Delay();
SetCSLow();
Ads7846_Delay();
Ads7846_SendCommand(ucCommand);
SetDInLow();
SetClkHigh();
Ads7846_Delay();
SetClkLow();
Ads7846_Delay();
while (GetBusy() == 1);
for (i = 0; i < 12; i++)
{
SetDInLow();
ulData <<= 1;
// ulData |= GetDout();
if (ADS7846_DOUT)
ulData ++;
// ulData |= ADS7846_DOUT;
SetClkHigh();
Ads7846_Delay();
SetClkLow();
Ads7846_Delay();
}
for (i = 0; i < 3; i++)
{
SetDInLow();
SetClkHigh();
Ads7846_Delay();
SetClkLow();
Ads7846_Delay();
}
SetCSHigh();
// if(GetDInt())
// {
// return -1;
// }
// else
{
return ulData;
}
}
/*********************************************************************************
* Initialize the ADS7846 chip and MCU's port
*********************************************************************************/
void Tb2_Init(void);
void Ads7846_Init(void)
{
GUI_TOUCH_SetDefaultCalibration();
pd11 = 0x07; /*All are output except the DOUT and BUSY pins*/
SetClkLow();
Ads7846_Delay();
SetCSLow();
Ads7846_Delay();
Ads7846_SendCommand(COMMAND_INIT);
Ads7846_SendCommand(COMMAND_INIT);
Ads7846_Delay();
SetCSHigh();
TS_State.x = 0;
TS_State.y = 0;
TS_State.Pressed = 0;
TS_State.Key_Off = 0;
}
#define XY_DETECT_CNT 10
#define XY_INGRO_CNT 4
void Ads7846_Delay()
{
volatile INT8U i;
// for (i = 0; i < 5; i++);
for (i = 0; i < 2; i++);
}
/*Receive the X dirction data*/
int DetectX(void)
{
#define DETECT_CNT XY_DETECT_CNT
#define INGRO_CNT XY_INGRO_CNT
U8 i,j;
U32 temp;
int ClickData[DETECT_CNT],ClickDataCnt=0;
for(i=0;i<DETECT_CNT;i++)
{
ClickData[i]=Ads7846_Get(COMMAND_GET_X);
if(ClickData[i]==-1)
return -1;
}
for(j=1;j<DETECT_CNT;j++) /*Sort the received data*/
{
for(i=0;i<DETECT_CNT-j;i++)
{
if(ClickData[i]<ClickData[i+1])
{
temp=ClickData[i];
ClickData[i]=ClickData[i+1];
ClickData[i+1]=temp;
}
}
}
for(i=0;i<(DETECT_CNT-INGRO_CNT);i++)
ClickDataCnt+=ClickData[INGRO_CNT/2-1+i];
if(GetDInt()) /*not during the interrupt status*/
return -1;
else
{
temp = (ClickDataCnt/(DETECT_CNT-INGRO_CNT));
return temp;
}
}
/*Receive the Y dirction data*/
#define DETECT_CNT1 XY_DETECT_CNT
#define INGRO_CNT1 XY_INGRO_CNT
int DetectY(void)
{
#define DETECT_CNT1 XY_DETECT_CNT
#define INGRO_CNT1 XY_INGRO_CNT
U8 i,j;
U32 temp;
int ClickData[DETECT_CNT1],ClickDataCnt=0;
for(i=0;i<DETECT_CNT1;i++)
{
ClickData[i]=Ads7846_Get(COMMAND_GET_Y);
if(ClickData[i]==-1)
{
return -1;
}
}
for(j=1;j<DETECT_CNT1;j++)
{
for(i=0;i<DETECT_CNT1-j;i++)
{
if(ClickData[i]<ClickData[i+1])
{
temp=ClickData[i];
ClickData[i]=ClickData[i+1];
ClickData[i+1]=temp;
}
}
}
for(i=0;i<(DETECT_CNT1-INGRO_CNT1);i++)
ClickDataCnt+=ClickData[INGRO_CNT1/2-1+i];
if(GetDInt())
{
// bellcnt = 0x10;
// return -1;
}
// else
{
temp = (ClickDataCnt/(DETECT_CNT1-INGRO_CNT1));
return temp;
}
}
/*Receive the data both X and Y direction*/
int DetectXY(void)
{
return (DetectY()<<12)|DetectX();
}
/*********************************************************************
*
* GUI_TOUCH_GetState
*
**********************************************************************
*/
int GUI_TOUCH_GetState(GUI_HID_STATE *pState)
{
*pState = TS_State;
return (TS_State.Pressed != 0) ? 1 : 0;
}
/*Store the Touch Screen status*/
void GUI_TOUCH_StoreState(int x, int y)
{
static int xPrev=-1, yPrev=-1;
#if 0
TS_State.x = xPrev; /*Previous status is stored*/
TS_State.y = yPrev;
TS_State.Pressed = ((x==-1) | (xPrev==-1)) ? 0 : 1;
if ((xPrev != x) || (yPrev != y)) /*两次不同的坐标,则保存*/
{
}
xPrev = x;
yPrev = y;
// bellcnt = 0x30;
#else
if ( (x != -1) && (y !=-1))
{
TS_State.x = x; /*Previous status is stored*/
TS_State.y = y;
TS_State.Pressed = 1;
}
// bellcnt = 0x30;
#endif
}
/*********************************************************************
* Initlialize the parameter to default
*********************************************************************/
void GUI_X_Init(void)
{
GUI_TOUCH_SetDefaultCalibration();
}
/*X 方向测量*/
int TOUCH_X_MeasureX(void)
{
int x;
x=DetectX();
return x;
}
/*Y 方向测量*/
int TOUCH_X_MeasureY(void)
{
int y;
y=DetectY();
return y;
}
#pragma INTERRUPT/E Int1_Isr
void Int1_Isr(void)
{
tb2s = 1;
int1ic = 0x00;
}
/*
********************************************************************************
* Initialize the Timer B2 as interval timer
* Inteval time is 5 ms
********************************************************************************
*/
void Tb2_Init(void)
{
disint();
tb2mr = 0x40; // f8, disable gate, timer mode
tb2 = 5000 * FOSC/8; // set timer A2 initial value
tb2ic = 0x01; // 最低级别的中断
tb2s = 0;
ir_tb2ic = 0;
enint();
}
void Ts_KeyOffChk(void)
{
static WORD cnt = 0;
static WORD cnt1 = 0;
if (ADS7846_INT_PIN)
{
cnt1 = 0;
cnt ++;
if ( cnt >= 200) /*500 mSecond*/
{
TS_State.Key_Off = 1;
cnt = 0;
}
}
// else
// {
// cnt = 0;
// cnt1 ++;
// if (cnt1 >= 100)
// {
// TS_State.Key_Off = 0;
// cnt1 = 0;
// }
// }
}
#pragma INTERRUPT/E Tb2_Isr
/*
********************************************************************************
* Timer B2 interrrupt routine
********************************************************************************
*/
unsigned char key_bak = 0;//,key_bak1=0;
void Tb2_Isr(void)
{
unsigned char key;
tb2s = 0; /*Stop the timer b2*/
enint(); /*Enable other interrupt */
if (!ADS7846_INT_PIN)
{
if ( TS_State.Key_Off) /*Keyboard raise up?*/
{
GUI_TOUCH_Exec();
if (TS_State.Pressed)
{
TS_State.Pressed = 0;
key = Ts_Loc_Get(TS_State); /*Get the key input index no*/
if (key == key_bak) /*2 times is same, store it */
{
key_rngset(((word)key)<<8);
TS_State.Key_Off = 0;
}
else
{
key_bak = key; /*Use the new key*/
}
}
}
}
disint();
int1ic = 0x23; /*Enable the external pen interrupt*/
enint();
}
/*
********************************************************************************
* Initialize the external Interrupt 2 as low level interrupt
* ADS7846的外部硬件中断为低电平触发
********************************************************************************
*/
void Init_ExtInt1(void)
{
pd8_3 = 0; /*used as inupt mode*/
int1ic = 0x23; /*level 3 and low LEVEL interrupt*/
// ifsr &= 0xcb;
ifsr = 0;
}
/*
********************************************************************************
* Initialize the Touch Panel working environment
********************************************************************************
*/
void Init_Ts(void)
{
Ads7846_Init();
Tb2_Init(); /*Initialize the Timer B2 as interval for delay*/
Init_ExtInt1();
}
char Uart_WriteByte(char,char);
void Wr_Word_Uart(int data)
{
Uart_WriteByte(PORT1,(char)(data>>8));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -