添加了一些注释

This commit is contained in:
2021-08-10 16:24:16 +08:00
parent 697e23bebf
commit 41a158898b
+34 -20
View File
@@ -154,45 +154,49 @@ void TP_adjustment()
{ {
//判断是否需要校准,从eeprom获取数据 //判断是否需要校准,从eeprom获取数据
EEPROM_READ_BATY(16,(char *)&tconfig,sizeof(touch_config)); EEPROM_READ_BATY(16,(char *)&tconfig,sizeof(touch_config));
if(tconfig.begin==0xab&&tconfig.end==0xcd) if(tconfig.begin==0xab&&tconfig.end==0xcd) //判断校准标记
{ {
return; return; //已经校准过了
} }
//校准方法比较简单,读取4个坐标计算ad值与像素的关系
char str[64]; char str[64]; //用于字符串提示
uint16_t y_adc,x_adc,step=0,r=10; uint16_t y_adc,x_adc,step=0,r=10; //adc缓存,校准步骤,坐标的半径
uint16_t y1,y2,y3,y4,x1,x2,x3,x4; uint16_t y1,y2,y3,y4,x1,x2,x3,x4; //4个点缓存
int y5,x5,xd,xl,yd,yl; int y5,x5,xd,xl,yd,yl; //通过4个点算出xy的长边和短边
float acc_x,acc_y; float acc_x,acc_y; //算出的关系倍率
int offset_x,offset_y; int offset_x,offset_y; //算出的偏差
uint32_t wait=HAL_GetTick()+50000,ms100=0; uint32_t wait=HAL_GetTick()+50000,ms100=0; //校准时间,50秒没操作就自动退出
//显示字符串提示
LCD_Clear(GRAY); LCD_Clear(GRAY);
LCD_ShowString(0,50,"Calibrate the touch screen",16,RED,RED); LCD_ShowString(0,50,"Calibrate the touch screen",16,RED,RED);
//TP_DrwaTrage(30,30,10); //TP_DrwaTrage(30,30,10);
//开始校准
while(HAL_GetTick()<wait) while(HAL_GetTick()<wait)
{ {
if(TPEN==0) if(TPEN==0) //如果屏幕被按下
{ {
wait=HAL_GetTick()+50000; wait=HAL_GetTick()+50000; //重置50秒
TP_Read_XY2(&x_adc,&y_adc); TP_Read_XY2(&x_adc,&y_adc); //读取xy ad值
//将读到的值显示出来
sprintf(str,"ADC_X:%04d",x_adc); sprintf(str,"ADC_X:%04d",x_adc);
LCD_ShowString(100, 0, str, 16, RED, GRAY); LCD_ShowString(100, 0, str, 16, RED, GRAY);
sprintf(str,"ADC_Y:%04d",y_adc); sprintf(str,"ADC_Y:%04d",y_adc);
LCD_ShowString(100, 16, str, 16, RED, GRAY); LCD_ShowString(100, 16, str, 16, RED, GRAY);
//特效,半径开始收缩
if(HAL_GetTick()>ms100) if(HAL_GetTick()>ms100)
{ {
ms100=HAL_GetTick()+100; ms100=HAL_GetTick()+100;
if(r>0){r--;} if(r>0){r--;}
} }
} }
//步骤0,将点画在(30,30)此时半径为10
if(step==0) if(step==0)
{ {
TP_DrwaTrage(30,30,r); TP_DrwaTrage(30,30,r);
if(r==0) if(r==0)//当半径收缩为0的时候
{ {
//进入下一个步骤,缓存这个点的值,显示出来
step+=1; step+=1;
y1=y_adc; y1=y_adc;
x1=x_adc; x1=x_adc;
@@ -200,6 +204,7 @@ void TP_adjustment()
LCD_ShowString(0,66,str,16,RED,RED); LCD_ShowString(0,66,str,16,RED,RED);
} }
} }
//步骤1,等待屏幕被松开,进入下一个步骤,重置半径
if(step==1) if(step==1)
{ {
if(TPEN==1) if(TPEN==1)
@@ -209,6 +214,7 @@ void TP_adjustment()
} }
} }
//下面几个步骤和上面一样
if(step==2) if(step==2)
{ {
TP_DrwaTrage(290,30,r); TP_DrwaTrage(290,30,r);
@@ -271,41 +277,49 @@ void TP_adjustment()
r=10; r=10;
} }
} }
//当4个点读取完,开始计算关系
if(step==8) if(step==8)
{ {
//其实只需要两个点就能校准,通过取平均值获得xy的长边和短边
xd=((x1+x3)/2); xd=((x1+x3)/2);
xl=((x2+x4)/2); xl=((x2+x4)/2);
yd=((y1+y2)/2); yd=((y1+y2)/2);
yl=((y3+y4)/2); yl=((y3+y4)/2);
//长边减去短边可以再获得一个点
x5=xl-xd; x5=xl-xd;
y5=yl-yd; y5=yl-yd;
//这个点如果是负数,肯定有错,可能是xy搞反了
if(x5<0||y5<0) if(x5<0||y5<0)
{ {
//显示error
sprintf(str,"ERROR"); sprintf(str,"ERROR");
LCD_ShowString(0,66+16+16+16+16,str,16,RED, GRAY); LCD_ShowString(0,66+16+16+16+16,str,16,RED, GRAY);
}else }else
{ {
//计算关系倍率
//ad的长边减去短边再除去实际屏幕像素的长边减短边(260=320-30-30180=240-30-30
acc_x=x5/260.0; acc_x=x5/260.0;
acc_y=y5/180.0; acc_y=y5/180.0;
//验证倍率,将实际值减去验证值就等于误差值,因为有两个点,所以计算了两个误差后求了平均值
offset_x=(((xd/acc_x)-30)+((xl/acc_x)-290))/2; offset_x=(((xd/acc_x)-30)+((xl/acc_x)-290))/2;
offset_y=(((yd/acc_y)-30)+((yl/acc_y)-210))/2; offset_y=(((yd/acc_y)-30)+((yl/acc_y)-210))/2;
//保存计算结果
tconfig.x_acc=acc_x; tconfig.x_acc=acc_x;
tconfig.x_offset=offset_x; tconfig.x_offset=offset_x;
tconfig.y_acc=acc_y; tconfig.y_acc=acc_y;
tconfig.y_offset=offset_y; tconfig.y_offset=offset_y;
//eeprom块标记
tconfig.begin=0xab; tconfig.begin=0xab;
tconfig.end=0xcd; tconfig.end=0xcd;
//显示计算结果
sprintf(str,"x_acc=%f y_acc=%f",acc_x,acc_y); sprintf(str,"x_acc=%f y_acc=%f",acc_x,acc_y);
LCD_ShowString(0,66+16+16+16+16,str,16,RED,RED); LCD_ShowString(0,66+16+16+16+16,str,16,RED,RED);
sprintf(str,"x_offset=%d y_offset=%d",offset_x,offset_y); sprintf(str,"x_offset=%d y_offset=%d",offset_x,offset_y);
LCD_ShowString(0,66+16+16+16+16+16,str,16,RED,RED); LCD_ShowString(0,66+16+16+16+16+16,str,16,RED,RED);
} }
//将结果保存起来
EEPROM_WRITE_BATY(16,(char *)&tconfig,sizeof(touch_config)); EEPROM_WRITE_BATY(16,(char *)&tconfig,sizeof(touch_config));
HAL_Delay(1000); HAL_Delay(1000);
return; return;