关于STM32f1系列实现LCD的触摸功能——基于HAL库
触摸功能一共有两种实现方式:轮询方式和中断方式
先使用的轮询方式,测试后没有问题:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_FSMC_Init();
MX_TIM7_Init();
MX_I2C2_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
LCD_ShowString(10,0,240,16,16,(uint8_t*)"Demo20_1:Touch-Res and GUI");
//====1. 读取保存在EEPROM中的电阻触摸屏参数
EP24C_ReadBytes(TOUCH_PARA_ADDR, &TouchPara.isSaved, sizeof(TouchPara));
if (TouchPara.isSaved ==TOUCH_PARA_SAVED)
LCD_ShowString(10,80,240,16,16,(uint8_t*)"Touch-Res has been calibrated");
else
LCD_ShowString(10,80,240,16,16,(uint8_t*)"Touch-Res has not been calibrated");
//===2.文字菜单
LCD_ShowString(10,20,240,16,16, (uint8_t*)"[1]Key1 = Show parameters");
LCD_ShowString(10,40,240,16,16, (uint8_t*)"[2]Key2= To calibrate");
LCD_ShowString(10,60, 240,16,16,(uint8_t*)"[3]Key3= Enter GUI screen");
uint16_t InfoStartPosY=80;
uint8_t EnterGUI=0;
KEYS curKey;
while(1)
{
curKey=ScanKeys(KEY_WAIT_ALWAYS);
LCD_Fill(0, InfoStartPosY,240, 320, WHITE); //清除信息显示区
switch(curKey)
{
case KEY1:
{
// 检测触摸参数是否保存,如果保存了就直接显示在LCD上
if (TouchPara.isSaved == TOUCH_PARA_SAVED)
ShowTouchPara();
// 没有保存提示触摸分辨率尚未校准
else
LCD_ShowString(10,80,240,16,16,(uint8_t*)"Touch-Res has not been calibrated");
break;
}
case KEY2:{
TouchCalibrate(); //触摸屏校准,会清屏,然后进入GUI
EnterGUI=1;
break;
}
case KEY3:
EnterGUI=1;
}//end switch
HAL_Delay(300);
if (EnterGUI)
break; //退出while(1)循环,进入GUI
}//end while(1)
Partition(); // 画出数字显示
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
Touch_Point();
HAL_Delay(300); //消除抖动影响,触摸操作也有抖动
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}//end while()
然后想修改为中断方式,将LCD的输入引脚改为中断方式,然后使用中断就出问题了:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_FSMC_Init();
MX_TIM7_Init();
MX_I2C2_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
LCD_ShowString(10,0,240,16,16,(uint8_t*)"Demo20_1:Touch-Res and GUI");
HAL_NVIC_DisableIRQ(EXTI15_10_IRQn ); // 先关闭中断
//====1. 读取保存在EEPROM中的电阻触摸屏参数
EP24C_ReadBytes(TOUCH_PARA_ADDR, &TouchPara.isSaved, sizeof(TouchPara));
if (TouchPara.isSaved ==TOUCH_PARA_SAVED)
LCD_ShowString(10,80,240,16,16,(uint8_t*)"Touch-Res has been calibrated");
else
LCD_ShowString(10,80,240,16,16,(uint8_t*)"Touch-Res has not been calibrated");
//===2.文字菜单
LCD_ShowString(10,20,240,16,16, (uint8_t*)"[1]Key1 = Show parameters");
LCD_ShowString(10,40,240,16,16, (uint8_t*)"[2]Key2= To calibrate");
LCD_ShowString(10,60, 240,16,16,(uint8_t*)"[3]Key3= Enter GUI screen");
uint16_t InfoStartPosY=80;
uint8_t EnterGUI=0;
KEYS curKey;
while(1)
{
curKey=ScanKeys(KEY_WAIT_ALWAYS);
LCD_Fill(0, InfoStartPosY,240, 320, WHITE); //清除信息显示区
switch(curKey)
{
case KEY1:
{
// 检测触摸参数是否保存,如果保存了就直接显示在LCD上
if (TouchPara.isSaved == TOUCH_PARA_SAVED)
ShowTouchPara();
// 没有保存提示触摸分辨率尚未校准
else
LCD_ShowString(10,80,240,16,16,(uint8_t*)"Touch-Res has not been calibrated");
break;
}
case KEY2:{
TouchCalibrate(); //触摸屏校准,会清屏,然后进入GUI
EnterGUI=1;
break;
}
case KEY3:
EnterGUI=1;
}//end switch
HAL_Delay(300);
if (EnterGUI)
break; //退出while(1)循环,进入GUI
}//end while(1)
Partition(); // 画出数字显示
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn ); // 打开中断
while (1)
{
// Touch_Point();
// HAL_Delay(300); //消除抖动影响,触摸操作也有抖动
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}//end while()
/* USER CODE END 3 */
}
// 中断回调函数
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
HAL_Delay(20);
TOUCH_ScanAfterINT();
Touch_Point();
HAL_Delay(200);
}
修改后出现各种问题,前面的第一个while循环有时都进不去,点击复位键直接黑屏等问题
这是什么原因导致的?