最近做了一个小项目,里面一个小部分,基于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下载到开发板内进行触摸屏校正,校正完成后再修改为!=就可以了。
本帖附件中可下载此压缩包
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;
- printf("%X",temp);
- }
- status = PcdAnticoll(g_ucTempbuf);//防冲撞
- if(status != MI_OK)
- { continue; }
- ////////以下为超级终端打印出的内容////////////////////////
- printf("卡序列号:"); //超级终端显示,
- for(i=0;i<4;i++)
- {
- temp=g_ucTempbuf;
- 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
|