觸控屏的控制電路制作小記(附STM32的驅(qū)動(dòng))
程序:
//程序來源于 www.51hei.com#include"hx8347.h" //自己編寫的液晶屏頭文件,此頭文件只定義了一些基本變量,
不涉及驅(qū)動(dòng)相關(guān)函數(shù)//定義引腳高低電平#define ADS_DCLK_H() GPIO_SetBits(GPIOB,GPIO_Pin_6)//ADS7846時(shí)鐘信號(hào)#define ADS_DCLK_L() GPIO_ResetBits(GPIOB,GPIO_Pin_6)#define ADS_CS_H() GPIO_SetBits(GPIOB,GPIO_Pin_7)//ADS7846片選信號(hào)#define ADS_CS_L() GPIO_ResetBits(GPIOB,GPIO_Pin_7)#define ADS_DIN_H() GPIO_SetBits(GPIOB,GPIO_Pin_8)#define ADS_DIN_L() GPIO_ResetBits(GPIOB,GPIO_Pin_8)#define ADS_DOUT GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)#define ADS_PEN GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10)////ADS7846響應(yīng)信號(hào) //初始化I/O口void ADS_GPIO_Config(){GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, GPIO_InitStructure);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOB, GPIO_InitStructure);}void ADS_Spi_Start()//初始信號(hào){ADS_CS_H();ADS_DCLK_H();ADS_DIN_H();}void ADS_Write_Byte(u8 num){u8 count=0;ADS_DCLK_L();for(count=0;count8;count++){if(num0x80)ADS_DIN_H();else ADS_DIN_L();num=1;ADS_DCLK_L();ADS_DCLK_H(); //上升沿有效 }}u16 ADS_Readdata(){u16 num;u8 count;for(count=0;count12;count++){num=1;ADS_DCLK_H();ADS_DCLK_L();if(ADS_DOUT)num++;}return num;}#define CMD_RDX 0X90 //0B10010000即用差分方式讀X坐標(biāo)#define CMD_RDY 0XD0 //0B11010000即用差分方式讀Y坐標(biāo) u16 X=0,Y=0;//當(dāng)前觸控坐標(biāo)u8 Readonce(){ADS_Spi_Start();ADS_CS_L();ADS_Write_Byte(CMD_RDX);ADS_DCLK_H();delay_us(3);ADS_DCLK_L();delay_us(3);Y=ADS_Readdata();ADS_Write_Byte(CMD_RDY);ADS_DCLK_H();delay_us(1);ADS_DCLK_L();delay_us(1);X=ADS_Readdata();ADS_CS_H();if(X>100Y>100X3800Y3800)return 1;//讀取成功(范圍限制)else return 0;//讀取失敗}void drawbigpoint(u8 x,u16 y,u16 col){if(x>220y9){ LCD_DrawBlock(0,0,239,319,0x0000);//清屏 LCD_write_english_string(210,0,"CLR",0xFFE0,0x001F);//清屏按鍵區(qū)域}else {LCD_Set_Point(x,y,col);//中心點(diǎn) LCD_Set_Point((x+1),y,col);LCD_Set_Point(x,(y+1),col);LCD_Set_Point((x+1),(y+1),col); } } //讀取ADS7846(畫線)void Read_Ads7846(void){u8 t,t1,count=0;u16 databuffer[2][10]={{5,7,9,3,2,6,4,0,3,1},{5,7,9,3,2,6,4,0,3,1}};
//數(shù)據(jù)組u16 temp=0; //循環(huán)讀數(shù)10次do{t=ADS_PEN; //觸摸屏被按下,PEN為L(zhǎng)if(Readonce()) //讀數(shù)成功{ databuffer[0][count]=X;databuffer[1][count]=Y;count++; }}while(!tcount10); if(count==10)//讀10次數(shù)據(jù)有效{ //X升序排列do{ t1=0; for(t=0;tcount-1;t++){if(databuffer[0][t]>databuffer[0][t+1])//升序排列{temp=databuffer[0][t+1];databuffer[0][t+1]=databuffer[0][t];databuffer[0][t]=temp;t1=1; }}}while(t1); do//Y升序排列{ t1=0; for(t=0;tcount-1;t++){if(databuffer[1][t]>databuffer[1][t+1])//升序排列{temp=databuffer[1][t+1];databuffer[1][t+1]=databuffer[1][t];databuffer[1][t]=temp;t1=1; } }}while(t1); X=(databuffer[0][3]+databuffer[0][4]+databuffer[0][5])/3;Y=(databuffer[1][3]+databuffer[1][4]+databuffer[1][5])/3;//根據(jù)觸摸屏的具體參數(shù)設(shè)置if(X=4000Y=4000){if(X>=240)X-=240;else X=0;if(Y>=320)Y-=320;else Y=0; drawbigpoint(X/15,Y/11+10,BLUE); } }} void EXTI1_IRQHandler(void){ u8 t=0; //消除抖動(dòng) do{delay_us(10);t=ADS_PEN;Read_Ads7846(); }while(t==0); EXTI_ClearITPendingBit(EXTI_Line1);} //中斷優(yōu)先級(jí)管理/開啟 void NVIC_Configuration(void){NVIC_InitTypeDef NVIC_InitStructure;//存儲(chǔ)器映射 #ifdef VECT_TAB_RAM NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//優(yōu)先級(jí)分到第0組 總共5組 NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQChannel; //使用外部中斷1NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//階級(jí)1NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(NVIC_InitStructure); }//外部中斷初始化void EXTI_Configuration(void)//配置外部中斷{EXTI_InitTypeDef EXTI_InitStructure; //聲明中斷庫(kù)函數(shù)結(jié)構(gòu)體EXTI_InitStructure.EXTI_Line = EXTI_Line1; //外部中斷通道1EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中斷模式EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿觸發(fā)EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能EXTI_Init(EXTI_InitStructure);GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource10);
//設(shè)置外部中斷通道1到PB10}//END
評(píng)論