/**
******************************************************************************
* 文件名程: main.c
* 作 者: 硬石嵌入式开发团队
* 版 本: V1.0
* 编写日期: 2015-10-04
* 功 能: 触摸屏使用实现
******************************************************************************
* 说明:
* 本例程配套硬石stm32开发板YS-F1Pro使用。
*
* 淘宝:
* 论坛:http://www.ing10bbs.com
* 版权归硬石嵌入式开发团队所有,请勿商用。
******************************************************************************
*/
/* 包含头文件 ----------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "usart\bsp_debug_usart.h"
#include "lcd\bsp_lcd.h"
#include "spiflash\bsp_spiflash.h"
#include "touch/bsp_touch.h"
#include "stdlib.h"
#include "key/bsp_key.h"
/* 私有类型定义 --------------------------------------------------------------*/
/* 私有宏定义 ----------------------------------------------------------------*/
/* 私有变量 ------------------------------------------------------------------*/
uint8_t screen_flag=1;
/* 扩展变量 ------------------------------------------------------------------*/
/* 私有函数原形 --------------------------------------------------------------*/
/* 函数体 --------------------------------------------------------------------*/
/**
* 函数功能: 系统时钟配置
* 输入参数: 无
* 返 回 值: 无
* 说 明: 无
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; // 外部晶振,8MHz
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // 9倍频,得到72MHz主时钟
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 系统时钟:72MHz
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // AHB时钟:72MHz
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // APB1时钟:36MHz
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // APB2时钟:72MHz
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
// HAL_RCC_GetHCLKFreq()/1000 1ms中断一次
// HAL_RCC_GetHCLKFreq()/100000 10us中断一次
// HAL_RCC_GetHCLKFreq()/1000000 1us中断一次
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); // 配置并启动系统滴答定时器
/* 系统滴答定时器时钟源 */
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* 系统滴答定时器中断优先级配置 */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/**
* 函数功能: 画板初始化,用于取色用
* 输入参数: 无
* 返 回 值: 无
* 说 明: 无
*/
void Palette_Init(void)
{
if(screen_flag==1)
{
/* 整屏清为白色 */
LCD_Clear( 0, 0,320, 480,WHITE );
LCD_Clear( 0,440, 40, 40, BLACK );
LCD_DispString_EN(3,450,"CL",BLACK,RED,USB_FONT_24);
LCD_Clear( 40,440, 40, 40, GREEN );
LCD_Clear( 80,440, 40, 40, BLUE );
LCD_Clear(120,440, 40, 40, MAGENTA );
LCD_Clear(160,440, 40, 40, ORANGE );
LCD_Clear(200,440, 40, 40, CYAN );
LCD_Clear(240,440, 40, 40, BLACK );
LCD_Clear(280,440, 40, 40, RED );
}
else if(screen_flag==2)
{
/* 整屏清为白色 */
LCD_Clear( 0, 0,480, 320,WHITE);
LCD_Clear( 0,280, 60, 40, BLACK);
LCD_DispString_EN(3,290,"CL",BLACK,RED,USB_FONT_24);
LCD_Clear( 60,280, 60, 40, GREEN );
LCD_Clear(120,280, 60, 40, BLUE );
LCD_Clear(180,280, 60, 40, MAGENTA );
LCD_Clear(240,280, 60, 40, ORANGE );
LCD_Clear(300,280, 60, 40, CYAN );
LCD_Clear(360,280, 60, 40, BLACK );
LCD_Clear(420,280, 60, 40, RED );
}
}
/**
* 函数功能: 主函数.
* 输入参数: 无
* 返 回 值: 无
* 说 明: 无
*/
int main(void)
{
uint32_t lcdid;
uint16_t x,y;
uint8_t i;
uint16_t color=BLUE;
/* 复位所有外设,初始化Flash接口和系统滴答定时器 */
HAL_Init();
/* 配置系统时钟 */
SystemClock_Config();
/* 初始化3.5寸TFT液晶模组,一般优先于调试串口初始化 */
lcdid=BSP_LCD_Init();
KEY_GPIO_Init();
/* 初始化串口并配置串口中断优先级 */
MX_DEBUG_USART_Init();
MX_SPIFlash_Init();
Touch_Init_GPIO();
/* 调用格式化输出函数打印输出数据 */
printf("LCD ID=0x%08X\n",lcdid);
printf("触摸屏校准测试\n");
LCD_Clear(0,0,LCD_DEFAULT_WIDTH,LCD_DEFAULT_HEIGTH,BLACK);
/* 开背光 */
LCD_BK_ON();
#if 0 /* 1:重新校准,0:读取原有数据进行校准 */
/* 擦除存储触摸校准值数据,重新进行校准 */
SPI_FLASH_SectorErase(XPT2046_SPIFLASH_ADDR);
#endif
SPI_FLASH_BufferRead((uint8_t *)&cal_value,XPT2046_SPIFLASH_ADDR,sizeof(XPT2046_Calibration));
printf("cal_flag: 0x%X\n",cal_value.cal_flag);
for(i=0;i<5;++i)
{
printf("%d lcd_x=%d lcd_y=%d touch_x=%d touch_y=%d\n",i,cal_value.lcd_x[i],cal_value.lcd_y[i],cal_value.touch_x[i],cal_value.touch_y[i]);
}
for(i=0;i<7;++i)
{
printf("adjust[%d]=%d ",i,cal_value.adjust[i]);
}
printf("\n");
if(cal_value.cal_flag!=0xAA55)
{
/* 等待触摸屏校正完毕 */
while(XPT2046_Touch_Calibrate() !=0);
}
else
{
LCD_Clear(0, 0,LCD_DEFAULT_WIDTH, LCD_DEFAULT_HEIGTH, BACKGROUND);
}
Palette_Init();
/* 无限循环 */
while (1)
{ \
if(KEY1_StateRead()==KEY_DOWN)
{
if(screen_flag==1)
{
screen_flag=2;
}
else if(screen_flag==2)
{
screen_flag=1;
}
HAL_Delay(100);
LCD_SetDirection(screen_flag);
Palette_Init();
}
if(XPT2046_EXTI_Read()==XPT2046_EXTI_ActiveLevel) /*如果触笔按下了*/
{
if(screen_flag==1)
{
/*获取点的坐标*/
XPT2046_Get_TouchedPoint(&x,&y);
if((x==0xFFFF)||(y==0xFFFF)) continue;
if(y<440)
{
Palette_draw_point(x,y,color);
}
else if(y<480)
{
if(x>280)
color=RED;
else if(x>240)
color=BLACK;
else if(x>200)
color=CYAN;
else if(x>160)
color=ORANGE;
else if(x>120)
color=MAGENTA;
else if(x>80)
color=BLUE;
else if(x>40)
color=GREEN;
else
{
color=BLUE;
LCD_Clear(0,0,320,440,WHITE);
}
}
}
else if(screen_flag==2)
{
/*获取点的坐标*/
XPT2046_Get_TouchedPoint(&y,&x);
if((x==0xFFFF)||(y==0xFFFF)) continue;
y=320-y;
if(y<280)
{
Palette_draw_point(x,y,color);
}
else if(y<320)
{
if(x>420)
color=RED;
else if(x>360)
color=BLACK;
else if(x>300)
color=CYAN;
else if(x>240)
color=ORANGE;
else if(x>180)
color=MAGENTA;
else if(x>120)
color=BLUE;
else if(x>60)
color=GREEN;
else
{
color=BLUE;
LCD_Clear(0,0,480,280,WHITE);
}
}
}
}
HAL_Delay(10);
}
}
/******************* (C) COPYRIGHT 2015-2020 硬石嵌入式开发团队 *****END OF FILE****/