<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> STM32 >> 瀏覽文章

        stm32 四線IO口驅動觸摸屏

        作者:未知   來源:網上一位高手   點擊數:  更新時間:2014年06月01日   【字體:

        #include

        #include
        #define TOUCH_DOWNDELAYCOUNT 4
        #define TOUCH_UPDELAYCOUNT 10
        uint8 Touch_Update;
        uint8 TOUCH_Tos_TaskID;
        _Touch_Event Touch_Event;
         
        const uint16 TOUCH_CROSS_ADDR[5][2]={
        {TOUCH_CROSS_GAUGE,TOUCH_CROSS_GAUGE},//左上角校準點位置
        {LCD_XSIZE-TOUCH_CROSS_GAUGE,TOUCH_CROSS_GAUGE},//右上角校準點位置
        {LCD_XSIZE-TOUCH_CROSS_GAUGE,LCD_YSIZE-TOUCH_CROSS_GAUGE},//右下角校準點位置
        {TOUCH_CROSS_GAUGE,LCD_YSIZE-TOUCH_CROSS_GAUGE},//左下角校準點位置
        {LCD_XSIZE/2,LCD_YSIZE/2}//中間校準點位置
        };
         
        volatile uint16 TOUCH_ADC_Value[TOUCH_ADC_BUFSize][TOUCH_ADC_CHN]; //觸摸屏ADC采集池
        uint16 TOUCH_ADC_XValue,TOUCH_ADC_YValue;//觸摸屏ADC模數值
         
        void Touch_ADCY_Config()
        {
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//強拉接地
        GPIO_Init(GPIOC, &GPIO_InitStructure);
        PCout(5)=0;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;//強拉接VCC
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        PBout(1)=1;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPD;//上拉輸入
        GPIO_Init(GPIOB, &GPIO_InitStructure);
         
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模擬輸入
        GPIO_Init(GPIOC, &GPIO_InitStructure);
         
        }
        void Touch_ADCX_Config()
        {
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_Init(GPIOC, &GPIO_InitStructure);
        PCout(4)=0;
         
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        PBout(0)=1;
         
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//上拉輸入
        GPIO_Init(GPIOB, &GPIO_InitStructure);
         
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模擬輸入
        GPIO_Init(GPIOC, &GPIO_InitStructure);
         
        }
         
        void Touch_UpIngEvent()
        {
        if(Touch_Event.UpIng_Event)
        {
        DeBug("TOUCH_EVENT_UPING");
        Touch_Event.UpIng_Event=Null;
        }
        }
         
        void Touch_DownIngEvent()
        {
        if(Touch_Event.DownIng_Event)
        {
        DeBug("TOUCH_EVENT_DOWNING");
        Touch_Event.DownIng_Event=Null;
        }
        }
         
        void Touch_UpDelayEvent()
        {
        if(Touch_Event.Up_Event)
        {
        DeBug("TOUCH_EVENT_UP");
        Touch_Event.Up_Event=Null;
        }
        }
         
        void Touch_DownDelayEvent()
        {
        if(Touch_Event.Down_Event)
        {
        DeBug("TOUCH_EVENT_DOWN");
        Touch_Event.Down_Event=Null;
        }
        }
         
        void Task_GetTouch(void *Tags)
        {
        static uint32 Temp_ADC1,Temp_ADC2,Temp_ADC11;
        TOUCH_Tos_TaskID=Tos_TaskCurrent;
        while(1)
        {
         
        Tos_TaskDelay(100);
        Touch_ADCX_Config();
        Tos_TaskDelay(10);
        Temp_ADC11=AvrFilter_ByWords((uint16 *)&TOUCH_ADC_Value[0][1],TOUCH_ADC_BUFSize,TOUCH_ADC_BUFSize);
        if(Temp_ADC11>100)
        {
        Tos_TaskDelay(10);
        Temp_ADC1=AvrFilter_ByWords((uint16 *)&TOUCH_ADC_Value[0][1],TOUCH_ADC_BUFSize,TOUCH_ADC_BUFSize);
        Touch_ADCY_Config();
        Tos_TaskDelay(10);
        Temp_ADC2=AvrFilter_ByWords((uint16 *)&TOUCH_ADC_Value[0][0],TOUCH_ADC_BUFSize,TOUCH_ADC_BUFSize);
        if(__fabs(Temp_ADC11-Temp_ADC1)<20&Temp_ADC2>400)//消抖
        {
        SPK_ON;
        TOUCH_ADC_YValue=Temp_ADC2;
        TOUCH_ADC_XValue=Temp_ADC1;
        if(Touch_Update==TOUCH_DOWNING){Touch_Update=TOUCH_DOWN;}
        if(Touch_Update!=TOUCH_DOWN)
        {
        Touch_Update=TOUCH_DOWNING;
        Touch_Event.DownIng_Event=Touch_DownIngEvent;
        Touch_Event.Down_Event=Touch_DownDelayEvent;
        if(Touch_Event.DownIng_Event)Touch_Event.DownIng_Event();//當存在按下事件時觸發
        Touch_Event.UpIng_Event=Touch_UpIngEvent;//////事件測試
        Touch_Event.DownDelay_Count=0;
        }
        if(Touch_Update==TOUCH_DOWN)
        {
        if(Touch_Event.DownDelay_Count
        {
        Touch_Event.DownDelay_Count++;
        }else if(Touch_Event.Down_Event)Touch_Event.Down_Event();//當存在持續按下事件時觸發
        }
        SPK_OFF;
        DeBug("X=%d Y=%d",TOUCH_ADC_XValue,TOUCH_ADC_YValue);
         
        }
        }else
        {
        if(Touch_Update==TOUCH_UPING){Touch_Update=TOUCH_UP;}
        if(Touch_Update!=TOUCH_UP)//觸摸屏第一次按下觸發
        {
        Touch_Update=TOUCH_UPING;
        if(Touch_Event.UpIng_Event)Touch_Event.UpIng_Event();//當存在放下動作時觸發
        Touch_Event.UpDelay_Count=0;//按下第一次時清除按下時間計數
        Touch_Event.Up_Event=Touch_UpDelayEvent;//////事件測試
        }
        if(Touch_Update==TOUCH_UP)//觸摸屏一直按下
        {
         
        if(Touch_Event.UpDelay_Count
        {
         
        Touch_Event.UpDelay_Count++;
        }else if(Touch_Event.Up_Event)Touch_Event.Up_Event();//當存在持續未按下觸發
        }
        }
        }
        }
         
        void Touch_ADC1_Configuration(void)
        {
        ADC_InitTypeDef ADC_InitStructure;
        ADC_DeInit(ADC1); //將外設 ADC1 的全部寄存器重設為缺省值
        ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在獨立模式
        ADC_InitStructure.ADC_ScanConvMode =ENABLE; //模數轉換工作在掃描模式
        ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模數轉換工作在連續轉換模式
        ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //外部觸發轉換關閉
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC數據右對齊
        ADC_InitStructure.ADC_NbrOfChannel =TOUCH_ADC_CHN; //順序進行規則轉換的ADC通道的數目
        ADC_Init(ADC1, &ADC_InitStructure); //根據ADC_InitStruct中指定的參數初始化外設ADCx的寄存器
        ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_239Cycles5);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 2, ADC_SampleTime_239Cycles5);
        ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1
        ADC_ResetCalibration(ADC1); //復位指定的ADC1的校準寄存器
        while(ADC_GetResetCalibrationStatus(ADC1)); //獲取ADC1復位校準寄存器的狀態,設置狀態則等待
        ADC_StartCalibration(ADC1); //開始指定ADC1的校準狀態
        while(ADC_GetCalibrationStatus(ADC1)); //獲取指定ADC1的校準程序,設置狀態則等待
        }
        void Touch_DMA_Configuration(void)
        {
        DMA_InitTypeDef DMA_InitStructure;
        DMA_DeInit(DMA1_Channel1); //將DMA的通道1寄存器重設為缺省值
        DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //DMA外設ADC基地址
        DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&TOUCH_ADC_Value; //DMA內存基地址
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //內存作為數據傳輸的目的地
        DMA_InitStructure.DMA_BufferSize = TOUCH_ADC_BUFSize*TOUCH_ADC_CHN; //DMA通道的DMA緩存的大小
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外設地址寄存器不變
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //內存地址寄存器遞增
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //數據寬度為16位
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //數據寬度為16位
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //工作在循環緩存模式
        DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA通道 x擁有高優先級
        DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x沒有設置為內存到內存傳輸
        DMA_Init(DMA1_Channel1, &DMA_InitStructure); //根據DMA_InitStruct中指定的參數初始化DMA的通道
        ADC_SoftwareStartConvCmd(ADC1, ENABLE);
        DMA_Cmd(DMA1_Channel1, ENABLE);
        DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
        ADC_DMACmd(ADC1, ENABLE);
        }
        void Touch_ADC_Init()
        {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, ENABLE);
        Touch_ADC1_Configuration();
        Touch_DMA_Configuration();
        ADC_SoftwareStartConvCmd(ADC1, ENABLE);
        DMA_Cmd(DMA1_Channel1, ENABLE);
        Touch_Event.UpIng_Event=Null;
        Touch_Event.Up_Event=Null;
        Touch_Event.DownIng_Event=Null;
        Touch_Event.Down_Event=Null;
        Touch_Event.UpDelay_Count=0;//延遲周期數后執行延遲任務
        Touch_Event.DownDelay_Count=0;//延遲周期數后執行延遲任務
        }
         
         
        void DeviceMount_Touch()
        {
        _Tos_Device_Tab[DeviceId_TOUCH].DeviceId=DeviceId_TOUCH;
        _Tos_Device_Tab[DeviceId_TOUCH].DeviceName="TOUCH";
        _Tos_Device_Tab[DeviceId_TOUCH].DeviceOwnerId=Null;
        _Tos_Device_Tab[DeviceId_TOUCH].DeviceVirtue=ReadOnly|WriteOnly|CharDerice;
        _Tos_Device_Tab[DeviceId_TOUCH].DeviceState=NotBusy;
        _Tos_Device_Tab[DeviceId_TOUCH].Init=Touch_ADC_Init;
        _Tos_Device_Tab[DeviceId_TOUCH].Write=Null;
        _Tos_Device_Tab[DeviceId_TOUCH].Read=Null;
        _Tos_Device_Tab[DeviceId_TOUCH].Exit=Null;
        }
         
         
         
         
         
         
        void TouchScreen_DrawCross(uint16 X0,uint16 Y0,uint16 R,uint16 FColor)
        {
        TGui_Draw_Line(X0-R,Y0,X0+R,Y0,FColor);
        TGui_Draw_Line(X0,Y0-R,X0,Y0+R,FColor);
        }
        void TouchScreen_GetTouch(uint16 *VX,uint16 *VY)
        {
        uint16 X_BUF[4];
        uint16 Y_BUF[10];
        uint8 i;
        while(1)
        {
        Tos_TaskDelay(10);
        if(Touch_Update==TOUCH_DOWN)
        {
        *VX=TOUCH_ADC_XValue;
        // *VY=TOUCH_ADC_YValue;
        return;
        while(1)
        {
        if(Touch_Update==TOUCH_UP)return;
        Tos_TaskDelay(10);
        }
        }
        }
        }
         
        void TouchScreen_Correct()
        {
        int8 i;
        uint16 X,Y;
         
        for(i=0;i<5;i++)
        {
        LCD_Clear_Screen(Color_Black);
        TouchScreen_DrawCross(TOUCH_CROSS_ADDR[i][0],TOUCH_CROSS_ADDR[i][1],TOUCH_CROSS_SIZE,Color_White);
        TouchScreen_GetTouch(&X,&Y);
         
        DeBug("nihao");
        Tos_TaskDelay(1000);
        }
        }
        關閉窗口

        相關文章

        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮