/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 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 "gpio.h"
#include "lcd.h"
#include "stdio.h"
#include "string.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void led_action(void);
void lcd_action(void);
void led_key(void);
void pwm_action(void);
void run_wei(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
unsigned char key_number=0;
unsigned char key_caton=0;
unsigned char timer0=0,timer1=0,timer2=0,timer3=0;
unsigned char key_time=0,door_late_time=0,door_time=0,power_time=0,key_down=0;
int led=0;
unsigned char led1,led2,led3,led4;
uint8_t view=1;
int colk_timer[4]={0,55,50,12};
char time_lcd[30];
int now_space=1;
char space_lcd[30];
int active=0;
unsigned char active_time=0,active_flag=0;;
unsigned char set_led=0,way=2;
/* 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_TIM1_Init();
MX_TIM3_Init();
MX_TIM16_Init();
MX_TIM17_Init();
LCD_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
HAL_TIM_Base_Start_IT(&htim1);
HAL_TIM_Base_Start_IT(&htim3);
HAL_TIM_PWM_Start(&htim16,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim17,TIM_CHANNEL_1);
/* Infinite loop */
LCD_Clear(Black);
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
pwm_action();
led_action();
lcd_action();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance==TIM1)
{
switch(key_number)
{
case 0:
if(!HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)||!HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)||!HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)||!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
{
key_number=9;
key_down=1;
timer0=0;
}
break;
case 9:
if(!HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0))
{
key_number=1;
key_caton=1;
}
else if(!HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1))
{
key_number=2;
key_caton=1;
}
else if(!HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2))
{
key_number=3;
key_caton=1;
}
else if(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
{
key_number=4;
key_caton=1;
}
else
{
key_number=0;
}
break;
case 1:
if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0))
{
key_number=0;
}
break;
case 2:
if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1))
{
key_number=0;
}
break;
case 3:
if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2))
{
key_number=0;
}
break;
case 4:
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
{
key_number=0;
}
break;
default:break;
}
switch(key_number)
{
case 1:
if(key_caton)
{
set_led=1;
if(now_space==1);
else
led1=1;
}
break;
case 2:
if(key_caton)
{
set_led=2;
if(now_space==2);
else
led2=1;
}
break;
case 3:
if(key_caton)
{
set_led=3;
if(now_space==3);
else
led3=1;
}
break;
case 4:
if(key_caton)
{
set_led=4;
if(now_space==4);
else
led4=1;
}
break;
default:break;
}
}
if(htim->Instance==TIM3)
{
if(way==0)//向上
{
led--;
if(led<=0)
{
led=100;
}
}
else if(way==1)
{
led++;
if(led>=100)
{
led=0;
}
}
else if(way==2)
led=0;
if(active_flag)
{
if(active_time<10)
{
active_time++;
}
else
{
active_time=0;
active=1;
}
}
if(key_down)
{
if(timer0<10)
{
timer0++;
}
else
{
key_time=1;
timer0=0;
}
}
if(timer1<20)
{
timer1++;
}
else
{
timer1=0;
door_late_time=1;
}
if(timer2<60)
{
timer2++;
}
else
{
power_time=1;
timer2=0;
}
if(timer3<40)
{
timer3++;
}
else
{
door_time=1;
timer3=0;
}
colk_timer[0]++;
if(colk_timer[0]>=10)
{
colk_timer[0]=0;
colk_timer[1]++;
if(colk_timer[1]>=60)
{
colk_timer[1]=0;
colk_timer[2]++;
if(colk_timer[2]>=60)
{
colk_timer[2]=0;
colk_timer[3]++;
colk_timer[3]=colk_timer[3]%24;
}
}
}
}
}
void run_wei(void)
{
switch(now_space)
{
case 1:
way=2;
break;
case 2:
if(led1)
way=1;
break;
case 3:
if(led2)
way=1;
if(led1)
way=1;
break;
case 4:
if(led3)
way=1;
if(led2)
way=1;
if(led1)
way=1;
break;
default:break;
}
switch(now_space)
{
case 1:
if(led2)
way=0;
if(led3)
way=0;
if(led4)
way=0;
break;
case 2:
if(led3)
way=0;
if(led4)
way=0;
break;
case 3:
if(led4)
way=0;
break;
case 4:
way=2;
break;
default:break;
}
}
void pwm_action(void)
{
if(key_time)
{
__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,50);
run_wei();
}
__HAL_TIM_SetCompare(&htim16,TIM_CHANNEL_1,50);
}
void led_action(void)
{
switch(led)
{
case 0:
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11, GPIO_PIN_SET);
led_key();
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
break;
case 10:
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_SET);
led_key();
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
break;
case 20:
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
led_key();
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
break;
case 30:
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, GPIO_PIN_SET);
led_key();
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
break;
case 40:
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_SET);
led_key();
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
break;
case 70:
if(way==0)
{
now_space++;
}
else if(way==1)
{
now_space--;
}
else ;
break;
default:break;
}
// led++;
// led=led%1000;
// led--;
// if(led<0)
// {
// led=1000;
// }
}
void led_key(void)
{
// switch (set_led)
// {
// case 1:
// led1=1;
// break;
// case 2:
// led2=1;
// break;
// case 3:
// led3=1;
// break;
// case 4:
// led4=1;
// break;
// default:break;
// }
if(led1)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11, GPIO_PIN_RESET);
}
if(led2)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_RESET);
}
if(led3)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
}
if(led4)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
}
}
void lcd_action(void)
{
if(view)
{
sprintf(time_lcd," %02d:%02d:%02d ",colk_timer[3],colk_timer[2],colk_timer[1]);
sprintf(space_lcd," %d ",now_space);
LCD_SetTextColor(White);
LCD_SetBackColor(Black);
LCD_DisplayStringLine(Line1, (u8 *)" ");
LCD_DisplayStringLine(Line2, (u8 *)" ");
LCD_DisplayStringLine(Line3, (u8 *)" now postion ");
LCD_DisplayStringLine(Line4, (u8 *)" ");
if(active_time==0)
LCD_DisplayStringLine(Line5, (u8 *)space_lcd);
else if(active_time==2)
LCD_DisplayStringLine(Line5, (u8 *)" ");
else if(active_time==4)
LCD_DisplayStringLine(Line5, (u8 *)space_lcd);
else if(active_time==6)
LCD_DisplayStringLine(Line5, (u8 *)" ");
else if(active_time==8)
LCD_DisplayStringLine(Line5, (u8 *)space_lcd);
LCD_DisplayStringLine(Line4, (u8 *)" ");
LCD_DisplayStringLine(Line6, (u8 *)" ");
LCD_DisplayStringLine(Line7, (u8 *)time_lcd);
LCD_DisplayStringLine(Line8, (u8 *)" ");
LCD_DisplayStringLine(Line9, (u8 *)" ");
}
}
/**
* @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_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
RCC_OscInitStruct.PLL.PLLN = 20;
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_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* 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 */
qq3212756914