中考結束,成績不錯重點高中,更新帖子。這次帶來的是觸摸的使用我用的是RTrobot 家的2.4寸帶觸摸屏幕(某一個寶找不到pdd有),MCU是HC32F460kcta。飛點也解決的比較好具體的大家看代碼。
單片機xpt2046源程序如下
- #include "xpt2046.h"
- void XPT2046_init(void)
- {
- stc_port_init_t stcPortInit;
- /* configuration structure initialization */
- MEM_ZERO_STRUCT(stcPortInit);
- stcPortInit.enPinMode = Pin_Mode_Out;
- stcPortInit.enExInt = Enable;
- stcPortInit.enPullUp = Enable;
- PORT_Init(XPT2046_PIN_PORT ,XPT2046_CS_PIN , &stcPortInit);
- stcPortInit.enPinMode = Pin_Mode_In;
- PORT_Init(XPT2046_PIN_PORT ,XPT2046_PEN_PIN , &stcPortInit);
- PORT_Unlock();
- M4_PORT->PSPCR = 0x0Bu;
- PORT_Lock();
-
- XPT2046_CS_Set();
- }
-
- void send_8way(uint8_t data)
- {
- while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagSpiIdle))
- {}
- SPI_SendData8(SPI_UNIT,data);
- while (Reset == SPI_GetFlag(SPI_UNIT,SpiFlagReceiveBufferFull))
- {}
- SPI_ReceiveData8(SPI_UNIT);
- }
-
- uint8_t receiv_8way(void)
- {
- while (Reset == SPI_GetFlag(SPI_UNIT, SpiFlagSpiIdle))
- {}
- SPI_SendData8(SPI_UNIT,0x00);
- while (Reset == SPI_GetFlag(SPI_UNIT,SpiFlagReceiveBufferFull))
- {}
- return SPI_ReceiveData8(SPI_UNIT);
- }
-
- int cmp(const void *a, const void *b)
- {
- return *(uint16_t *)a - *(uint16_t *)b; //從小到大排序(b-a則是從大到小)。需要與被測類型一致,否則排序亂七八糟
- }
- static const uint8_t count = REPEATED_SAMPLING_TIMES; //采集次數
- u16 x_data[count], y_data[count];
- void XPT2046_Rd_Addata(uint16_t *rxdata,uint16_t *rydata)
- {
- if(PORT_GetBit(XPT2046_PIN_PORT,XPT2046_PEN_PIN)==Reset)//觸摸按下了
- {
- Ddl_Delay1ms(30);
- if(PORT_GetBit(XPT2046_PIN_PORT,XPT2046_PEN_PIN)==Reset)
- {
- XPT2046_CS_Clr();
- SPI_SetClockDiv(SPI_UNIT,SpiClkDiv32);
- Ddl_Delay1us(10);
- for (uint8_t i = 0; i < count; i++)
- {
- send_8way(0xd0); //發送命令
- Ddl_Delay1us(8);
- xdata = receiv_8way();
- xdata <<= 8;
- xdata |= receiv_8way();
- xdata >>= 3;
- x_data[i] = xdata & 0xFFF;
-
- send_8way(0x90);
- Ddl_Delay1us(8);
- ydata = receiv_8way();
- ydata <<= 8;
- ydata |= receiv_8way();
- ydata >>= 3;
- y_data[i] = ydata & 0xFFF;
- Ddl_Delay1us(200);
- }
- qsort(x_data, count, sizeof(x_data[0]), cmp); //stdlib.h里的排序函數
- qsort(y_data, count, sizeof(y_data[0]), cmp);
- *rydata = x_data[count >> 1]; //取中間值,因為是橫屏所以XY是反的
- *rxdata = y_data[count >> 1];
- XPT2046_CS_Set();
- }
- }
- }
- void Show_circle(u16 x,u16 y,u16 c)
- {
- Draw_Circle(x,y,10,c);
- LCD_DrawLine(x-10,y,x+10,y,c);
- LCD_DrawLine(x,y-10,x,y+10,c);
- }
- void Touch_Adjust(float32_t *xdata,float32_t *ydata)
- {
- uint16_t x1_data,y1_data,x2_data,y2_data;
- LCD_ShowString(0,200,"Please click on the red dot wait 5s",BLACK,WHITE,16,0);
- Show_circle(10,10,RED);
- Ddl_Delay1ms(3000);
- XPT2046_Rd_Addata(&x1_data,&y1_data);
- Ddl_Delay1ms(2000);
- LCD_ShowString(0,200,"Please click on the green dot wait 5s",BLACK,WHITE,16,0);
- Show_circle(310,230,GREEN);
- Ddl_Delay1ms(3000);
- XPT2046_Rd_Addata(&x2_data,&y2_data);
- Ddl_Delay1ms(2000);
- *xdata = (float32_t)(x2_data-x1_data)/(300);
- *ydata = (float32_t)(y1_data-y2_data)/(220);
-
- LCD_Fill(0,0,320,240,WHITE);
- LCD_ShowString(0,200,"You are good",BLACK,WHITE,16,0);
-
- }
- void XPT2046_getPoint(u16 *x, u16 *y)
- {
- uint16_t xdata=0,ydata=0;
- XPT2046_Rd_Addata(&xdata,&ydata);
- *x = (u16)xdata/11.1633329-15;//由于掃描方式的不同這里的參數需要根據實際修改.
- *y = (u16)ydata/-15.4863634+255;
- }
復制代碼
main
- #include "hc32_ddl.h"
- #include "Kthermocouple.h"
- #include "userADC.h"
- #include "userUSART.h"
- #include "userSPI.h"
- #include "userPWM.h"
- #include "ev_hc32f460_lqfp100_v2.h"
- #include "rtthread.h"
- #include "lcd.h"
- #include "xpt2046.h"
- static void touch_thread_init(void);
- static struct rt_thread touch_thread;
- ALIGN(RT_ALIGN_SIZE)
- static rt_uint8_t rt_touch_thread_stack[1024];
- static void touch_thread_entry(void * para);
- static rt_uint8_t touch_thread_priority = 6;
- /**
- *******************************************************************************
- ** \brief Main function of template project
- **
- ** \param None
- **
- ** \retval int32_t return value, if needed
- **
- ******************************************************************************/
- int32_t main(void)
- {
- SPI1_Init();
- XPT2046_init();
- LCD_Init();
- LCD_Fill(0,0,320,240,WHITE);
- Ddl_Delay1ms(1000);
- touch_thread_init();
- }
- static void touch_thread_init(void)
- {
- rt_thread_init(&touch_thread,
- "touch_thread",
- touch_thread_entry,
- RT_NULL,
- &rt_touch_thread_stack,
- sizeof(rt_touch_thread_stack),
- touch_thread_priority,
- 20);//時間 20ms
- rt_thread_startup(&touch_thread);
- }
- static void touch_thread_entry(void * para)
- {
- u16 tx=0,ty=0;
- DDL_Printf("touch_thread_runing. \r\n");
- while(1)
- {
- if(PORT_GetBit(XPT2046_PIN_PORT,XPT2046_PEN_PIN)==Reset)
- {
- XPT2046_getPoint(&tx,&ty);
- LCD_Fill(tx-2,ty-2,tx+2,ty+2,RED);
- }
-
- }
- }
復制代碼
Keil代碼下載:
HC32F460TOUCH工程.7z
(2.8 MB, 下載次數: 33)
2022-7-17 14:53 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|