stm32f103单片机驱动4.3寸触摸屏(含松手检测),加入蓝牙控制。
单片机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "key.h"
- #include "touch.h"
- #include <math.h>
- #include "LanYa.h"
- ////////////////////////////////////////////////////////////////////////////////
- //5个触控点的颜色
- const u16 POINT_COLOR_TBL[CT_MAX_TOUCH]={RED,GREEN,BLUE,BROWN,GRED};
- extern const unsigned char gImage_0[600];
- extern const unsigned char gImage_1[600];
- extern const unsigned char gImage_2[600];
- extern const unsigned char gImage_3[600];
- extern const unsigned char gImage_4[600];
- extern u8 receive_data[10];
- extern u8 ChuLi_data[10];
- //显示按键函数
- void Lcd_XianShi_AnJian(void)
- {
- u8 i;
-
- for(i=0;i<50;i++)LCD_Circle(100,300,BLUE,i);//画圆
- LCD_DrawRectangle(210, 250, 410, 350); //画矩形
- LCD_Fill(210, 250, 410, 350,BROWN); //填充指定颜色
- LCD_ShowString(260,300,200,24,24,"曲线");
-
- // for(i=0;i<50;i++)LCD_Circle(100,600,BLUE,i);//画圆
- // LCD_DrawRectangle(210, 550, 410, 650); //画矩形
- // LCD_Fill(210, 550, 410,650,BROWN); //填充指定颜色
- // LCD_ShowString(260,600,200,24,24,"公司");
- LCD_DrawRectangle(0, 740, 80, 800); //画矩形,(x1,y1),(x2,y2):矩形的对角坐标, “\”对角线
- LCD_Fill(0, 740, 80, 800,BROWN); //填充指定颜色
- LCD_ShowString(15,760,200,24,24,"按键1"); //按键1
-
- LCD_DrawRectangle(100, 740, 180, 800); //画矩形,(x1,y1),(x2,y2):矩形的对角坐标
- LCD_Fill(100, 740, 180, 800,RED); //填充指定颜色
- LCD_ShowString(115,760,200,24,24,"按键2"); //按键2
-
- LCD_DrawRectangle(200, 740, 280, 800); //画矩形,(x1,y1),(x2,y2):矩形的对角坐标
- LCD_Fill(200, 740, 280, 800,GREEN); //填充指定颜色
- LCD_ShowString(215,760,200,24,24,"按键3"); //按键3
-
- LCD_DrawRectangle(300, 740, 380, 800); //画矩形,(x1,y1),(x2,y2):矩形的对角坐标
- LCD_Fill(300, 740, 380, 800,BLUE); //填充指定颜色
- LCD_ShowString(315,760,200,24,24,"按键4"); //按键4
- LCD_DrawRectangle(400, 740, 480, 800); //画矩形,(x1,y1),(x2,y2):矩形的对角坐标
- LCD_Fill(400, 740, 480, 800,GRED); //填充指定颜色
- LCD_ShowString(415,760,200,24,24,"按键5"); //按键5
-
- LCD_Draw_Picture(0,0,80,60,(u8 *)gImage_0);
- LCD_Draw_Picture(80,0,160,60,(u8 *)gImage_1);
- LCD_Draw_Picture(160,0,240,60,(u8 *)gImage_2);
- LCD_Draw_Picture(240,0,320,60,(u8 *)gImage_3);
- LCD_Draw_Picture(320,0,400,60,(u8 *)gImage_4);
-
-
- }
- void LanYa_ShuJu_XianShi(u8 *receive_data,u8 *ChuLi_data)
- {
- u8 Data[400];
- sprintf(Data,"蓝牙接收数据:%s",receive_data);
- LCD_ShowString(10,100,300,24,24,(u8 *)&Data); //
-
- sprintf(Data,"蓝牙接收命令:%s",ChuLi_data);
- LCD_ShowString(10,130,300,24,24,(u8 *)&Data); //
- }
- //此函数显示波形框
- void XinShi_Boxing_Kuang(void)
- {
- u16 x,y;
- LCD_ShowString(15,590,200,24,24,"Y");
- LCD_ShowString(10,620,200,24,24,"轴");
- LCD_ShowString(50,655,200,24,24,"X");
- LCD_ShowString(80,655,200,24,24,"轴");
-
- LCD_DrawRectangle(50, 400, 480, 650); //画矩形,(x1,y1),(x2,y2):矩形的对角坐标
- LCD_Fill(50, 400, 480, 650,BLACK); //填充指定颜色
- for(x=50;x<480;x++)
- {
- for(y=400;y<650;y++)
- {
- if((x%50==0) && (y%50==0))
- {
- LCD_Fast_DrawPoint(x,y,WHITE); //快速画点
- }
- }
- }
- }
- //显示波形
- void BoXing_XianShi(void)
- {
- u16 x,y,T=10;
- for(x=50;x<480;x++)
- {
- y=650-T*sin(T*x);
- LCD_Fast_DrawPoint(x,y,YELLOW); //快速画点
- delay_ms(20);
- }
- }
- //电容触摸屏测试函数
- void ctp_test1(void)
- {
- u8 t=0;
- u8 i=0;
- u8 l0,l1;
- // u8 L1_flag=0,L2_flag=0;
- u16 lastpos[5][2]; //记录最后一次的数据
- while(1)
- {
-
- LanYa_ShuJu_XianShi(receive_data,ChuLi_data);
-
- tp_dev.scan(0);
- for(t=0;t<CT_MAX_TOUCH;t++)
- {
- if((tp_dev.sta)&(1<<t))
- {
- if(tp_dev.x[t]<lcddev.width&&tp_dev.y[t]<lcddev.height)
- {
- if(lastpos[t][0]==0XFFFF)
- {
- lastpos[t][0] = tp_dev.x[t];
- lastpos[t][1] = tp_dev.y[t];
- }
- //=========按键操作========
- if((tp_dev.x[t]>210)&&(tp_dev.x[t]<410)){
-
-
- if((tp_dev.y[t]>250)&&(tp_dev.y[t]<350)){//在按键LED1的坐标范围内
-
- while(tp_dev.sta&TP_PRES_DOWN) //松手检测
- {
- tp_dev.scan(0);
- }
-
- l0=~l0;
- if(l0)
- for(i=0;i<50;i++)LCD_Circle(100,300,RED,i); //画圆
- else
- for(i=0;i<50;i++)LCD_Circle(100,300,GREEN,i); //画圆
-
- }else if((tp_dev.y[t]>550)&&(tp_dev.y[t]<650)){//在按键LED2的坐标范围内
-
- while(tp_dev.sta&TP_PRES_DOWN) //松手检测
- {
- tp_dev.scan(0);
- }
-
- l1=~l1;
- if(l1)
- for(i=0;i<50;i++)LCD_Circle(100,600,RED,i);
- else
- for(i=0;i<50;i++)LCD_Circle(100,600,GREEN,i);
- }
-
- }
- if((tp_dev.y[t]>760)&&(tp_dev.y[t]<800)){
- if((tp_dev.x[t]>0)&&(tp_dev.x[t]<80)){ //按键1检测
- while(tp_dev.sta&TP_PRES_DOWN) //松手检测
- {
- tp_dev.scan(0);
- }
- l0=~l0;
- if(l0)
- for(i=0;i<50;i++)LCD_Circle(100,300,RED,i); //画圆
- else
- for(i=0;i<50;i++)LCD_Circle(100,300,GREEN,i); //画圆
- }else if((tp_dev.x[t]>100)&&(tp_dev.x[t]<180)){ //按键2检测
- while(tp_dev.sta&TP_PRES_DOWN) //松手检测
- {
- tp_dev.scan(0);
- }
- l0=~l0;
- if(l0)
- for(i=0;i<50;i++)LCD_Circle(100,300,RED,i); //画圆
- else
- for(i=0;i<50;i++)LCD_Circle(100,300,GREEN,i); //画圆
- }else if((tp_dev.x[t]>200)&&(tp_dev.x[t]<280)){ //按键3检测
- while(tp_dev.sta&TP_PRES_DOWN) //松手检测
- {
- tp_dev.scan(0);
- }
- l0=~l0;
- if(l0)
- for(i=0;i<50;i++)LCD_Circle(100,300,RED,i); //画圆
- else
- for(i=0;i<50;i++)LCD_Circle(100,300,GREEN,i); //画圆
- }else if((tp_dev.x[t]>300)&&(tp_dev.x[t]<380)){ //按键4检测
- while(tp_dev.sta&TP_PRES_DOWN) //松手检测
- {
- tp_dev.scan(0);
- }
- l0=~l0;
- if(l0)
- for(i=0;i<50;i++)LCD_Circle(100,300,RED,i); //画圆
- else
- for(i=0;i<50;i++)LCD_Circle(100,300,GREEN,i); //画圆
- }else if((tp_dev.x[t]>400)&&(tp_dev.x[t]<480)){ //按键5检测
- while(tp_dev.sta&TP_PRES_DOWN) //松手检测
- {
- tp_dev.scan(0);
- }
- l0=~l0;
- if(l0)
- for(i=0;i<50;i++)LCD_Circle(100,300,RED,i); //画圆
- else
- for(i=0;i<50;i++)LCD_Circle(100,300,GREEN,i); //画圆
- }
- }
- }
- }else lastpos[t][0]=0XFFFF;
- }
-
- delay_ms(5);i++;
- if(i%20==0)LED0=!LED0;
- }
- }
- int main(void)
- {
-
- uart_init(115200); //串口初始化为115200
- Uart3_Init(9600);
- delay_init(); //延时初始化
- LED_Init(); //初始化与LED连接的硬件接口
- LCD_Init(); //初始化LCD
- KEY_Init(); //按键初始化
- tp_dev.init(); //触摸屏初始化
- POINT_COLOR=RED;//设置字体为红色
-
- Lcd_XianShi_AnJian();
- XinShi_Boxing_Kuang();
- // BoXing_XianShi(); //显示正弦波
-
- if(tp_dev.touchtype&0X80)ctp_test1(); //电容屏测试
- }
复制代码
所有代码51hei附件下载:
触摸屏控制+蓝牙.7z
(260.79 KB, 下载次数: 42)
|