觸控屏的控制電路制作小記
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;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;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; //使用外部中斷1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//階級(jí)1
NVIC_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; //外部中斷通道1
EXTI_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
DIY機(jī)械鍵盤相關(guān)社區(qū):機(jī)械鍵盤DIY
評(píng)論