基于STM32 -EEPROM驱动

  • V5_366772
    了解作者
  • 3.3KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-14 07:23
    上传日期
适用于各种大小的存储芯片,简单修改驱动引脚就可以用。
EEPROM.zip
  • EEPROM
  • eep.c
    8.1KB
  • eep.h
    1.4KB
内容介绍
/** ****************************************************************************** * @file eep.c * @author xxx * @version V1.0 * @date 15-August-2016 * @brief This file provides all the main firmware functions. ****************************************************************************** * @attention * * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. * * <h2><center>&copy; COPYRIGHT 2061 Maxonic</center></h2> ****************************************************************************** */ #include "eep.h" #include "config.h" /****************************************************************/ /* FM24CL04操作定义:*/ #define PAGE0 0 #define PAGE1 1 #define FM24_PAGE 256 #define FLAG_E2P_ADDR 0x01 #define FM24CL04 511 #define FM24CL16 1023 #define FM24CL32 2047 #define FM24CL64 8191 #define FM24_TYPE FM24CL64 #define E2P_END 8192 #define FM24_ADDR 0xA0 #define FM24CLXX_WR 0xA0 //1010 A2 A1 A0(page 0) R/W(write) #define FM24CLXX_RD 0xA1 //1010 A2 A1 A0(page 0) R/W(read) #define SCL_H GPIO_SetBits(GPIOB, GPIO_Pin_6) #define SCL_L GPIO_ResetBits(GPIOB, GPIO_Pin_6)//SCL PB6 #define SDA_H GPIO_SetBits(GPIOB, GPIO_Pin_7) #define SDA_L GPIO_ResetBits(GPIOB, GPIO_Pin_7)//SDA PB7 #define WP_H GPIO_SetBits(GPIOB, GPIO_Pin_5) //WP拉高写保护 #define WP_L GPIO_ResetBits(GPIOB, GPIO_Pin_5)//WP PB5 WP拉低可写可读 #define SDA_READ GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)//读SDA状态,用来判断从机是否返回响应 /* 单指令周期为125ns */ #define _1US_ 30 //8 #define _2US_ 90 //16 #define _4US_ 160 //32 #define _5US_ 200 //40 #define _10US_ 400 //80 EEPdata_float EEP_Data;//存储数据时使用,浮点数据按字节存储 //EEPROM初始化函数 void EEP_Init(void) { I2C2_Stop(); } /********************************************************************************************** @function E2PROM_WriteSingleByte @params addr, eeprom address to read from *p, write data size, number of bytes to write @notes write data from E2PROM at the address addr E2PROM FM24CL04 enforces a write with an address before a write for selective writes **********************************************************************************************/ void E2PROM_WriteSingleByte(u16 WriteAddr,u8 DataToWrite) { I2C2_Start(); if((FM24_TYPE>FM24CL16)&&(FM24_TYPE<=FM24CL64)) { Alarm_flag &= ~FAULT_EEPROM_HANDLE; I2C2_SendByte(FM24_ADDR); I2C2_ReceiveAck(); I2C2_SendByte(WriteAddr>>8); } else if(FM24_TYPE<=FM24CL16) { Alarm_flag &= ~FAULT_EEPROM_HANDLE; I2C2_SendByte(FM24_ADDR+((WriteAddr/256)<<1)); } else Alarm_flag |= FAULT_EEPROM_HANDLE; I2C2_ReceiveAck(); I2C2_SendByte(WriteAddr%256); I2C2_ReceiveAck(); I2C2_SendByte(DataToWrite); I2C2_ReceiveAck(); I2C2_Stop(); //在无应答信号发出后才能发停止信号 Delay(50); } /********************************************************************************************** @function E2PROM_ReadSingleByte @params addr, eeprom address to read from *p, read data size, number of bytes to read @notes Reads data from E2PROM at the address addr E2PROM FM24CL04 enforces a write with an address before a read for selective reads **********************************************************************************************/ u8 E2PROM_ReadSingleByte(u16 ReadAddr) { u8 temp_read; I2C2_Start(); if((FM24_TYPE>FM24CL16)&&(FM24_TYPE<=FM24CL64)) { Alarm_flag &= ~FAULT_EEPROM_HANDLE; I2C2_SendByte(FM24_ADDR); I2C2_ReceiveAck(); I2C2_SendByte(ReadAddr>>8); } else if(FM24_TYPE<=FM24CL16) { Alarm_flag &= ~FAULT_EEPROM_HANDLE; I2C2_SendByte(FM24_ADDR+((ReadAddr/256)<<1)); } else Alarm_flag |= FAULT_EEPROM_HANDLE; I2C2_ReceiveAck(); I2C2_SendByte(ReadAddr%256); I2C2_ReceiveAck(); I2C2_Start(); I2C2_SendByte(FM24CLXX_RD); //读操作 I2C2_ReceiveAck(); temp_read = I2C2_ReceiveByte(); I2C2_SendNoAck(); //在第9个SCL时,发无应答信号 I2C2_Stop(); //在无应答信号发出后才能发停止信号 Delay(50); return temp_read; } /********************************************************************************** @function E2PROM_WriteBytes @params addr, EEPROM 地址, *p, data character size, data size i.e. number of characters @notes Writing to address with data pointed by *p of number of bytes size ***********************************************************************************/ void E2PROM_WriteBytes(u16 WriteAddr,u8 *p,u16 size) { while(size--) { E2PROM_WriteSingleByte(WriteAddr,*p); WriteAddr++; p++; } } /****************************************************************************************** @function E2PROM_ReadBytes @params addr, eeprom address to read from *p, read data size, number of bytes to read @notes Reads data from E2PROM at the address addr E2PROM FM24CL04 enforces a write with an address before a read for selective reads ******************************************************************************************/ void E2PROM_ReadBytes(u16 WriteAddr, u8 *p, u16 size) { while(size) { *p++=E2PROM_ReadSingleByte(WriteAddr++); size--; } } /************************************************************* 读e2p字节数据 返回读到的字节数据 *************************************************************/ u8 I2C2_ReceiveByte(void) { u8 i,buf; buf=0; for(i=0;i<8;i++) { SCL_H; Delay(_1US_); //tHIGH>0.6us buf<<=1; if(SDA_READ ==1) buf|=0x01; else {;} Delay(_1US_); //tBUF>1.3us SCL_L; Delay(_1US_); //tLOW>1.3us } return(buf); } /************************************************************* 写e2p字节数据 byte=待写入的1字节数据 *************************************************************/ void I2C2_SendByte(u8 byte) { u8 i; for(i=0;i<8;i++) { if(byte&0x80) SDA_H; else SDA_L; byte<<=1; Delay(_2US_); //tBUF>1.3us SCL_H; Delay(_1US_); //tHIGH>0.6us SCL_L; Delay(_2US_); //tLOW>1.3us } } //发送启动信号 SCL为高电平间SDA出现下降沿 void I2C2_Start(void) { SCL_H; Delay(_1US_); //tHIGH>0.6us SDA_H; Delay(_2US_); //tBUF>1.3us Delay(_1US_); //tSU:STA>0.6us SDA_L; Delay(_1US_); //tHD:STA>0.6us SCL_L; Delay(_2US_); //tLOW>1.3us } //发送停止信号 SCL为高电平期间SDA出现上升沿 void I2C2_Stop(void) { SCL_H; Delay(_1US_); //tHIGH>0.6us SDA_L; Delay(_4US_); //tSU:STO>4.0us,由PCF8563决定 SDA_H; Delay(_2US_); //tBUF>1.3us SCL_L; Delay(_2US_); //tLOW>1.3us } //接收应答信号 bool I2C2_ReceiveAck(void) { u16 i; bool AckState = TRUE; SCL_H; Delay(_1US_); //tHIGH>0.6us for(i=0; SDA_READ==1; i++) { //等待应答信号到来,需要延时片刻,但又要防止因为无应答信号导致死机 if(i>1000) { AckState = FALSE; break; } } SCL_L; Delay(_2US_); //tLOW>1.3us return AckState; } //发送应答信号 void I2C2_SendAck(void) { SDA_L; Delay(_2US_); //tBUF>1.3us SCL_H;
评论
    相关推荐
    • STM32
      STM32存储库用于容纳我的学习项目,以便使用STM32开发板进行嵌入式系统编程。
    • STM32 一点心得
      包括 数模 模数转换 常用TFT液晶显示 2 8 [屏幕大小不影响显示] 数据存储 程序存储 SD卡 音乐播放器 硬件解码 [软件解码还没的搞好] 网卡芯片
    • STM32 内部 RTC 修正
      STM32的手册上说 内部RTC低速晶振频率典型是40K,实际上内部低速晶振的频率是会随温度变化而变化的。所以如果没有修正的话,时间会 偏差很大。这个项目实现了用TIM2对RTC的精确修正,同时应用了备份寄存器,存储修正...
    • STM32 flash存储实验
      STM32 flash存储实验,替代外接EEPROM,调试通过,仅供参考。
    • 芯达STM32入门系列
      作为stm32很不错的系列文档 目录:芯达STM32入门系列教程之一《如何提高STM32的学习效率》 芯达STM32入门系列教程之二《如何安装J-Link驱动软件》 芯达STM32入门系列教程之三《如何使用J-Flash调试》 芯达STM...
    • stm32STM32存储
      stm32 STM32版本的存储库。
    • STM32开发板
      TFT液晶屏,SD卡槽,红外传感器,温度传感器,测试灯,独立按键,JINK仿真,串口下载,一键下载,存储器满足基本工程开发的需求
    • stm32存储Excel/csv文件
      以excel的csv格式存储数据,SPI3端口操作,经过调试成功
    • STM32u盘串口通信
      双向串口通信,一个串口连接数据采集卡,另一个连接U盘存储模块,将数据采集卡的数据存储进u盘
    • STM32手册.zip
      STM32的各种手册,有STM32F10x-英文参考手册 STM32F10x-中文参考手册 STM32F103xCDE_数据手册-英文 2-STM32F103xCDE_数据手册-中文 CM3权威指南CnR2 STM8和STM32产品选型手册 STM32F10xxx Cortex-M3编程手册-英文版 ...