最近做了一個小項目,里面一個小部分,基于STM32刷卡帶觸摸屏,功能可能有點簡單,但是可以繼續往下編。還有刷卡號的識別要更改。
使用觸摸屏-SPI程序時如果出現觸摸不靈或者誤差大,可以按照下面方法進行校正修改,如果使用可以強制校準程序則不需要,只需通過K_UP按鍵強制校準即可。
如果下載程序后對觸摸屏劃線,沒有出現線條,可以顯示觸摸值變化,這個是因為沒有對觸摸屏校正,因此只需要在 touch.c 這個文件內的函數 void TOUCH_Init(void),將這個函數內的語句
if(TouchAdj.posState != TOUCH_ADJ_OK)
{
TOUCH_Adjust(); //校正
}
改成
if(TouchAdj.posState == TOUCH_ADJ_OK)
{
TOUCH_Adjust(); //校正
}
編譯后將HEX下載到開發板內進行觸摸屏校正,校正完成后再修改為!=就可以了。
0.png (50.75 KB, 下載次數: 179)
下載附件
2017-8-18 23:14 上傳
本帖附件中可下載此壓縮包
stm32單片機源程序如下(主程序):
- /* 程序下載進去后D1指示燈閃爍,表示程序正常運行,按下按鍵K_UP可以強制觸摸校正 */
- #include "stm32f10x.h"
- #include "led.h"
- #include "system.h"
- #include "gui.h"
- #include "touch.h"
- #include "flash.h"
- #include "key.h"
- #include "systick.h"
- #include "tim.h"
- #include "RC522.h"
- #include "usart.h"
- uint16_t penColor;
- unsigned char data2[4] = {0,0,0,0x01};
- unsigned char DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
- unsigned char g_ucTempbuf[20];
- void kai_display() //開機顯示
- {
- TFT_ClearScreen(BLACK);
- GUI_Show12Char(10,103,"歡迎使用",GREEN,BLACK);
- GUI_Show12Char(10,10,"K_UP:Adjust",GREEN,BLACK);
- }
- void lcd_display() //LCD初始化顯示
- {
- TFT_ClearScreen(BLACK);
- TFT_ClearScreen(BLACK);
- GUI_Show12Char(10,50,"關門 ",RED,BLACK); //關門
- GUI_Show12Char(10,150," ",YELLOW,BLACK);
- }
- void display_init() //初始化顯示
- {
- TFT_ClearScreen(BLACK);
- GUI_Show12Char(0, TFT_YMAX - 16, "X:", RED, BLACK);
- GUI_Show12Char(56, TFT_YMAX - 16, "Y:", RED, BLACK);
- GUI_Show12Char(TFT_XMAX-8*3, 0, "RST", RED, BLACK);
- GUI_Show12Char(56, TFT_YMAX - 106, "一號存儲:", RED, BLACK);
- GUI_Show12Char(56, TFT_YMAX - 156, "二號存儲:", RED, BLACK);
- GUI_Show12Char(56, TFT_YMAX - 206, "三號存儲:", RED, BLACK);
- }
- int main(void)
- {
- unsigned char status,i;
- unsigned int temp;
- uint8_t touchValue[5] = {0, 0, 0, 0, 0};
- // uint32_t i;
- penColor = BLUE;
- RC522_Init();
- /* 初始化 */
- PcdReset();
- PcdAntennaOff();
- TFT_Init();
- FLASH_Init();
- LED_Config();
- KEY_Config();
- SYSTICK_Config();
- delay_ms(10);
- PcdAntennaOn();
- delay_ms(10);
- tim3_init(1000,7199);
- uart_init(9600);
- kai_display();
- SYSTICK_Delay1ms(1500);
- TOUCH_Init();
- display_init();
- while(1)
- {
- if(KEY_Scan()==KEY_UP) //按下K_UP按鍵 強制校準
- {
- TOUCH_Adjust(); //校正
- display_init();
- }
- if(TOUCH_Scan() == 0)
- {
- /* 顯示X軸的物理坐標值 */
- touchValue[0] = (TouchData.x % 10000 /1000) + '0';
- touchValue[1] = (TouchData.x % 1000 /100) + '0';
- touchValue[2] = (TouchData.x % 100 /10) + '0';
- touchValue[3] = (TouchData.x % 10) + '0';
- GUI_Show12Char(16, TFT_YMAX - 16, touchValue, RED, BLACK);
- /* 顯示Y軸的物理坐標值 */
- touchValue[0] = (TouchData.y % 10000 /1000) + '0';
- touchValue[1] = (TouchData.y % 1000 /100) + '0';
- touchValue[2] = (TouchData.y % 100 /10) + '0';
- touchValue[3] = (TouchData.y % 10) + '0';
-
- GUI_Show12Char(72, TFT_YMAX - 16, touchValue, RED, BLACK);
- }
- status = PcdRequest(PICC_REQALL, g_ucTempbuf);//尋卡
- if (status != MI_OK)
- {
- PcdReset();
- PcdAntennaOff();
- PcdAntennaOn();
- continue;
- }
- printf("卡的類型:");
- for(i=0;i<2;i++)
- {
- temp=g_ucTempbuf[i];
- printf("%X",temp);
-
- }
- status = PcdAnticoll(g_ucTempbuf);//防沖撞
- if(status != MI_OK)
- { continue; }
-
- ////////以下為超級終端打印出的內容////////////////////////
-
- printf("卡序列號:"); //超級終端顯示,
- for(i=0;i<4;i++)
- {
- temp=g_ucTempbuf[i];
- printf("%X",temp);
- }
- if(g_ucTempbuf[0]==0x37&&g_ucTempbuf[1]==0x25&&g_ucTempbuf[2]==0xAA&&g_ucTempbuf[3]==0x29)
- {
- GUI_Show12Char(10,50,"開門 ",GREEN,BLACK); //開門
- GUI_Show12Char(10,10,"姓名:一號 ",RED,BLACK);
- // GPIO_SetBits(GPIOC, (uint16_t)0xff & 0x00FF);
- if((TouchData.lcdx>50)&&(TouchData.lcdx<100))
- {
- if((TouchData.lcdy>280)&&(TouchData.lcdy<330))
- {
- GPIO_ResetBits(GPIOC, (~(uint16_t)0xfe) & 0x00FF);
- delay_ms(50000);
- // TouchData.lcdx=0;
- // TouchData.lcdy=0;
- // delay_ms(10);
- // GPIO_SetBits(GPIOC, (uint16_t)0xfe& 0x00FF);
- }
- if((TouchData.lcdy>220)&&(TouchData.lcdy<270))
- {
- GPIO_ResetBits(GPIOC, (~(uint16_t)0xfd) & 0x00FF);
- delay_ms(50000);
- // TouchData.lcdx=0;
- // TouchData.lcdy=0;
- // delay_ms(10);
- // GPIO_SetBits(GPIOC, (uint16_t)0xfd & 0x00FF);
- }
- if((TouchData.lcdy>120)&&(TouchData.lcdy<180))
- {
- GPIO_ResetBits(GPIOC, (~(uint16_t)0xfb) & 0x00FF);
- delay_ms(50000);
- // TouchData.lcdx=0;
- // TouchData.lcdy=0;
- // delay_ms(10);
- // GPIO_SetBits(GPIOC, (uint16_t)0xfb & 0x00FF);
- }
- if ((TouchData.lcdx > TFT_XMAX-8*3) && (TouchData.lcdy < 16))//215 = TFT_XMAX - 24
- {
- GPIO_SetBits(GPIOC, (uint16_t)0xff & 0x00FF);
- }
- delay_ms(50000);
- GPIO_SetBits(GPIOC, (uint16_t)0xff & 0x00FF);
- TouchData.lcdx=0;
- TouchData.lcdy=0;
- }
- }
- else
- {
- if(g_ucTempbuf[0]==0x1A&&g_ucTempbuf[1]==0x46&&g_ucTempbuf[2]==0x95&&g_ucTempbuf[3]==0x1E)
- {
- // led1=0;
- GUI_Show12Char(10,50,"開門 ",GREEN,BLACK); //開門
- GUI_Show12Char(10,10,"姓名:二號 ",RED,BLACK);
- // GPIO_SetBits(GPIOC, (uint16_t)0xff & 0x00FF);
- if((TouchData.lcdx>50)&&(TouchData.lcdx<100))
- {
- if((TouchData.lcdy>280)&&(TouchData.lcdy<330))
- {
- GPIO_ResetBits(GPIOC, (~(uint16_t)0xfe) & 0x00FF);
- delay_ms(50000);
- TouchData.lcdx=0;
- TouchData.lcdy=0;
- // delay_ms(10);
- // GPIO_SetBits(GPIOC, (uint16_t)0xfe& 0x00FF);
- }
- if((TouchData.lcdy>220)&&(TouchData.lcdy<270))
- {
- GPIO_ResetBits(GPIOC, (~(uint16_t)0xfd) & 0x00FF);
- delay_ms(50000);
- TouchData.lcdx=0;
- TouchData.lcdy=0;
- // delay_ms(10);
- // GPIO_SetBits(GPIOC, (uint16_t)0xfd & 0x00FF);
- }
- if((TouchData.lcdy>120)&&(TouchData.lcdy<180))
- {
- GPIO_ResetBits(GPIOC, (~(uint16_t)0xfb) & 0x00FF);
- delay_ms(50000);
- TouchData.lcdx=0;
- TouchData.lcdy=0;
- // delay_ms(10);
- // GPIO_SetBits(GPIOC, (uint16_t)0xfb & 0x00FF);
- }
- if ((TouchData.lcdx > TFT_XMAX-8*3) && (TouchData.lcdy < 16))//215 = TFT_XMAX - 24
- {
- GPIO_SetBits(GPIOC, (uint16_t)0xff & 0x00FF);
- }
- }
- if ((TouchData.lcdx > TFT_XMAX-8*3) && (TouchData.lcdy < 16))//215 = TFT_XMAX - 24
- {
- GPIO_SetBits(GPIOC, (uint16_t)0xff & 0x00FF);
- }
-
- }
- else
- {
- // led1=1;
- GUI_Show12Char(10,50,"刷卡無法識別 ",RED,BLACK);
- }}}}
復制代碼
所有資料51hei提供下載:
觸摸屏刷卡.rar
(1.95 MB, 下載次數: 580)
2017-8-18 23:16 上傳
點擊文件名下載附件
卡號要更改 下載積分: 黑幣 -5
|