蓝桥杯嵌入式第六届真题(完成)STM32G431

蓝桥杯嵌入式第六届真题(完成)STM32G431

题目部分

image-20240131021944807

image-20240131021954358

image-20240131022003532

image-20240131022011969

相关文件

main.c
/* USER CODE BEGIN Header */
/********************************************************************************* @file           : main.c* @brief          : Main program body******************************************************************************* @attention** <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.* All rights reserved.</center></h2>** 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 "adc.h"
#include "rtc.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "i2c_hal.h"
#include "key.h"
#include "myadc.h"
#include "led.h"
#include "string.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint8_t lcdtext[30];
extern struct Key key[4];
uint8_t view = 0;//鍒濆lcd鏄剧ず
RTC_TimeTypeDef Time;
RTC_DateTypeDef Date;
uint8_t h=0,m=0,s=0;
float val;
float k = 0.1;
uint8_t ledflag = 1;
uint8_t ledtimes;
extern unsigned char Recive_Data[5];
extern unsigned char Temp_Data[1];
extern bool rxflag;
extern unsigned char rx_pointer;
uint32_t counter = 0;
bool ledState = false;
/* 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 */
void key_process(void);
void lcd_process(void);
void led_process(void);
void rx_process(void);
void tx_process(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_TIM2_Init();MX_ADC2_Init();MX_RTC_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */HAL_TIM_Base_Start_IT(&htim2);HAL_UART_Receive_IT(&huart1, Temp_Data, 1);LCD_Init();/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);//EEPROM_Write_Float(0,0.9);//HAL_Delay(5);while (1){lcd_process();//EEPROM_Write(0,20);// HAL_RTC_GetDate(&hrtc,&Date,RTC_FORMAT_BIN);// HAL_RTC_GetTime(&hrtc,&Time,RTC_FORMAT_BIN);// sprintf((char *)lcdtext,"%.2f",get_adc(&hadc2));// LCD_DisplayStringLine(Line2,lcdtext);// sprintf((char *)lcdtext,"%02d-%02d-%02d",Time.Hours,Time.Minutes,Time.Seconds);// LCD_DisplayStringLine(Line4,lcdtext);key_process();led_process();rx_process();tx_process();/* 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_HSI|RCC_OSCILLATORTYPE_LSI;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;RCC_OscInitStruct.LSIState = RCC_LSI_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;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_RTC|RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_ADC12;PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 */
void key_process(void)
{if(key[0].key_flag==1&&view==0){LCD_Clear(Black);key[0].key_flag=0;view = 1;ledflag = 0;}else if(key[0].key_flag==1&&view==1){LCD_Clear(Black);key[0].key_flag=0;view = 0;ledflag = 1;}if(key[1].key_flag==1&&(view==0||view==1)){LCD_Clear(Black);key[1].key_flag=0;view = 2;}if(key[1].key_flag==1&&(view==3||view==4||view==5)){LCD_Clear(Black);key[1].key_flag=0;view = 0;}if(key[2].key_flag==1&&(view==2||view==3||view==4||view==5)){LCD_Clear(Black);key[2].key_flag=0;view++;if(view>5){view = 3;}}if(key[3].key_flag==1&&view==3){LCD_Clear(Black);key[3].key_flag=0;h++;if(h>24){h=0;}}else if (key[3].key_flag==1&&view==4){LCD_Clear(Black);key[3].key_flag=0;m++;if(m>60){m=0;}}else if (key[3].key_flag==1&&view==5){LCD_Clear(Black);key[3].key_flag=0;s++;if(s>60){s=0;}}}void lcd_process(void)
{switch (view){case 0: //LED鎵撳紑鐣岄潰{val = get_adc(&hadc2);sprintf((char *)lcdtext,"    V1:%.2f  ",val);LCD_DisplayStringLine(Line1,lcdtext);sprintf((char *)lcdtext,"    K:%.1f  ",EEPROM_Read_Float(0));LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext,"    LED:ON  ");LCD_DisplayStringLine(Line5,lcdtext);HAL_RTC_GetDate(&hrtc,&Date,RTC_FORMAT_BIN);HAL_RTC_GetTime(&hrtc,&Time,RTC_FORMAT_BIN);sprintf((char *)lcdtext,"    T:%02d-%02d-%02d",Time.Hours,Time.Minutes,Time.Seconds);LCD_DisplayStringLine(Line7,lcdtext);}break;case 1://LED鍏抽棴鐣岄潰{val = get_adc(&hadc2);sprintf((char *)lcdtext,"    V1:%.2f  ",val);LCD_DisplayStringLine(Line1,lcdtext);sprintf((char *)lcdtext,"    K:%.1f  ",EEPROM_Read_Float(0));LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext,"    LED:OFF  ");LCD_DisplayStringLine(Line5,lcdtext);HAL_RTC_GetDate(&hrtc,&Date,RTC_FORMAT_BIN);HAL_RTC_GetTime(&hrtc,&Time,RTC_FORMAT_BIN);sprintf((char *)lcdtext,"    T:%02d-%02d-%02d",Time.Hours,Time.Minutes,Time.Seconds);LCD_DisplayStringLine(Line7,lcdtext);}break;case 2://璁剧疆鐣岄潰{sprintf((char *)lcdtext,"       Setting  ");LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext,"    %02d - %02d - %02d",h,m,s);LCD_DisplayStringLine(Line5,lcdtext);}break;case 3://璁剧疆灏忔椂鐣岄潰{sprintf((char *)lcdtext,"       Setting  ");LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext,"    %02d - %02d - %02d",h,m,s);LCD_DisplayStringLine(Line5,lcdtext);sprintf((char *)lcdtext,"    --");LCD_DisplayStringLine(Line6,lcdtext);}break;case 4://璁剧疆鍒嗛挓鐣岄潰{sprintf((char *)lcdtext,"       Setting  ");LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext,"    %02d - %02d - %02d",h,m,s);LCD_DisplayStringLine(Line5,lcdtext);sprintf((char *)lcdtext,"         --");LCD_DisplayStringLine(Line6,lcdtext);}break;case 5://璁剧疆绉掔晫闈?{sprintf((char *)lcdtext,"       Setting  ");LCD_DisplayStringLine(Line3,lcdtext);sprintf((char *)lcdtext,"    %02d - %02d - %02d",h,m,s);LCD_DisplayStringLine(Line5,lcdtext);sprintf((char *)lcdtext,"              --");LCD_DisplayStringLine(Line6,lcdtext);}break;default:break;}}void led_process(void)
{if (ledflag && val > 3.3f * k){if (ledState){leddisplay(0x02);}else{leddisplay(0x00);}}
}void rx_process(void)
{if(rxflag){if(Recive_Data[3] == '1'){k = 0.1;char My_sentdata[30];sprintf(My_sentdata,"ok\n");HAL_UART_Transmit(&huart1,(uint8_t*)My_sentdata,strlen(My_sentdata),50);}else if(Recive_Data[3] == '2'){k = 0.2;char My_sentdata[30];sprintf(My_sentdata,"ok\n");HAL_UART_Transmit(&huart1,(uint8_t*)My_sentdata,strlen(My_sentdata),50);}else if(Recive_Data[3] == '3'){k = 0.3;char My_sentdata[30];sprintf(My_sentdata,"ok\n");HAL_UART_Transmit(&huart1,(uint8_t*)My_sentdata,strlen(My_sentdata),50);}else if(Recive_Data[3] == '4'){k = 0.4;char My_sentdata[30];sprintf(My_sentdata,"ok\n");HAL_UART_Transmit(&huart1,(uint8_t*)My_sentdata,strlen(My_sentdata),50);}else if(Recive_Data[3] == '5'){k = 0.5;char My_sentdata[30];sprintf(My_sentdata,"ok\n");HAL_UART_Transmit(&huart1,(uint8_t*)My_sentdata,strlen(My_sentdata),50);}else if(Recive_Data[3] == '6'){k = 0.6;char My_sentdata[30];sprintf(My_sentdata,"ok\n");HAL_UART_Transmit(&huart1,(uint8_t*)My_sentdata,strlen(My_sentdata),50);}else if(Recive_Data[3] == '7'){k = 0.7;char My_sentdata[30];sprintf(My_sentdata,"ok\n");HAL_UART_Transmit(&huart1,(uint8_t*)My_sentdata,strlen(My_sentdata),50);}else if(Recive_Data[3] == '8'){k = 0.8;char My_sentdata[30];sprintf(My_sentdata,"ok\n");HAL_UART_Transmit(&huart1,(uint8_t*)My_sentdata,strlen(My_sentdata),50);}else if(Recive_Data[3] == '9'){k = 0.9;char My_sentdata[30];sprintf(My_sentdata,"ok\n");HAL_UART_Transmit(&huart1,(uint8_t*)My_sentdata,strlen(My_sentdata),50);}EEPROM_Write_Float(0,k);rx_pointer=0;rxflag=false; memset(Recive_Data,0,5);}}void tx_process(void)
{static bool already_sent = false;if(Time.Hours == h && Time.Minutes == m && Time.Seconds == s){if (!already_sent) // 检查是否已经发送过数据{char My_sentdata[30];sprintf(My_sentdata,"%.2f+%.1f+%02d%02d%02d\n", val, k, h, m, s);HAL_UART_Transmit(&huart1, (uint8_t*)My_sentdata, strlen(My_sentdata), 50);already_sent = true; // 标记已发送}}else{already_sent = false; // 当时间改变时重置标志}
}/* 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****/
void key_process(void);

处理按键任务,用户多个界面之间的转换以及上报时间的设置,根据当前lcd的状态,来决定按键按下的功能

void lcd_process(void);

显示各种状态,使用状态机,显示不同状态,使用sprinf函数格式化重定向字符串

void led_process(void);

控制led闪烁,使用一个计数值,在抵达定时器中++实现200ms计数

image-20240131022539272

void rx_process(void);

控制串口发送的数据,不知为何HAL_UART_Receive_IT(huart, Temp_Data, 1);中如果不是1,就会只能进入一次串口接收回调函数,所以使用每次接收一个然后设置一个缓冲区,控制指针来一次接收5个数据,只有接受完五个数据即进入5次串口接收回调函数后才执行解析函数,注意最后全波清0

void tx_process(void);

控制上报的数据,当rtc时钟的时间到达设置时间发送数据

led.c
#include "led.h"void leddisplay(uint8_t led)
{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOC,led<<8,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}

该板子是低电平点亮,8个led灯使用的是高8位所以需要左移8位,led等于几就是将高8位中第几位设置成低电平即点亮,由于led与lcd复用引脚最后打开锁存器让值被写入之后,立刻关闭锁存器防止影响lcd

myadc.c
#include "myadc.h"float get_adc(ADC_HandleTypeDef *hadc)
{float val;// 等待ADC转换完成HAL_ADC_Start(hadc);val = HAL_ADC_GetValue(hadc);return val*3.3f/4096;
}
usart1.c
#include "usart1.h"
#include "string.h"
#include "usart.h"
#include "stdbool.h"
extern float k;
unsigned char Recive_Data[5];
unsigned char Temp_Data[1];
unsigned char rx_pointer = 0;
bool rxflag = false;
char usartsend[30];
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{if(huart->Instance==USART1){Recive_Data[rx_pointer++] = Temp_Data[0];HAL_UART_Receive_IT(huart, Temp_Data, 1);if(Recive_Data[4]!=0){rxflag = true;}}
}

接收完四个之后置一个标志位,rx_process才能执行,每次都需调用HAL_UART_Receive_IT函数重新开启串口接收

key.c
#include "key.h"
#include "led.h"
struct Key key[4]={0,0,0,0};
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM2){key[0].key_gpio = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);key[1].key_gpio = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);key[2].key_gpio = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);key[3].key_gpio = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);for(int i = 0; i<4;i++){switch (key[i].key_index){case 0:/* constant-expression */{if(key[i].key_gpio==0){key[i].key_index = 1;}}break;case 1:{if(key[i].key_gpio==0){key[i].key_index = 2;key[i].key_flag=1;}else{key[i].key_index = 0;}}case 2:{if(key[i].key_gpio==1){key[i].key_index=0;}}break;}}}
}

使用状态机,第一次进入之后,进入下一个状态,如果电平还是刚刚的电平说明真的按下,如果不是重新进入第一个状态重新判断,定时器定时时间10ms刚好消抖,最后一个case2是判断如果按键松开,说明可以进入下一次判断。

i2c_hal.c
/*程序说明: CT117E-M4嵌入式竞赛板GPIO模拟I2C总线驱动程序软件环境: MDK-ARM HAL库硬件环境: CT117E-M4嵌入式竞赛板日    期: 2020-3-1
*/#include "i2c_hal.h"
#include "main.h"
#define DELAY_TIME	20/*** @brief SDA线输入模式配置* @param None* @retval None*/
void SDA_Input_Mode()
{GPIO_InitTypeDef GPIO_InitStructure = {0};GPIO_InitStructure.Pin = GPIO_PIN_7;GPIO_InitStructure.Mode = GPIO_MODE_INPUT;GPIO_InitStructure.Pull = GPIO_PULLUP;GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}/*** @brief SDA线输出模式配置* @param None* @retval None*/
void SDA_Output_Mode()
{GPIO_InitTypeDef GPIO_InitStructure = {0};GPIO_InitStructure.Pin = GPIO_PIN_7;GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;GPIO_InitStructure.Pull = GPIO_NOPULL;GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}/*** @brief SDA线输出一个位* @param val 输出的数据* @retval None*/
void SDA_Output( uint16_t val )
{if ( val ){GPIOB->BSRR |= GPIO_PIN_7;}else{GPIOB->BRR |= GPIO_PIN_7;}
}/*** @brief SCL线输出一个位* @param val 输出的数据* @retval None*/
void SCL_Output( uint16_t val )
{if ( val ){GPIOB->BSRR |= GPIO_PIN_6;}else{GPIOB->BRR |= GPIO_PIN_6;}
}/*** @brief SDA输入一位* @param None* @retval GPIO读入一位*/
uint8_t SDA_Input(void)
{if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == GPIO_PIN_SET){return 1;}else{return 0;}
}/*** @brief I2C的短暂延时* @param None* @retval None*/
static void delay1(unsigned int n)
{uint32_t i;for ( i = 0; i < n; ++i);
}/*** @brief I2C起始信号* @param None* @retval None*/
void I2CStart(void)
{SDA_Output(1);delay1(DELAY_TIME);SCL_Output(1);delay1(DELAY_TIME);SDA_Output(0);delay1(DELAY_TIME);SCL_Output(0);delay1(DELAY_TIME);
}/*** @brief I2C结束信号* @param None* @retval None*/
void I2CStop(void)
{SCL_Output(0);delay1(DELAY_TIME);SDA_Output(0);delay1(DELAY_TIME);SCL_Output(1);delay1(DELAY_TIME);SDA_Output(1);delay1(DELAY_TIME);}/*** @brief I2C等待确认信号* @param None* @retval None*/
unsigned char I2CWaitAck(void)
{unsigned short cErrTime = 5;SDA_Input_Mode();delay1(DELAY_TIME);SCL_Output(1);delay1(DELAY_TIME);while(SDA_Input()){cErrTime--;delay1(DELAY_TIME);if (0 == cErrTime){SDA_Output_Mode();I2CStop();return ERROR;}}SDA_Output_Mode();SCL_Output(0);delay1(DELAY_TIME);return SUCCESS;
}/*** @brief I2C发送确认信号* @param None* @retval None*/
void I2CSendAck(void)
{SDA_Output(0);delay1(DELAY_TIME);delay1(DELAY_TIME);SCL_Output(1);delay1(DELAY_TIME);SCL_Output(0);delay1(DELAY_TIME);}/*** @brief I2C发送非确认信号* @param None* @retval None*/
void I2CSendNotAck(void)
{SDA_Output(1);delay1(DELAY_TIME);delay1(DELAY_TIME);SCL_Output(1);delay1(DELAY_TIME);SCL_Output(0);delay1(DELAY_TIME);}/*** @brief I2C发送一个字节* @param cSendByte 需要发送的字节* @retval None*/
void I2CSendByte(unsigned char cSendByte)
{unsigned char  i = 8;while (i--){SCL_Output(0);delay1(DELAY_TIME);SDA_Output(cSendByte & 0x80);delay1(DELAY_TIME);cSendByte += cSendByte;delay1(DELAY_TIME);SCL_Output(1);delay1(DELAY_TIME);}SCL_Output(0);delay1(DELAY_TIME);
}/*** @brief I2C接收一个字节* @param None* @retval 接收到的字节*/
unsigned char I2CReceiveByte(void)
{unsigned char i = 8;unsigned char cR_Byte = 0;SDA_Input_Mode();while (i--){cR_Byte += cR_Byte;SCL_Output(0);delay1(DELAY_TIME);delay1(DELAY_TIME);SCL_Output(1);delay1(DELAY_TIME);cR_Byte |=  SDA_Input();}SCL_Output(0);delay1(DELAY_TIME);SDA_Output_Mode();return cR_Byte;
}//
void I2CInit(void)
{GPIO_InitTypeDef GPIO_InitStructure = {0};GPIO_InitStructure.Pin = GPIO_PIN_7 | GPIO_PIN_6;GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStructure.Pull = GPIO_PULLUP;GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
}void EEPROM_Write(uint8_t address,uint8_t Data)
{I2CStart();I2CSendByte(0xA0);I2CWaitAck();I2CSendByte(address);I2CWaitAck();I2CSendByte(Data);I2CWaitAck();I2CStop();
}uint8_t EEPROM_Read(uint8_t address)
{uint8_t data;I2CStart();I2CSendByte(0xA0);I2CWaitAck();I2CSendByte(address);I2CWaitAck();I2CStop();I2CStart();I2CSendByte(0xA1);I2CWaitAck();data = I2CReceiveByte();I2CWaitAck();I2CStop();return data;}void EEPROM_Write_Float(uint8_t address, float data)
{union FloatUnion fu;fu.floatval = data;for(int i = 0; i < sizeof(float); i++){EEPROM_Write(address + i, fu.bytes[i]);HAL_Delay(5); }
}float EEPROM_Read_Float(uint8_t address)
{union FloatUnion fu;for(int i = 0; i < sizeof(float); i++){fu.bytes[i] = EEPROM_Read(address + i);}return fu.floatval;
}

只有最后四个函数是本人写的,其余为蓝桥杯官方提供,主要是两种i2c时序,指定地址写和指定地址读的时序,重点在读需要首先写入要读取的地址之后再开始读,浮点数的存储使用联合体union

image-20240131023917944

floatval和bytes数组共用内存

可以看本人文章stm32教程中有对i2c四种时序的说明

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/680241.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode:69.x的平方根

嗨嗨嗨&#xff0c;二分又来了&#xff0c;淦它&#xff0c; 这个题官解是&#xff0c;C函数法&#xff0c;二分&#xff0c;和牛顿迭代法&#xff08;暂且搁置&#xff09;&#xff0c; 当然还有暴力&#xff08;不必讨论&#xff0c;就从0开始一个一个试&#xff09;&#…

Git 消除对某个文件的追踪

参考&#xff1a; &#xff08;尚未验证他的该方法&#xff09; https://www.golinuxcloud.com/git-remove-file-from-tracking/https://www.golinuxcloud.com/git-remove-file-from-tracking/

计网day1

RTT&#xff1a;往返传播时延&#xff08;越大&#xff0c;游戏延迟&#xff09; 一.算机网络概念 网络&#xff1a;网样的东西&#xff0c;网状系统 计算机网络&#xff1a;是一个将分散得、具有独立功能的计算机系统&#xff0c;通过通信设备与线路连接起来&#xff0c;由功…

[疑难杂症2024-001] java多线程运行时遇到java.util.ConcurrentModificationException的解决方案

本文由Markdown语法编辑器编辑完成。 1.背景 由于近日在改进一个医学图像的收图服务。之前的版本&#xff0c;我们采用了pynetdicom的服务。 https://pydicom.github.io/pynetdicom/stable/ 它的介绍为: pynetdicom is a pure Python package that implements the DICOM net…

基于大语言模型的AI Agents

代理&#xff08;Agent&#xff09;指能自主感知环境并采取行动实现目标的智能体。基于大语言模型&#xff08;LLM&#xff09;的 AI Agent 利用 LLM 进行记忆检索、决策推理和行动顺序选择等&#xff0c;把Agent的智能程度提升到了新的高度。LLM驱动的Agent具体是怎么做的呢&a…

Hive调优——explain执行计划

一、explain查询计划概述 explain将Hive SQL 语句的实现步骤、依赖关系进行解析&#xff0c;帮助用户理解一条HQL 语句在底层是如何实现数据的查询及处理&#xff0c;通过分析执行计划来达到Hive 调优&#xff0c;数据倾斜排查等目的。 https://cwiki.apache.org/confluence/d…

Java安全 URLDNS链分析

Java安全 URLDNS链分析 什么是URLDNS链URLDNS链分析调用链路HashMap类分析URL类分析 exp编写思路整理初步expexp改进最终exp 什么是URLDNS链 URLDNS链是Java安全中比较简单的一条利用链&#xff0c;无需使用任何第三方库&#xff0c;全依靠Java内置的一些类实现&#xff0c;但…

网络专栏目录

大家好我是苏麟 , 这是网络专栏目录 . 图解网络 资料来源 : 小林coding 小林官方网站 : 小林coding (xiaolincoding.com) 图解网络目录 基础篇 基础篇 TCP/IP网络模型有几层? : TCP/IP网络模型 键入网址到页面显示,期间发生了什么? : 键入网址到页面显示,期间发生了什么 现阶…

C++:面向对象——类的构造

1.1学会面向对象的编程思想 面向对象的英文缩写是OO&#xff0c;它是一种设计思想。 面向对象有3大特点&#xff1a;封装、继承和多态。 1.封装 封装有两个作用&#xff0c;一个是将不同的小对象封装成一个大对象&#xff1b;另外一个是把一部分内部属性和功能对外界屏蔽。…

数据结构哈希表

这里个大家用数组来模拟哈希表 法一&#xff1a;拉链法 法二&#xff1a;开放寻址法 /** Project: 11_哈希表* File Created:Sunday, January 17th 2021, 2:11:23 pm* Author: Bug-Free* Problem:AcWing 840. 模拟散列表 拉链法*/ #include <cstring> #include <iostr…

Gazebo无法显示模型 [Err] [InsertModelWidget.cc:403] Missing model.config for model

跟着教程一步步运行demo project时遇到了一个问题&#xff1a; 运行launch文件后&#xff0c;gazebo中世界文件加载了&#xff0c;但是机器人模型没有加载出来&#xff0c;之前没有遇到过这种情况。 后面发现&#xff0c;运行launch命令时&#xff0c;一直处在某个conda环境下&…

牛客——递归实现指数型枚举(枚举,dfs)

链接&#xff1a;登录—专业IT笔试面试备考平台_牛客网 来源&#xff1a;牛客网 题目描述 从 1∼n1\sim n1∼n这 n (n≤16)(n \leq 16)(n≤16) 个整数中随机选取任意多个&#xff0c;输出所有可能的选择方案。 输入描述: 一个整数n。 输出描述: 每行一种方案。同一行内…

Vue学习笔记(三)常用指令、生命周期

Vue学习笔记&#xff08;三&#xff09;常用指令 vue指令&#xff1a;html标签上带有 v- 前缀的特殊属性&#xff0c;不同的指令具有不同的含义&#xff0c;可以实现不同的功能。 常用指令&#xff1a; 指令作用v-for列表渲染&#xff0c;遍历容器的元素或者对象的属性v-bind…

docker安装、运行

1、安装 之前有docker的话&#xff0c;需要先卸载旧版本&#xff1a; sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine 安装之前需要安装yum工具&#xff1a; sud…

SHA-512在Go中的实战应用: 性能优化和安全最佳实践

SHA-512在Go中的实战应用: 性能优化和安全最佳实践 简介深入理解SHA-512算法SHA-512的工作原理安全性分析SHA-512与SHA-256的比较结论 实际案例分析数据完整性验证用户密码存储数字签名总结 性能优化技巧1. 利用并发处理2. 避免不必要的内存分配3. 适当的数据块大小总结 与其他…

python 笔记:shapely(形状篇)

主要是点&#xff08;point&#xff09;、线&#xff08;linestring&#xff09;、面&#xff08;surface&#xff09; 1 基本方法和属性 object.area 返回对象的面积&#xff08;浮点数&#xff09; object.bounds 返回一个&#xff08;minx, miny, maxx, maxy&#xff09;元…

《乱弹篇(十三)明朝事儿》

2024年农历除夕夜&#xff0c;因追剧收看电视连续剧《后宫》而放弃了收看一年一度的《春晚》&#xff0c;至到春节&#xff08;农历正月初一&#xff09;晚才看完了《后宫》。 社交网站“必应”图片《后宫》 电视连续剧《后宫》&#xff0c; 讲的是明朝英宗末年的历史故事&…

python -m SimpleHTTPServer mac报错

错误内容&#xff1a; Traceback (most recent call last):File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 174, in _run_module_as_main"__main__", fname, loader, pkg_name)File "/System/Libra…

【数据回顾】20240205千股跌停的信息面回顾

一、回顾过去 这是发生在2024年2月5日的事件&#xff0c;一千多只股票跌停&#xff0c;当时传闻的利空消息主要是&#xff1a; 1. 基金公司把基金持仓的股票转融通给别人做空&#xff0c;收融券利息&#xff0c;然后这利息还不算基金收益。 2. 上海机场发布&#xff0c;机场往来…

【更新】企业数字化转型-年度报告175个词频、文本统计

数据说明&#xff1a; 这份数据含数字化转型175个词频、各维度水平&#xff0c;保留2000-2021年数据。参考吴非、赵宸宇两位老师做法&#xff0c;根据上市公司年报文本&#xff0c;整理数字化转型175个词频数据&#xff0c;希望对大家有所帮助。 参考管理世界中吴非&#xff…