In this application I use touch panel to control 2 LED.
First you can view this post to configure Pwm output to smt32-f7 disco board.
Connect LED_1 to PA_8 and LED_2 to PA_15 pin.
After generate the code add this code to Src/main.c:
#include <string.h>
#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_uart.h"
#include "stm32f7xx_hal_sdram.h"
#include "stm32f7xx_hal_ltdc.h"
#include "stm32746g_discovery.h"
#include "stm32746g_discovery_ts.h"
#include "stm32746g_discovery_lcd.h"
#include "stm32746g_discovery_sdram.h"
#include "stm32f7xx_ll_fmc.h"
#include "stdio.h"
#include <stdbool.h>
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_TIM1_Init(void);
static void MX_TIM2_Init(void);
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
TS_StateTypeDef TS_State;
void led1(float duty){
TIM1->CCR1=(int)(duty*65535.0);
}
void led2(float duty){
TIM2->CCR1=(int)(duty*65535.0);
}
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
MX_TIM2_Init();
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
BSP_LCD_Init();
BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS+(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()*4));
BSP_LCD_DisplayOn();
BSP_LCD_SelectLayer(0);
BSP_LCD_Clear(LCD_COLOR_BLACK);
BSP_LCD_SelectLayer(1);
BSP_LCD_Clear(LCD_COLOR_BLACK);
BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
if( BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize())!=TS_OK){
Error_Handler();
}
bool state_init=false;
char text[15];
int count=0;
uint16_t x[2],y[2],last_y[2];
uint16_t get_y,pre_y=0;
x[0]=50;
x[1]=200;
y[0]=220;
y[1]=220;
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_FillRect(x[0],y[0],20,10);
BSP_LCD_FillRect(x[1],y[1],20,10);
BSP_LCD_DrawRect(x[0],40,20,190);
BSP_LCD_DrawRect(x[1],40,20,190);
BSP_LCD_DisplayStringAt(0, LINE(10), (uint8_t *)" YELLOW RED", LEFT_MODE);
while (1)
{
BSP_TS_GetState(&TS_State);
led1(((float)(220-y[0])/180.0));
led2(((float)(220-y[1])/180.0));
count++;
if (TS_State.touchDetected) {
if(count>20){
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_FillRect(x[0],y[0],20,10);
BSP_LCD_FillRect(x[1],y[1],20,10);
BSP_LCD_DrawRect(x[0],40,20,190);
BSP_LCD_DrawRect(x[1],40,20,190);
BSP_LCD_DisplayStringAt(0, LINE(10), (uint8_t *)" YELLOW RED", LEFT_MODE);
count=0;
}
get_y = TS_State.touchY[0];
if(state_init==false){
state_init=true;
pre_y=get_y;
}
int16_t d_y=get_y-pre_y;
pre_y=get_y;
if(state_init==true){
memset(text,'\0',sizeof(text));
if( TS_State.touchX[0]>x[0]-30&&TS_State.touchX[0]<x[0]+50){
if(d_y>1){
y[0]+=2;
last_y[0]=y[0];
}
else if(d_y<-1){
y[0]-=2;
last_y[0]=y[0];
}
else if (d_y>=-1||d_y<=1){
y[0]=last_y[0];
}
if(y[0]<=40){
y[0]=40;
}
else if(y[0]>220){
y[0]=220;
}
}
if( TS_State.touchX[0]>x[1]-50&&TS_State.touchX[0]<x[1]+50){
if(d_y>1){
y[1]+=2;
last_y[1]=y[1];
}
else if(d_y<-1){
y[1]-=2;
last_y[1]=y[1];
}
else if (d_y>=-1||d_y<=1){
y[1]=last_y[1];
}
if(y[1]<=40){
y[1]=40;
}
else if(y[1]>220){
y[1]=220;
}
}
}
}
else{
state_init=false;
}
}
}
First you can view this post to configure Pwm output to smt32-f7 disco board.
Connect LED_1 to PA_8 and LED_2 to PA_15 pin.
![]() |
Stm32-f7-Disco touch panel application |
#include <string.h>
#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_uart.h"
#include "stm32f7xx_hal_sdram.h"
#include "stm32f7xx_hal_ltdc.h"
#include "stm32746g_discovery.h"
#include "stm32746g_discovery_ts.h"
#include "stm32746g_discovery_lcd.h"
#include "stm32746g_discovery_sdram.h"
#include "stm32f7xx_ll_fmc.h"
#include "stdio.h"
#include <stdbool.h>
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_TIM1_Init(void);
static void MX_TIM2_Init(void);
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
TS_StateTypeDef TS_State;
void led1(float duty){
TIM1->CCR1=(int)(duty*65535.0);
}
void led2(float duty){
TIM2->CCR1=(int)(duty*65535.0);
}
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM1_Init();
MX_TIM2_Init();
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
BSP_LCD_Init();
BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS+(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()*4));
BSP_LCD_DisplayOn();
BSP_LCD_SelectLayer(0);
BSP_LCD_Clear(LCD_COLOR_BLACK);
BSP_LCD_SelectLayer(1);
BSP_LCD_Clear(LCD_COLOR_BLACK);
BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
if( BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize())!=TS_OK){
Error_Handler();
}
bool state_init=false;
char text[15];
int count=0;
uint16_t x[2],y[2],last_y[2];
uint16_t get_y,pre_y=0;
x[0]=50;
x[1]=200;
y[0]=220;
y[1]=220;
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_FillRect(x[0],y[0],20,10);
BSP_LCD_FillRect(x[1],y[1],20,10);
BSP_LCD_DrawRect(x[0],40,20,190);
BSP_LCD_DrawRect(x[1],40,20,190);
BSP_LCD_DisplayStringAt(0, LINE(10), (uint8_t *)" YELLOW RED", LEFT_MODE);
while (1)
{
BSP_TS_GetState(&TS_State);
led1(((float)(220-y[0])/180.0));
led2(((float)(220-y[1])/180.0));
count++;
if (TS_State.touchDetected) {
if(count>20){
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_FillRect(x[0],y[0],20,10);
BSP_LCD_FillRect(x[1],y[1],20,10);
BSP_LCD_DrawRect(x[0],40,20,190);
BSP_LCD_DrawRect(x[1],40,20,190);
BSP_LCD_DisplayStringAt(0, LINE(10), (uint8_t *)" YELLOW RED", LEFT_MODE);
count=0;
}
get_y = TS_State.touchY[0];
if(state_init==false){
state_init=true;
pre_y=get_y;
}
int16_t d_y=get_y-pre_y;
pre_y=get_y;
if(state_init==true){
memset(text,'\0',sizeof(text));
if( TS_State.touchX[0]>x[0]-30&&TS_State.touchX[0]<x[0]+50){
if(d_y>1){
y[0]+=2;
last_y[0]=y[0];
}
else if(d_y<-1){
y[0]-=2;
last_y[0]=y[0];
}
else if (d_y>=-1||d_y<=1){
y[0]=last_y[0];
}
if(y[0]<=40){
y[0]=40;
}
else if(y[0]>220){
y[0]=220;
}
}
if( TS_State.touchX[0]>x[1]-50&&TS_State.touchX[0]<x[1]+50){
if(d_y>1){
y[1]+=2;
last_y[1]=y[1];
}
else if(d_y<-1){
y[1]-=2;
last_y[1]=y[1];
}
else if (d_y>=-1||d_y<=1){
y[1]=last_y[1];
}
if(y[1]<=40){
y[1]=40;
}
else if(y[1]>220){
y[1]=220;
}
}
}
}
else{
state_init=false;
}
}
}
what is the compiler that you use in this program?
Trả lờiXóaI use gcc arm toolchain to compile code.
Xóabạn ơi cho mình hỏi tại sao khi copy mấy dòng include khi build lại báo lỗi vậy bạn...giúp mình với.
Trả lờiXóaem email vào ngobachduong@gmail.com nhé
XóaMình dùng gcc arm toolchain trong hệ điều hành linux để compile. Trong file Makefile cần thêm đường link dẫn đến file thư viện có sẵn.vd
XóaINCLUDEDIRS += /home/ngoduong/stm32f7/Drivers/HAL/Inc
.....
LIBSOURCES += $(wildcard /home/ngoduong/stm32f7/Drivers/HAL/Src/*.c)
......
a có thể chỉ e làm với được không e làm hoài mà vẫn báo lỗi. a có thể teamwiew hướng dẫn e với
Xóaa có thể làm một video hướng dẫn cách built project khi add thêm thư viện bsp được không a, e dùng keil c làm không được anh ơi
Trả lờiXóa