蓝桥杯嵌入式赛道复习笔记8(eeprom读写)
原理学习
自己看一下江科大的存储器的读取,原理是一样的。只是使用了IIC原理是不变的
代码
cubeMX的配置
代码
eeprom层代码的书写
#include "eeprom_display.h"
uint8_t data;
uint8_t eeprom_read(uint8_t addr){
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStop();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
data=I2CReceiveByte();
I2CSendNotAck();
I2CStop();
return data;
}
void eeprom_write(uint8_t addr,uint8_t dat){
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
HAL_Delay(20);
}
main.c
LCD_Init();
I2CInit();
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
LCD_Clear(Black);
char buff[20];
uint8_t num_cin=1;
uint8_t num_cout=0;
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
char date_time[100];
while (1)
{
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
sprintf(date_time, " test ");
LCD_DisplayStringLine(Line0, (uint8_t *)date_time);
sprintf(date_time,"Date %d-%d-%d-%d",sDate.Year,sDate.Month,sDate.WeekDay,sDate.Date);
LCD_DisplayStringLine(Line2,(uint8_t *)date_time);
sprintf(date_time,"Time %2d-%2d-%2d",sTime.Hours,sTime.Minutes,sTime.Seconds);
LCD_DisplayStringLine(Line3,(uint8_t *)date_time);
sprintf(date_time,"Alarm %d",count_alarm);
LCD_DisplayStringLine(Line4,(uint8_t *)date_time);
// eeprom_write(0,num_cin);
// sprintf(buff,"cin number %d",num_cin);
// LCD_DisplayStringLine(Line2,(uint8_t *)buff);
// num_cout=eeprom_read(0);
// sprintf(buff,"cout number %d",num_cout);
// LCD_DisplayStringLine(Line3,(uint8_t *)buff);
// num_cin++;
// HAL_Delay(1000);
//HAL_PWREx_DisableUSBDeadBatteryPD();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}