我们从2011年坚守至今,只想做存粹的技术论坛。  由于网站在外面,点击附件后要很长世间才弹出下载,请耐心等待,勿重复点击不要用Edge和IE浏览器下载,否则提示不安全下载不了

 找回密码
 立即注册
搜索
查看: 646|回复: 2

EEPROM 驱动程序

[复制链接]

该用户从未签到

3

主题

1

回帖

0

积分

一级逆天

积分
0

终身成就奖

发表于 2021-2-8 09:44:40 | 显示全部楼层 |阅读模式
#include "eeprom.h"

/** I2C 总线接口 */
#define I2C_PORT GPIOB
#define SDA_Pin    GPIO_Pin_7
#define SCL_Pin GPIO_Pin_6

#define FAILURE 0
#define SUCCESS 1




void SendData(uint8_t addr ,uint8_t data)
{
    I2CStart();
    I2CSendByte(0xa0);
    I2CWaitAck();
    I2CSendByte(addr);
    I2CWaitAck();
    I2CSendByte(data);
    I2CWaitAck();
    I2CStop();
}


uint8_t ReadData(uint8_t addr )
{
    uint8_t data;
   
    I2CStart();
    I2CSendByte(0xa0);
    I2CWaitAck();
    I2CSendByte(addr);
    I2CWaitAck();
   
    I2CStart();
    I2CSendByte(0xa1);
    I2CWaitAck();
    data = I2CReceiveByte();
    I2CSendNotAck();
    I2CStop();
    return data;
}










//配置SDA信号线为输入模式
void SDA_Input_Mode()
{
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin = SDA_Pin;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;     

      GPIO_Init(I2C_PORT, &GPIO_InitStructure);
}

//配置SDA信号线为输出模式
void SDA_Output_Mode()
{
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin = SDA_Pin;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

      GPIO_Init(I2C_PORT, &GPIO_InitStructure);
}

//
void SDA_Output( uint16_t val )
{
    if ( val ) {
        GPIO_SetBits(I2C_PORT,SDA_Pin);
    } else {
        GPIO_ResetBits(I2C_PORT,SDA_Pin);
    }
}

//
void SCL_Output( uint16_t val )
{
    if ( val ) {
        GPIO_SetBits(I2C_PORT,SCL_Pin);
    } else {
        GPIO_ResetBits(I2C_PORT,SCL_Pin);
    }
}

//
uint8_t SDA_Input()
{
    return GPIO_ReadInputDataBit( I2C_PORT, SDA_Pin);
}

//延时程序
void delay1(unsigned int n)
{
    unsigned int i;
    for ( i=0;i<n;++i);
}

//I2C总线启动
void I2CStart(void)
{
    SDA_Output(1);delay1(500);
    SCL_Output(1);delay1(500);
    SDA_Output(0);delay1(500);
    SCL_Output(0);delay1(500);
}

//I2C总线停止
void I2CStop(void)
{
    SCL_Output(0); delay1(500);
    SDA_Output(0); delay1(500);
    SCL_Output(1); delay1(500);
    SDA_Output(1); delay1(500);

}

//等待应答
unsigned char I2CWaitAck(void)
{
    unsigned short cErrTime = 5;
    SDA_Input_Mode();
    delay1(500);
    SCL_Output(1);delay1(500);
    while(SDA_Input())
    {
        cErrTime--;
        delay1(500);
        if (0 == cErrTime)
        {
            SDA_Output_Mode();
            I2CStop();
            return FAILURE;
        }
    }
    SDA_Output_Mode();
    SCL_Output(0);delay1(500);
    return SUCCESS;
}

//发送应答位
void I2CSendAck(void)
{
    SDA_Output(0);delay1(500);
    delay1(500);
    SCL_Output(1); delay1(500);
    SCL_Output(0); delay1(500);

}

//
void I2CSendNotAck(void)
{
    SDA_Output(1);
    delay1(500);
    SCL_Output(1); delay1(500);
    SCL_Output(0); delay1(500);

}

//通过I2C总线发送一个字节数据
void I2CSendByte(unsigned char cSendByte)
{
    unsigned char  i = 8;
    while (i--)
    {
        SCL_Output(0);delay1(500);
        SDA_Output(cSendByte & 0x80); delay1(500);
        cSendByte += cSendByte;
        delay1(500);
        SCL_Output(1);delay1(500);
    }
    SCL_Output(0);delay1(500);
}

//从I2C总线接收一个字节数据
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(500);
        delay1(500);
        SCL_Output(1);delay1(500);
        cR_Byte |=  SDA_Input();
    }
    SCL_Output(0);delay1(500);
    SDA_Output_Mode();
    return cR_Byte;
}

//I2C总线初始化
void i2c_init()
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_InitStructure.GPIO_Pin = SDA_Pin | SCL_Pin;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     // **

      GPIO_Init(I2C_PORT, &GPIO_InitStructure);

}
回复

使用道具 举报

该用户从未签到

1

主题

206

回帖

0

积分

二级逆天

积分
0

终身成就奖原创先锋奖

发表于 2021-2-8 13:12:10 | 显示全部楼层
回复

使用道具 举报

该用户从未签到

3

主题

3952

回帖

4658

积分

二级逆天

积分
4658

社区居民忠实会员最爱沙发终身成就奖优秀斑竹奖

QQ
发表于 2021-2-8 15:55:11 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

论坛开启做任务可以
额外奖励金币快速赚
积分升级了


Copyright ©2011-2024 NTpcb.com All Right Reserved.  Powered by Discuz! (NTpcb)

本站信息均由会员发表,不代表NTpcb立场,如侵犯了您的权利请发帖投诉

平平安安
TOP
快速回复 返回顶部 返回列表