/******************************************************************************* * Function Name : Read_X * Description : 读取ADS7843通道X+的ADC值 * Input : None * Output : None * Return : ADS7843返回通道X+的ADC值 * Attention : None *******************************************************************************/ int Read_X(void) { int i; TP_CS(0); //CS作用有待考虑。。。。写指令读数据时候需将CS拉低? DelayUS(1); WR_CMD(CHX); /* 通道Y+的选择控制字 */ DelayUS(1); i=RD_AD(); TP_CS(1); return i; }
/******************************************************************************* * Function Name : Read_Y * Description : ¶ÁÈ¡ADS7843ͨµÀY+µÄADCÖµ * Input : None * Output : None * Return : ADS7843·µ»ØͨµÀY+µÄADCÖµ * 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 : Read_X * Description : Read ADS7843 ADC value of X + channel * Input : None * Output : None * Return : return X + channel ADC value * Attention : None *******************************************************************************/ static int Read_X(void) { TP_CS(0); DelayUS(1); WR_CMD(CHX); DelayUS(1); int i = RD_AD(); TP_CS(1); return i; }
/******************************************************************************* * Function Name : TP_Init * Description : ADS7843端口初始化 * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ void TP_Init(void) { LPC_GPIO0->FIODIR |= (1<<6); /* P0.6 CS is output */ LPC_GPIO2->FIODIR |= (0<<13); /* P2.13 TP_INT is input */ TP_CS(1); ADS7843_SPI_Init(); }
//==================================================================================== static void WR_CMD (unsigned char cmd) { unsigned char buf; unsigned char i; TP_CS(1); TP_DIN(0); TP_DCLK(0); TP_CS(0); for(i=0;i<8;i++) { buf=(cmd>>(7-i))&0x1; TP_DIN(buf); Delayus(5); TP_DCLK(1); Delayus(5); TP_DCLK(0); } }
/******************************************************************************* * Function Name : TP_Init * Description : ADS7843 Port initialization * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ void TSDriver_Initialize(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStruct; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_13 | GPIO_Pin_14; GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2 ); //sclk 10 13 GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2 ); //mýso 11 14 GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2 ); //mosý 12 15 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); SPI_I2S_DeInit(SPI2 ); 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_CPOL_Low SPI_CPOL_High SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //SPI_NSS_Hard //SPI_NSS_Soft SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //16 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; // 3 GPIO_Init(GPIOB, &GPIO_InitStruct); // d TP_CS(1); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; //Pin 6 is IRQ GPIO_Init(GPIOD, &GPIO_InitStruct); touch_interrupt_init(); }
/******************************************************************************* * Function Name : TP_Init * Description : ADS7843¶Ë¿Ú³õʼ»¯ * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ void TP_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; /* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/ RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_25MHz; GPIO_InitStruct.GPIO_OType=GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Pin=GPIO_Pin_15|GPIO_Pin_13|GPIO_Pin_14; GPIO_Init(GPIOB,&GPIO_InitStruct); GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); //sclk 10 13 GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); //mýso 11 14 GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2); //mosý 12 15 /* TP_CS */ GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType=GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12; // 3 GPIO_Init(GPIOB,&GPIO_InitStruct); // d /* TP_IRQ */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN; GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType=GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6; GPIO_Init(GPIOD,&GPIO_InitStruct); /**/ SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD, EXTI_PinSource6); EXTI_InitStructure.EXTI_Line = EXTI_Line6; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /**/ TP_CS(1); ADS7843_SPI_Init(); }
/******************************************************************************* * Function Name : TP_Init * Description : * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ void TP_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(Open_SPI_SCK_GPIO_CLK | Open_SPI_MISO_GPIO_CLK | Open_SPI_MOSI_GPIO_CLK,ENABLE); RCC_AHB1PeriphClockCmd(Open_TP_CS_CLK | Open_TP_IRQ_CLK,ENABLE); Open_SPI_CLK_INIT(Open_RCC_SPI,ENABLE); GPIO_PinAFConfig(Open_SPI_SCK_GPIO_PORT, Open_SPI_SCK_SOURCE, Open_GPIO_AF_SPI); GPIO_PinAFConfig(Open_SPI_MISO_GPIO_PORT, Open_SPI_MISO_SOURCE, Open_GPIO_AF_SPI); GPIO_PinAFConfig(Open_SPI_MOSI_GPIO_PORT, Open_SPI_MOSI_SOURCE, Open_GPIO_AF_SPI); GPIO_InitStructure.GPIO_Pin = Open_SPI_SCK_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(Open_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = Open_SPI_MISO_PIN; GPIO_Init(Open_SPI_MISO_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = Open_SPI_MOSI_PIN; GPIO_Init(Open_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); /* TP_CS */ GPIO_InitStructure.GPIO_Pin = Open_TP_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(Open_TP_CS_PORT, &GPIO_InitStructure); /*TP_IRQ */ GPIO_InitStructure.GPIO_Pin = Open_TP_IRQ_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN ; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(Open_TP_IRQ_PORT, &GPIO_InitStructure); TP_CS(1); ADS7843_SPI_Init(); }
//读取触摸屏Y返回值 u16 TouchReadY(void) { u16 y=0; TP_CS(); SpiDelay(10); SPI_WriteByte(0xD0); SpiDelay(10); y=SPI_WriteByte(0x00); y<<=8; y+=SPI_WriteByte(0x00); SpiDelay(10); TP_DCS(); y = y>>3; return (y); }
//读取触摸屏X返回值 u16 TouchReadX(void) { u16 x=0; TP_CS(); SpiDelay(10); SPI_WriteByte(0x90); SpiDelay(10); x=SPI_WriteByte(0x00); x<<=8; x+=SPI_WriteByte(0x00); SpiDelay(10); TP_DCS(); x = x>>3; return (x); }
u16 TPReadY(SPI_TypeDef* SPIx) { u16 y=0; TP_CS(); SpiDelay(10); SPI_ReadWrite(SPIx, 0xD0); // SPI_ReadWrite(0x90); SpiDelay(10); y=SPI_ReadWrite(SPIx, 0x00); y<<=8; y+=SPI_ReadWrite(SPIx, 0x00); SpiDelay(10); TP_DCS(); y = y>>3; return (y); }
u16 TPReadX(SPI_TypeDef* SPIx) { u16 x=0; TP_CS(); SpiDelay(10); SPI_ReadWrite(SPIx, 0x90); // SPI_ReadWrite(0xd0); SpiDelay(10); x=SPI_ReadWrite(SPIx, 0x00); x<<=8; x+=SPI_ReadWrite(SPIx, 0x00); SpiDelay(10); TP_DCS(); x = x>>3; return (x); }
//==================================================================================== void TSC_Init(void) { /* Enable SPI1 and GPIO clocks */ RCC->AHB1ENR |=((1UL << 0) | /* Enable GPIOA clock */ (1UL << 1) | /* Enable GPIOB clock */ (1UL << 2)); /* Enable GPIOC clock */ //PA.15 as CS OUTPUT GPIOA->MODER &= ~0xC0000000; /* Clear Bits */ GPIOA->MODER |= 0x40000000; /* Alternate Function mode */ GPIOA->OSPEEDR &= ~0xC0000000; /* Clear Bits */ GPIOA->OSPEEDR |= 0x80000000; /* 50 MHz Fast speed */ GPIOA->AFR[0] &= ~0x00000000; /* Clear Bits */ GPIOA->AFR[0] |= 0x00000000; /* Alternate Function mode AF12 */ GPIOA->AFR[1] &= ~0xF0000000; /* Clear Bits */ GPIOA->AFR[1] |= 0x00000000; /* Alternate Function mode AF12 */ //PB.15 as IRQ[I], PB.04 as SCK[O], PB.05 as MISO[I], PB.01 as MOSI[O] GPIOB->MODER &= ~0xC0000F0C; /* Clear Bits */ GPIOB->MODER |= 0x00000104; /* Alternate Function mode */ GPIOB->OSPEEDR &= ~0xC0000F0C; /* Clear Bits */ GPIOB->OSPEEDR |= 0x80000A08; /* 50 MHz Fast speed */ GPIOB->AFR[0] &= ~0x00FF00F0; /* Clear Bits */ GPIOB->AFR[0] |= 0x00000000; /* Alternate Function mode AF12 */ GPIOB->AFR[1] &= ~0xF0000000; /* Clear Bits */ GPIOB->AFR[1] |= 0x00000000; /* Alternate Function mode AF12 */ //PC.2 as BUSY[I] GPIOC->MODER &= ~0x00000030; /* Clear Bits */ GPIOC->MODER |= 0x00000000; /* Alternate Function mode */ GPIOC->OSPEEDR &= ~0x00000030; /* Clear Bits */ GPIOC->OSPEEDR |= 0x00000020; /* 50 MHz Fast speed */ GPIOC->AFR[0] &= ~0x00000F00; /* Clear Bits */ GPIOC->AFR[0] |= 0x00000000; /* Alternate Function mode AF12 */ GPIOC->AFR[1] &= ~0x00000000; /* Clear Bits */ GPIOC->AFR[1] |= 0x00000000; /* Alternate Function mode AF12 */ TP_CS(1); TP_DCLK(0); TP_DIN(0); }
//==================================================================================== static unsigned short RD_AD(void) { unsigned short buf=0,temp; unsigned char i; TP_DIN(0); TP_DCLK(1); for(i=0;i<12;i++) { Delayus(5); TP_DCLK(0); Delayus(5); temp= (TP_DOUT) ? 1:0; buf|=(temp<<(11-i)); Delayus(5); TP_DCLK(1); } TP_CS(1); buf&=0x0fff; return(buf); }
/** * @brief Configure GPIO * @param None * @retval None */ static void TSC_GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); /* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStruct); #if 0 /* TP_CS pc6*/ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStruct); /* TP_IRQ Pc4*/ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStruct); #endif /* TP_CS */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStruct); /* TP_IRQ */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 ; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU ; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStruct); TP_CS(1); }
/******************************************************************************* * Function Name : TP_Init * Description : ADS7843端口初始化,触摸屏通信 * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ void TP_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE); /* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /* TP_CS */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /* TP_IRQ */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); TP_CS(1); //x ? GPIO_SetBits(GPIOB,GPIO_Pin_5): GPIO_ResetBits(GPIOB,GPIO_Pin_5)片选端的初始化 ADS7843_SPI_Init(); //TP_CS(0); }