/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lcd_hal.h"
#include "stdio.h"
#define Freq 1000
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
void delay(uint16_t time)
{
uint16_t i=0;
while(time--)
{
i = 32000;
while(i--);
}
}
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
char mima[3]={'@','@','@'},str[20];
uint8_t yemian = 0;
char recieve_buffer[10] = {0,0,0,0,1,2,3};
uint16_t value[2];
uint8_t N,cuowu;
float f;
uint8_t x;
uint16_t all_mima = 123,all_mima_last,mm,qiuhe;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
void mima_xiugai(void)
{
HAL_UART_Receive(&huart1,(uint8_t*)recieve_buffer,7,100);
if(recieve_buffer[3] == 45)
{
all_mima_last = all_mima;
mm = (((recieve_buffer[0] - 48) * 100) + ((recieve_buffer[1] - 48) * 10) + recieve_buffer[2] - 48);
if(mm == all_mima_last)
{
all_mima = (((recieve_buffer[4] - 48) * 100) + ((recieve_buffer[5] - 48) * 10) + recieve_buffer[6] - 48);
}
}
}
void led_init(void)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
void mima_mode(void)
{
LCD_DisplayStringLine(Line0,(uint8_t *)" PSD ");
if(mima[0] == '@')
{
sprintf(str," B1 = %c ",mima[0]);
LCD_DisplayStringLine(Line1,(uint8_t*)str);
}
else
{
sprintf(str," B1 = %d ",mima[0]);
LCD_DisplayStringLine(Line1,(uint8_t*)str);
}
if(mima[1] == '@')
{
sprintf(str," B2 = %c ",mima[1]);
LCD_DisplayStringLine(Line2,(uint8_t*)str);
}
else
{
sprintf(str," B2 = %d ",mima[1]);
LCD_DisplayStringLine(Line2,(uint8_t*)str);
}
if(mima[2] == '@')
{
sprintf(str," B3 = %c ",mima[2]);
LCD_DisplayStringLine(Line3,(uint8_t*)str);
}
else
{
sprintf(str," B3 = %d ",mima[2]);
LCD_DisplayStringLine(Line3,(uint8_t*)str);
}
}
void shuchu(void)
{
uint8_t duty = 10;
LCD_DisplayStringLine(Line0,(uint8_t *)" STA ");
sprintf(str," F : %.0fHz",Freq / 0.5);
LCD_DisplayStringLine(Line1,(uint8_t*)str);
sprintf(str," D : %d ",duty);
LCD_DisplayStringLine(Line2,(uint8_t*)str);
LCD_DisplayStringLine(Line3,(uint8_t *)" ");
yemian = 0;
mima[0] = '@';
mima[1] = '@';
mima[2] = '@';
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
HAL_Delay(5000);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
void key_B4(void)
{
if(HAL_GPIO_ReadPin(GPIOA,B4_Pin) == 0)
{
delay(100);
if(HAL_GPIO_ReadPin(GPIOA,B4_Pin) == 0)
{
qiuhe = (mima[0] * 100) + (mima[1] * 10) + mima[2];
if(all_mima == qiuhe)
{
yemian = 1;
mima[0] = '@';
mima[1] = '@';
mima[2] = '@';
}
else
{
yemian = 0;
mima[0] = '@';
mima[1] = '@';
mima[2] = '@';
cuowu += 1;
}
}
}
}
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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_TIM2_Init();
MX_USART1_UART_Init();
MX_TIM3_Init();
MX_TIM4_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim4);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
// __HAL_TIM_SET_COUNTER(&htim2,Freq);
// __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,0.1 * Freq);
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
led_init();
HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
mima_xiugai();
key_B4();
if(yemian)
{
__HAL_TIM_SET_COUNTER(&htim2,Freq * 0.5);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,Freq * 0.05);
shuchu();
}
else
{
mima_mode();
__HAL_TIM_SET_COUNTER(&htim2,Freq);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,0.05 * Freq);
}
// sprintf(str," qiuhe : %d ",qiuhe);
// LCD_DisplayStringLine(Line5,(uint8_t*)str);
// sprintf(str," mima : %d ",all_mima);
// LCD_DisplayStringLine(Line4,(uint8_t*)str);
sprintf(str," cuowu : %d ",cuowu);
LCD_DisplayStringLine(Line6,(uint8_t*)str);
// HAL_UART_Transmit(&huart1,(uint8_t*)"123",3,100);
// HAL_Delay(1000);
sprintf(str," DATA : %c%c%c%c%c%c%c ",recieve_buffer[0],recieve_buffer[1],recieve_buffer[2],recieve_buffer[3],recieve_buffer[4],recieve_buffer[5],recieve_buffer[6]);
LCD_DisplayStringLine(Line8,(uint8_t*)str);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
RCC_OscInitStruct.PLL.PLLN = 75;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == B1_Pin)
{
if(HAL_GPIO_ReadPin(GPIOB,B1_Pin) == 0)
{
delay(150);
if(HAL_GPIO_ReadPin(GPIOB,B1_Pin) == 0 && mima[0] != 0 && mima[0] == 64)
{
mima[0] = 0;
}
if(mima[0] < 9)
{
mima[0] += 1;
}
else if(mima[0] >= 9 && mima[0] < 64)
{
mima[0] = 0;
}
}
__HAL_GPIO_EXTI_CLEAR_IT(B1_Pin);
}
if(GPIO_Pin == B2_Pin)
{
if(HAL_GPIO_ReadPin(GPIOB,B2_Pin) == 0)
{
delay(150);
if(HAL_GPIO_ReadPin(GPIOB,B2_Pin) == 0 && mima[1] != 0 && mima[1] == 64 )
{
mima[1] = 0;
}
if(mima[1] < 9)
{
mima[1] += 1;
}
else if(mima[1] >= 9 && mima[1] < 64)
{
mima[1] = 0;
}
}
__HAL_GPIO_EXTI_CLEAR_IT(B2_Pin);
}
if(GPIO_Pin == B3_Pin)
{
if(HAL_GPIO_ReadPin(GPIOB,B3_Pin) == 0)
{
delay(150);
if(HAL_GPIO_ReadPin(GPIOB,B3_Pin) == 0 && mima[2] != 0 && mima[2] == 64 )
{
mima[2] = 0;
}
if(mima[2] < 9)
{
mima[2] += 1;
}
else if(mima[2] >= 9 && mima[2] < 64)
{
mima[2] = 0;
}
}
__HAL_GPIO_EXTI_CLEAR_IT(B3_Pin);
}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM3)
{
N++;
if(N == 1)
{
value[0] = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);
}
else if(N == 2)
{
value[1] = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);
}
f = value[1] - value[0];
f = 1000000 / f;
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(cuowu > 3)
{
if(htim->Instance == TIM4)
{
x++;
if(x > 0 && x <= 50)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
delay(20);
}
if(x > 50)
{
x = 0;
cuowu = 0;
}
}
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */