蓝桥杯嵌入式第十三届第二场真题代码分享——可能无缘国赛了

蓝桥杯嵌入式第十三届第二场真题代码分享——可能无缘国赛了,第1张

楼主这里个粗心的小bug,就是题目要求KEY3增加购买数量的时候,如果超出购买数量应该回到0,也就是
0-库存数-0,而楼主写成了0-库存-库存,没错,会直接锁死,呜呜呜,可能无源国赛了,各位友友努力吧。

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * 
© Copyright (c) 2021 STMicroelectronics. * All rights reserved.
* * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */
/* 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.h" #include "i2c_hal.h" #include /* 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); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ uint8_t str[21]; __IO uint32_t uwTick_Key_Speed; __IO uint32_t uwTick_LCD_Speed; __IO uint32_t uwTick_LED_Speed; uint8_t key_value; uint8_t key_down; uint8_t key_up; uint8_t key_old; uint8_t LCD_flag; //0:SHOP 1:PRICE 2:REP uint8_t PWM_50MS_Flag; uint8_t rx_buff[255]; uint8_t tx_buff[255]; int pwm_count; int X_num; int Y_num; float X_price = 1.0; float Y_price = 1.0; int X_sum = 10; int Y_sum = 10; uint8_t led_flag; void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if(rx_buff[0] == '?') { sprintf((char*)tx_buff,"X:%.1lf,Y:%.1lf\t",X_price,Y_price); HAL_UART_Transmit(&huart1,tx_buff,sizeof(rx_buff)/1,0xff); } HAL_UART_Receive_IT(&huart1,rx_buff,1); } void iic_write(uint8_t add,uint8_t reg,uint8_t data) { I2CStart(); I2CSendByte(add); I2CWaitAck(); I2CSendByte(reg); I2CWaitAck(); I2CSendByte(data); I2CWaitAck(); I2CStop(); } uint8_t iic_read(uint8_t add,uint8_t reg) { static uint8_t data; I2CStart(); I2CSendByte(add); I2CWaitAck(); I2CSendByte(reg); I2CWaitAck(); I2CStart(); I2CSendByte(add+1); I2CWaitAck(); data = I2CReceiveByte(); I2CWaitAck(); I2CStop(); return data; } void Save_price_sum(uint8_t id,uint8_t type,float price) { static uint8_t x_price,y_price; if(id == 1) { if(type == 1) { iic_write(0xa0,0,X_sum); } if(type == 2) { x_price = (int)(price*10); iic_write(0xa0,2,x_price); } } if(id == 2) { if(type == 1) { iic_write(0xa0,1,Y_sum); } if(type == 2) { y_price = (int)(price*10); iic_write(0xa0,3,y_price); } } } uint8_t Key_Scan() { static uint8_t value; if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0) == GPIO_PIN_RESET) value = 1; else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1) == GPIO_PIN_RESET) value = 2; else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2) == GPIO_PIN_RESET) value = 3; else if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == GPIO_PIN_RESET) value = 4; else value = 0; return value; } void Key_Proc() { if(uwTick - uwTick_Key_Speed < 100) return; else uwTick_Key_Speed = uwTick; key_value = Key_Scan(); key_down = key_value&(key_value ^ key_old); key_up = -key_value&(key_value ^ key_old); key_old = key_value; switch(key_down) { case 1: LCD_flag = LCD_flag + 1; if(LCD_flag == 3) { LCD_flag = 0; } break; case 2: if(LCD_flag == 0) //购买页面 { X_num++; if(X_num > X_sum) { X_num = X_sum; } } if(LCD_flag == 1) //价格页面 { X_price +=0.1; if(X_price > 2.05) { X_price = 1; } Save_price_sum(1,2,X_price); } if(LCD_flag == 2) { X_sum++; Save_price_sum(1,1,0); } break; case 3: if(LCD_flag == 0) //购买页面 { Y_num++; if(Y_num > Y_sum) { Y_num = Y_sum; } } if(LCD_flag == 1) //价格页面 { Y_price +=0.1; if(Y_price > 2.05) { Y_price = 1; } Save_price_sum(2,2,Y_price); } if(LCD_flag == 2) { Y_sum++; Save_price_sum(2,1,0); } break; case 4: if(LCD_flag == 0) { X_sum = X_sum - X_num; Y_sum = Y_sum - Y_num; Save_price_sum(1,1,0); Save_price_sum(2,1,0); PWM_50MS_Flag = 1; sprintf((char*)tx_buff,"X:%d,Y:%d,Z:%.1lf\t",X_num,Y_num,X_num*X_price + Y_num*Y_price); HAL_UART_Transmit(&huart1,tx_buff,sizeof(rx_buff)/1,0xff); X_num = 0; Y_num = 0; } break; } } void LED_Disp(uint8_t ucled) { HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11| GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOC,ucled << 8,GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET); } void Shop_Display() { sprintf((char *)str," SHOP "); LCD_DisplayStringLine(Line1,str); sprintf((char *)str," X:%d ",X_num); LCD_DisplayStringLine(Line3,str); sprintf((char *)str," Y:%d ",Y_num); LCD_DisplayStringLine(Line4,str); // sprintf((char *)str,"eeprom: "); // LCD_DisplayStringLine(Line5,str); // static uint8_t test_x_sum,test_y_sum; // test_x_sum = iic_read(0xa0,0); // test_y_sum = iic_read(0xa0,1); // sprintf((char *)str,"X_sum:%d ,Y_sum:%d ",test_x_sum,test_y_sum); // LCD_DisplayStringLine(Line6,str); } void Price_Display() { sprintf((char *)str," PRICE "); LCD_DisplayStringLine(Line1,str); sprintf((char *)str," X:%.1lf ",X_price); LCD_DisplayStringLine(Line3,str); sprintf((char *)str," Y:%.1lf ",Y_price); LCD_DisplayStringLine(Line4,str); } void REP_Display() { sprintf((char *)str," REP "); LCD_DisplayStringLine(Line1,str); sprintf((char *)str," X:%d ",X_sum); LCD_DisplayStringLine(Line3,str); sprintf((char *)str," Y:%d ",Y_sum); LCD_DisplayStringLine(Line4,str); } void LCD_Proc() { if(uwTick - uwTick_LCD_Speed < 100) return; else uwTick_LCD_Speed = uwTick; if(LCD_flag == 0) { Shop_Display(); } else if(LCD_flag == 1) { Price_Display(); } else if(LCD_flag == 2) { REP_Display(); } } void LED_Proc() { if(uwTick - uwTick_LED_Speed < 100) return; else uwTick_LED_Speed = uwTick; if(PWM_50MS_Flag == 1) { pwm_count++; TIM2->CCR2 = 150; LED_Disp(0x01); if(pwm_count > 50) { TIM2->CCR2 = 25; PWM_50MS_Flag = 0; LED_Disp(0x00); led_flag = 0; } } if(X_sum == 0 && Y_sum == 0) { if(led_flag == 0) { led_flag^=1; if(PWM_50MS_Flag == 1) { LED_Disp(0x03); } else LED_Disp(0x02); } else { led_flag^=1; if(PWM_50MS_Flag == 1) { LED_Disp(0x01); } else LED_Disp(0x00); } } else if(X_sum !=0 && Y_sum != 0) { if(PWM_50MS_Flag == 0) { LED_Disp(0X00); } } } /* 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_USART1_UART_Init(); MX_TIM2_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2); LCD_Init(); LCD_SetBackColor(Black); LCD_SetTextColor(White); LCD_Clear(Black); LED_Disp(0X00); I2CInit(); HAL_UART_Receive_IT(&huart1,rx_buff,1); X_sum = iic_read(0xa0,0); Y_sum = iic_read(0xa0,1); X_price = iic_read(0Xa0,2); Y_price = iic_read(0xa0,3); X_price = X_price/10; Y_price = Y_price/10; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { Key_Proc(); LCD_Proc(); LED_Proc(); /* 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}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {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(); } /** Initializes the peripherals clocks */ PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1; PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != 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 */ /* 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, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/920937.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存