u8 Read_ADS2(u16 *x,u16 *y) { u16 x1,y1; u16 x2,y2; u8 flag; flag=Read_ADS(&x1,&y1); if(flag==0)return(0); flag=Read_ADS(&x2,&y2); if(flag==0)return(0); if(((x2<=x1&&x1<x2+ERR_RANGE)||(x1<=x2&&x2<x1+ERR_RANGE))//前后两次采样在+-ERR_RANGE内 &&((y2<=y1&&y1<y2+ERR_RANGE)||(y1<=y2&&y2<y1+ERR_RANGE))) { *x=(x1+x2)>>1; *y=(y1+y2)>>1; return 1; }else return 0;
uint8_t Read_ADS2(uint16_t *x, uint16_t *y) { uint16_t x1, y1; uint16_t x2, y2; uint8_t flag; flag = Read_ADS(&x1, &y1); if(flag == 0) { return(0); } flag = Read_ADS(&x2, &y2); if(flag == 0) { return(0); } if(((x2 <= x1 && x1 < x2 + ERR_RANGE) || (x1 <= x2 && x2 < x1 + ERR_RANGE)) //前后两次采样在+-50内 && ((y2 <= y1 && y1 < y2 + ERR_RANGE) || (y1 <= y2 && y2 < y1 + ERR_RANGE))) { *x = (x1 + x2) / 2; *y = (y1 + y2) / 2; return 1; } else { return 0; } }
//外部中断初始化函数 void Touch_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; //中断 GPIO_InitTypeDef GPIO_InitStructure; //GPIO EXTI_InitTypeDef EXTI_InitStructure; //外部中断线 //注意,时钟使能之后,对GPIO的操作才有效 //所以上拉之前,必须使能时钟.才能实现真正的上拉输出 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_0|GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU ; //上拉输入 GPIO_Init(GPIOC, &GPIO_InitStructure); Read_ADS(&Pen_Point.X,&Pen_Point.Y);//第一次读取初始化 NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; //使能按键所在的外部中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级0级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource1); EXTI_InitStructure.EXTI_Line = EXTI_Line1; //外部线路EXIT1 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设外外部中断模式:EXTI线路为中断请求 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //外部中断触发沿选择:设置输入线路下降沿为中断请求 EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能外部中断新状态 EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器 #ifdef ADJ_SAVE_ENABLE AT24CXX_Init();//初始化24CXX if(Get_Adjdata())return;//已经校准 else //未校准? { LCD_Clear(WHITE);//清屏 Touch_Adjust(); //屏幕校准 Save_Adjdata(); } Get_Adjdata(); #else LCD_Clear(WHITE);//清屏 Touch_Adjust(); //屏幕校准,带自动保存 #endif }
//外部中断初始化函数 void Touch_Init(void) { //注意,时钟使能之后,对GPIO的操作才有效 //所以上拉之前,必须使能时钟.才能实现真正的上拉输出 RCC->APB2ENR|=1<<4; //PC时钟使能 RCC->APB2ENR|=1<<0; //开启辅助时钟 GPIOC->CRL&=0XFFFF0000;//PC0~3 GPIOC->CRL|=0X00003883; GPIOC->CRH&=0XFF0FFFFF;//PC13 GPIOC->CRH|=0X00300000;//PC13推挽输出 GPIOC->ODR|=0X200f; //PC0~3 13 全部上拉 Read_ADS(&Pen_Point.X,&Pen_Point.Y);//第一次读取初始化 MY_NVIC_Init(2,0,EXTI1_IRQChannel,2); RCC->APB2ENR|=0x01; //使能io复用时钟 AFIO->EXTICR[0]|=0X0020; //EXTI13映射到PC1 EXTI->IMR|=1<<1; //开启line1上的中断 EXTI->EMR|=1<<1; //不屏蔽line1上的事件 EXTI->FTSR|=1<<1; //line1上事件下降沿触发 #ifdef ADJ_SAVE_ENABLE AT24CXX_Init();//初始化24CXX if(Get_Adjdata())return;//已经校准 else //未校准? { LCD_Clear(WHITE);//清屏 Touch_Adjust(); //屏幕校准 Save_Adjdata(); } Get_Adjdata(); #else LCD_Clear(WHITE);//清屏 Touch_Adjust(); //屏幕校准,带自动保存 #endif // printf("Pen_Point.xfac:%f\n",Pen_Point.xfac); // printf("Pen_Point.yfac:%f\n",Pen_Point.yfac); // printf("Pen_Point.xoff:%d\n",Pen_Point.xoff); // printf("Pen_Point.yoff:%d\n",Pen_Point.yoff); }