文章目录
- 前言
- 一、题目![请添加图片描述](https://img-blog.csdnimg.cn/ccdd07679c9b4d86b6faad3554637eba.png)
- 二、模块初始化
- 三、代码实现
- interrupt.h:
- interrupt.c:
- main.h:
- main.c:
- 四、完成效果
- 五、总结
前言
无
一、题目
二、模块初始化
1.LCD这里不用配置,直接使用提供的资源包就行
2.AKEY, 打开双ADC采集再对采集值处理按键
3.LED:开启PC8-15,PD2输出模式就行了。
4.定时器:TIM3(按键消抖定时器):PSC:80-1,ARR:10000-1,TIM4(LED闪烁计时器):PSC:80-1,ARR:999
5.PA4:ADC_IN17,PA5:ADC_IN13都要为Single-ended模式
6.i2c:设置PB6,PB7为GPIO_Output模式即可
7.打开串口串行输出
三、代码实现
bsp组中共有:
interrupt.h:
#ifndef __INTERRUPT_H__
#define __INTERRUPT_H__
#include "main.h"
#include "stdbool.h"
struct keys
{
bool key_sta;
unsigned char key_judge;
bool single_flag;
unsigned int key_time;
bool long_flag;
};
#endif
interrupt.c:
#include "interrupt.h"
#include "akey.h"
#include "lcd.h"
struct keys akey[8] = {0, 0, 0, 0, 0};
unsigned char keyioAnti;
unsigned int LEDTick;
extern unsigned char LED;
unsigned char LED1Type;
unsigned int LEDpulse;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)
{
if(htim->Instance == TIM3)
{
ADC_Key_IO();
keyioAnti = ~keyio;
akey[0].key_sta = keyioAnti >> 0 & 0x01;
akey[1].key_sta = keyioAnti >> 1 & 0x01;
akey[2].key_sta = keyioAnti >> 2 & 0x01;
akey[3].key_sta = keyioAnti >> 3 & 0x01;
akey[4].key_sta = keyioAnti >> 4 & 0x01;
akey[5].key_sta = keyioAnti >> 5 & 0x01;
akey[6].key_sta = keyioAnti >> 6 & 0x01;
akey[7].key_sta = keyioAnti >> 7 & 0x01;
for(unsigned char i= 0; i < 8 ;i++)
{
switch(akey[i].key_judge)
{
case 0:
{
if(akey[i].key_sta == 0)
{
akey[i].key_judge = 1;
akey[i].key_time = 0;
// LCD_DisplayStringLine(Line0, "here");
}
break;
}
case 1:
{
if(akey[i].key_sta == 0)
{
akey[i].key_judge = 2;
}
else
{
akey[i].key_judge = 0;
}
break;
}
case 2:
{
if(akey[i].key_sta == 1)
{
akey[i].key_judge = 0;
if(akey[i].key_judge < 80)
{
akey[i].single_flag = 1;
}
}
else
{
akey[i].key_time++;
if(akey[i].key_time >= 80)
{
akey[i].long_flag = 1;
}
}
break;
}
}
}
}
if(htim->Instance == TIM4)
{
LEDTick++;
if(LEDTick == LEDpulse)
{
LEDTick = 0;
LED1Type = !LED1Type;
LED = LED & 0xfe | LED1Type;
}
}
}
main.h:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.h
* @brief : Header for main.c file.
* This file contains the common defines of the application.
******************************************************************************
* @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 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_hal.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
/* USER CODE END ET */
/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
/* USER CODE END EC */
/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */
/* Private defines -----------------------------------------------------------*/
/* USER CODE BEGIN Private defines */
#define DATA 0
#define PARA 1
/* USER CODE END Private defines */
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H */
main.c:
/* 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 "adc.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "akey.h"
#include "dadc.h"
#include "lcd.h"
#include "stdio.h"
#include "interrupt.h"
#include "i2c.h"
#include "stdlib.h"
#include "string.h"
#include "led.h"
/* 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 */
char text[30];
extern struct keys akey[8];
unsigned char eeprom_writedata;
unsigned char eeprom_readdata;
double weight;
unsigned char DisplayMode;
unsigned char goodsCode;
double Price[3] = {9.80, 9.80, 9.80};
double total;
unsigned char SettingIndex;
double TempPrice[3] = {9.80, 9.80, 9.80};
unsigned char SetN;
unsigned char keywait;
unsigned char LED;
extern unsigned int LEDpulse;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void DisposeKey(void);
void LCD_Disp(void);
/* 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_ADC2_Init();
MX_TIM3_Init();
MX_USART1_UART_Init();
MX_TIM4_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
LCD_DisplayStringLine(Line0, "ok");
HAL_TIM_Base_Start_IT(&htim3);
HAL_TIM_Base_Start_IT(&htim4);
getDualADC(&hadc2);
HAL_Delay(2);
getDualADC(&hadc2);
weight = adc2_in17_AO1 / 4096.0 * 10.0;
total = Price[goodsCode] * weight;
Price[0] = eeprom_read(0) + eeprom_read(1) / 100.0;
Price[1] = eeprom_read(2) + eeprom_read(3) / 100.0;
Price[2] = eeprom_read(4) + eeprom_read(5) / 100.0;
SetN = eeprom_read(6);
LED_Disp(0x00);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
getDualADC(&hadc2);
weight = adc2_in17_AO1 / 4096.0 * 10.0;
DisposeKey();
LCD_Disp();
if(DisplayMode == DATA)
{
LEDpulse = 800;
}
else
{
LEDpulse = 400;
}
LED_Disp(LED);
}
/* 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_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 */
void DisposeKey(void)
{
if(akey[0].single_flag)
{
LCD_Clear(Black);
if(DisplayMode == DATA)
{
DisplayMode = PARA;
}
else if(DisplayMode == PARA)
{
DisplayMode = DATA;
//±£´æ
eeprom_write(0, (unsigned int)(Price[0] * 100) / 100);
HAL_Delay(10);
eeprom_write(1, (unsigned int)(Price[0] * 100) % 100);
HAL_Delay(10);
eeprom_write(2, (unsigned int)(Price[1] * 100) / 100);
HAL_Delay(10);
eeprom_write(3, (unsigned int)(Price[1] * 100) % 100);
HAL_Delay(10);
eeprom_write(4, (unsigned int)(Price[2] * 100) / 100);
HAL_Delay(10);
eeprom_write(5, (unsigned int)(Price[2] * 100) % 100);
HAL_Delay(10);
if(TempPrice[0] != Price[0] || TempPrice[1] != Price[1] || TempPrice[2] != Price[2])
{
SetN++;
TempPrice[0] = Price[0];
TempPrice[1] = Price[1];
TempPrice[2] = Price[2];
eeprom_write(6, SetN);
}
// if((int)(TempPrice[0] * 100) != (int)(Price[0] * 100) ||(int)(TempPrice[1] * 100) != (int)(Price[1] * 100) || (int)(TempPrice[2] * 100) != (int)(Price[2] * 100))
// {
// SetN++;
// eeprom_write(6, SetN);
// }
//´®¿ÚÊä³ö
printf("U.W.1:%.2f\n", Price[0]);
printf("U.W.2:%.2f\n", Price[1]);
printf("U.W.3:%.2f\n", Price[2]);
}
akey[0].single_flag = 0;
}
if(akey[1].single_flag)
{
if(DisplayMode == PARA)
{
if((int)((Price[SettingIndex] + 0.01) * 100) <= 999)
{
Price[SettingIndex] += 0.01;
}
}
akey[1].single_flag = 0;
}
if(akey[1].long_flag)
{
if(DisplayMode == PARA)
{
keywait++;
if(keywait == 2)
{
keywait = 0;
if((int)((Price[SettingIndex] + 0.01) * 100) <= 999)
{
Price[SettingIndex] += 0.01;
}
}
}
akey[1].long_flag = 0;
}
if(akey[2].single_flag)
{
if(DisplayMode == PARA)
{
if((int)((Price[SettingIndex] - 0.01) * 100) >= 0)
{
Price[SettingIndex] -= 0.01;
}
if(Price[SettingIndex] < -0.000000)
Price[SettingIndex] = 0;
}
akey[2].single_flag = 0;
}
if(akey[2].long_flag)
{
if(DisplayMode == PARA)
{
keywait++;
if(keywait == 2)
{
keywait = 0;
if((int)((Price[SettingIndex] - 0.01) * 100) >= 0)
{
Price[SettingIndex] -= 0.01;
}
if(Price[SettingIndex] < -0.000000)
Price[SettingIndex] = 0;
}
}
akey[2].long_flag = 0;
}
if(akey[3].single_flag)
{
if(DisplayMode == PARA)
{
SettingIndex++;
SettingIndex %= 3;
}
akey[3].single_flag = 0;
}
if(akey[4].single_flag)
{
if(DisplayMode == DATA)
{
goodsCode = 0;
}
akey[4].single_flag = 0;
}
if(akey[5].single_flag)
{
if(DisplayMode == DATA)
{
goodsCode = 1;
}
akey[5].single_flag = 0;
}
if(akey[6].single_flag)
{
if(DisplayMode == DATA)
{
goodsCode = 2;
}
akey[6].single_flag = 0;
}
if(akey[7].single_flag)
{
if(DisplayMode == PARA)
{
total = weight * Price[goodsCode];
printf("U.W.%d:%.2f\n", goodsCode, Price[goodsCode]);
printf("G.W:%.2f\n", weight);
printf("Total:%.2f\n", total);
}
akey[7].single_flag = 0;
}
}
void LCD_Disp(void)
{
if(DisplayMode == DATA)
{
LCD_DisplayStringLine(Line0, " DATA");
sprintf(text, "good sCode:%d", goodsCode + 1);
LCD_DisplayStringLine(Line2, text);
sprintf(text, "goodsPrice:%.2f/kg", Price[goodsCode]);
LCD_DisplayStringLine(Line4, text);
sprintf(text, "weight :%.2fkg", weight);
LCD_DisplayStringLine(Line6, text);
sprintf(text, "total :%.1f", total);
LCD_DisplayStringLine(Line8, text);
}
if(DisplayMode == PARA)
{
LCD_DisplayStringLine(Line0, " PARA");
sprintf(text, "Price1:%.2lf/kg ", Price[0]);
if(SettingIndex == 0)
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line2, text);
LCD_SetBackColor(Black);
sprintf(text, "Price2:%.2lf/kg ", Price[1]);
if(SettingIndex == 1)
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line4, text);
LCD_SetBackColor(Black);
sprintf(text, "Price3:%.2lf/kg ", Price[2]);
if(SettingIndex == 2)
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line6, text);
LCD_SetBackColor(Black);
sprintf(text, " N:%d", SetN);
LCD_DisplayStringLine(Line9, text);
}
}
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
/* 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 */
四、完成效果
蓝桥杯嵌入式第九届国赛试题实现效果
五、总结
本篇文章只是为了存放我的代码,所以看不懂很正常,如果需要代码可以找我私信。