📄 touchpanel.c
字号:
/*********************************************************************************************************
*
* File : TouchPanel.c
* Hardware Environment:
* Build Environment : RealView MDK-ARM Version: 4.20
* Version : V1.0
* By :
*
* (c) Copyright 2005-2011, WaveShare
* http://www.waveshare.net
* All Rights Reserved
*
*********************************************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "TouchPanel.h"
#include "SysTick/systick.h"
#include "LCD/LCD.h"
/* Private variables ---------------------------------------------------------*/
Matrix matrix ;Coordinate display ;
Coordinate ScreenSample[3];Coordinate DisplaySample[3] = { { 30, 45 }, { 290, 45}, { 160,210} };
/* Private define ------------------------------------------------------------*/
#define THRESHOLD 2
/*******************************************************************************
* Function Name : ADS7843_SPI_Init
* Description :
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void ADS7843_SPI_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
/* DISABLE SPI3 */
SPI_Cmd(SPI3, DISABLE);
/* SPI3 Config -------------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI3, &SPI_InitStructure);
/* Enable SPI3 */
SPI_Cmd(SPI3, ENABLE);
}
/*******************************************************************************
* Function Name : TP_Init
* Description :
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void TP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);
/* Configure SPI3 pins: SCK, MISO and MOSI ---------------------------------*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);
/* TP_CS */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStruct);
/* TP_IRQ */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStruct);
TP_CS(1);
ADS7843_SPI_Init();
}
/*******************************************************************************
* Function Name : DelayUS
* Description :
* Input : - cnt:
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void DelayUS(vu32 cnt)
{
uint16_t i;
for(i = 0;i<cnt;i++)
{
uint8_t us = 12;
while (us--)
{
;
}
}
}
/*******************************************************************************
* Function Name : WR_CMD
* Description :
* Input : - cmd:
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void WR_CMD (uint8_t cmd)
{
/* Wait for SPI3 Tx buffer empty */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI3 data */
SPI_I2S_SendData(SPI3,cmd);
/* Wait for SPI3 data reception */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
/* Read SPI3 received data */
SPI_I2S_ReceiveData(SPI3);
}
/*******************************************************************************
* Function Name : RD_AD
* Description :
* Input : None
* Output : None
* Return :
* Attention : None
*******************************************************************************/
static int RD_AD(void)
{
unsigned short buf,temp;
/* Wait for SPI3 Tx buffer empty */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI3 data */
SPI_I2S_SendData(SPI3,0x0000);
/* Wait for SPI3 data reception */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
/* Read SPI3 received data */
temp=SPI_I2S_ReceiveData(SPI3);
buf=temp<<8;
DelayUS(1);
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI3 data */
SPI_I2S_SendData(SPI3,0x0000);
/* Wait for SPI3 data reception */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
/* Read SPI3 received data */
temp=SPI_I2S_ReceiveData(SPI3);
buf |= temp;
buf>>=3;
buf&=0xfff;
return buf;
}
/*******************************************************************************
* Function Name : Read_X
* Description : Read ADS7843 ADC X
* Input : None
* Output : None
* Return :
* Attention : None
*******************************************************************************/
int Read_X(void)
{
int i;
TP_CS(0);
DelayUS(1);
WR_CMD(CHX);
DelayUS(1);
i=RD_AD();
TP_CS(1);
return i;
}
/*******************************************************************************
* Function Name : Read_Y
* Description : Read ADS7843 ADC Y
* Input : None
* Output : None
* Return :
* Attention : None
*******************************************************************************/
int Read_Y(void)
{
int i;
TP_CS(0);
DelayUS(1);
WR_CMD(CHY);
DelayUS(1);
i=RD_AD();
TP_CS(1);
return i;
}
/*******************************************************************************
* Function Name : TP_GetAdXY
* Description : Read ADS7843
* Input : None
* Output : None
* Return :
* Attention : None
*******************************************************************************/
void TP_GetAdXY(int *x,int *y)
{
int adx,ady;
adx=Read_X();
DelayUS(1);
ady=Read_Y();
*x=adx;
*y=ady;
}
/*******************************************************************************
* Function Name : TP_DrawPoint
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -