蓝桥杯嵌入式第六届真题(完成)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.hqwc.cn/news/452706.html

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

相关文章

C++参悟:扩展资源

有用的资源 一、概述二、模式、手法、提示和技巧1. cpppatterns2. C Core Guidelines3. MSDN 三、第三方库 一、概述 在 cppreference 中我发现了很多比较有趣的扩展资源。我是从其官网下载的离线 chm 版本以便查看 https://zh.cppreference.com/w/%E9%A6%96%E9%A1%B5 在我下…

YoloV8改进策略:Neck改进:HAM混合注意力机制改进YoloV8|多种改进,多种姿势涨点|代码注释详解

摘要 HAM通过快速一维卷积来缓解通道注意机制的负担,并引入通道分离技术自适应强调重要特征。HAM作为通用模块,在CIFAR-10、CIFAR-100和STL-10数据集上实现了SOTA级别的分类性能。 论文链接:https://www.sciencedirect.com/science/article/abs/pii/S0031320322002667?vi…

嵌入式学习第十六天!(Linux文件查看、查找命令、标准IO)

Linux软件编程 1. Linux&#xff1a; 操作系统的内核&#xff1a; 1. 管理CPU 2. 管理内存 3. 管理硬件设备 4. 管理文件系统 5. 任务调度 2. Shell&#xff1a; 1. 保护Linux内核&#xff08;用户和Linux内核不直接操作&#xff0c;通过操作Shell&#xff0c;Shell和内核交互…

Linux---yum命令详解

&#x1f4d9; 作者简介 &#xff1a;RO-BERRY &#x1f4d7; 学习方向&#xff1a;致力于C、C、数据结构、TCP/IP、数据库等等一系列知识 &#x1f4d2; 日后方向 : 偏向于CPP开发以及大数据方向&#xff0c;欢迎各位关注&#xff0c;谢谢各位的支持 目录 1.概念2.yum的配置信…

逻辑回归与感知机详解

一逻辑回归 采用log函数作为代价函数 1 用于二分类问题 2 cost成本函数定义 3 求最小值&#xff0c;链式求导法则 4 梯度下降法 5 结构图表示 二 感知机 样本点到超平面距离法 1 线性二分类问题 2 点到直线距离 3 更新w 和b 参数 4 算法流程 5 例子

MATLAB | 绘图复刻(十四) | 右侧对齐桑基图,及工具函数SSankey更新

hey 真的好久不见了&#xff0c;本期既是一期绘图复刻教程&#xff0c;也是我写的工具函数的版本更新&#xff0c;本期复刻的图片来自《Nature》&#xff1a; Elmarakeby, H.A., Hwang, J., Arafeh, R. et al. Biologically informed deep neural network for prostate cancer…

.NET Core Web API使用HttpClient提交文件的二进制流(multipart/form-data内容类型)

需求背景&#xff1a; 在需要通过服务端请求传递文件二进制文件流数据到相关的服务端保存时&#xff0c;如对接第三方接口很多情况下都会提供一个上传文件的接口&#xff0c;但是当你直接通过前端Ajax的方式将文件流上传到对方提供的接口的时候往往都会存在跨域的情况&#xff…

随机图论基础

一&#xff0c;随机图、随机图空间 1&#xff0c;随机图 一个n个点的无向图&#xff0c;最多有sn(n-1)/2条边。 每条边都有一定的概率存在&#xff0c;有一定概率不存在&#xff0c;那么每个图都有一个出现概率。 2&#xff0c;随机图空间 一共有2^s种不同的图&#xff0c…

LeAPI 后端接口开发 - 发布、下线接口

一、上线接口&#xff08;仅管理员&#xff09; 1. 校验请求参数 2. 判断&#xff08;测试&#xff09;接口是否可以调用 引入调用接口的客户端&#xff08;自己写的 SDK&#xff09;注入客户端实例调用接口 3. 修改数据库中接口的状态 /*** 上线&#xff08;发布&#xff…

Linux一些实用操作

学习笔记&#xff0c;记录以下课程中关于Linux的一些实用操作。黑马程序员新版Linux零基础快速入门到精通&#xff0c;全涵盖linux系统知识、常用软件环境部署、Shell脚本、云平台实践、大数据集群项目实战等_哔哩哔哩_bilibili 目录 1 各类小技巧&#xff08;快捷键&#xff…

[C#][opencvsharp]opencvsharp sift和surf特征点匹配

SIFT特征和SURF特征比较 SIFT特征基本介绍 SIFT(Scale-Invariant Feature Transform)特征检测关键特征&#xff1a; 建立尺度空间&#xff0c;寻找极值关键点定位&#xff08;寻找关键点准确位置与删除弱边缘&#xff09;关键点方向指定关键点描述子 建立尺度空间&#xff0…

Java基础之反射

反射目录&#xff0c;重点的顶层接口 1. 顶层接口1.1 概述&#xff08;重点&#xff09;1.2 Member1.3 AnnotatedElement1.4 Type1.4.1 概述&#xff08;重点&#xff09;1.4.2 GenericDeclaration1.4.3 TypeVariable1.4.4 ParameterizedType1.4.5 WildcardType上下界解释 1.4.…