微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 模拟I2C

STM32 模拟I2C

时间:11-22 来源:互联网 点击:
使用STM32来访问I2C接口的铁电存储器,FM24CL16,2K字节
=================================
I2C的引脚配置:

/*ConfigureI2C1pins:SCLandSDA*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;
GPIO_Init(GPIOB,&GPIO_InitStructure);
=================================
/*******************************************************************************
*FileName:i2c_fram.h
*Author:MCDApplicationTeam
*Version:V2.0.1
*Date:06/13/2008
*Description:Headerfori2c_ee.cmodule
*******************************************************************************/
/*Definetopreventrecursiveinclusion------------------------------------*/
#ifndef__I2C_FRAM_H
#define__I2C_FRAM_H

/*Includes------------------------------------------------------------------*/
#include"stm32f10x_lib.h"

/*Exportedtypes------------------------------------------------------------*/
/*Exportedconstants--------------------------------------------------------*/

/*Exportedmacro------------------------------------------------------------*/
/*Exportedfunctions-------------------------------------------------------*/

boolI2C_FRAM_BufferWrite(u8*pBuffer,u16WriteAddr,u16NumByteToWrite);
boolI2C_FRAM_BufferRead(u8*pBuffer,u16ReadAddr,u16NumByteToRead);

#endif/*__I2C_FRAM_H*/

=================================
/*******************************************************************************
*FileName:i2c_fram.c
*Author:Appcat
*Version:V0.0.1
*Date:07/11/2009
*Description:Thisfileprovidesasetoffunctionsneededtomanagethe
*communicationbetweenI2CperipheralandI2CFM24CL16FRAM.
*******************************************************************************/

/*Includes------------------------------------------------------------------*/
#include"i2c_fram.h"

/*Privatetypedef-----------------------------------------------------------*/
/*Privatedefine------------------------------------------------------------*/
#defineI2C_Speed100000
#defineI2C1_SLAVE_ADDRESS70xA0
#defineI2C_PageSize256

#defineSCL_HGPIOB->BSRR=GPIO_Pin_6
#defineSCL_LGPIOB->BRR=GPIO_Pin_6

#defineSDA_HGPIOB->BSRR=GPIO_Pin_7
#defineSDA_LGPIOB->BRR=GPIO_Pin_7

#defineSCL_readGPIOB->IDR&GPIO_Pin_6
#defineSDA_readGPIOB->IDR&GPIO_Pin_7

/*Privatemacro-------------------------------------------------------------*/
/*Privatevariables---------------------------------------------------------*/
vu8FRAM_ADDRESS;

/*Privatefunctionprototypes-----------------------------------------------*/

/**/
voidI2C_delay(void)
{
u8i=150;//这里可以优化速度 ,经测试最低到5还能写入
while(i)
{
i--;
}
}

boolI2C_Start(void)
{
SDA_H;
SCL_H;
I2C_delay();
if(!SDA_read)returnFALSE; //SDA线为低电平则总线忙,退出
SDA_L;
I2C_delay();
if(SDA_read)returnFALSE; //SDA线为高电平则总线出错,退出
SDA_L;
I2C_delay();
returnTRUE;
}

voidI2C_Stop(void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SDA_H;
I2C_delay();
}

voidI2C_Ack(void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}

voidI2C_NoAck(void)
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}

boolI2C_WaitAck(void) //返回为:=1有ACK,=0无ACK
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
SCL_L;
returnFALSE;
}
SCL_L;
returnTRUE;
}

voidI2C_SendByte(u8SendByte)//数据从高位到低位//
{
u8i=8;
while(i--)
{
SCL_L;
I2C_delay();
if(SendByte&0x80)
SDA_H;
else
SDA_L;
SendByte<=1;
I2C_delay();
SCL_H;
I2C_delay();
}
SCL_L;
}

u8I2C_ReceiveByte(void)//数据从高位到低位//
{
u8i=8;
u8ReceiveByte=0;

SDA_H;
while(i--)
{
ReceiveByte<=1;
SCL_L;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
ReceiveByte|=0x01;
}
}
SCL_L;
returnReceiveByte;
}

boolI2C_FRAM_BufferWrite(u8*pBuffer,u16WriteAddr,u16NumByteToWrite)
{
u8Addr=0,count=0;

Addr=WriteAddr/I2C_PageSize;

count=WriteAddr%I2C_PageSize;

Addr=Addr<1;

Addr=Addr&0x0F;

FRAM_ADDRESS=I2C1_SLAVE_ADDRESS7|Addr;

if(!I2C_Start())returnFALSE;
I2C_SendByte(FRAM_ADDRESS);//设置器件地址+段地址
if(!I2C_WaitAck())
{
I2C_Stop();
returnFALSE;
}
I2C_SendByte(count);//设置段内地址
I2C_WaitAck();

while(NumByteToWrite--)
{
I2C_SendByte(*pBuffer);
I2C_WaitAck();
pBuffer++;
}
I2C_Stop();
//注意:因为这里要等待EEPROM写完,可以采用查询或延时方式(10ms)
//Systick_Delay_1ms(10);
returnTRUE;
}

//读出1串数据
boolI2C_FRAM_BufferRead(u8*pBuffer,u16WriteAddr,u16NumByteToRead)
{
u8Addr=0,count=0;

Addr=WriteAddr/I2C_PageSize;

count=WriteAddr%I2C_PageSize;

Addr=Addr<1;

Addr=Addr&0x0F;

FRAM_ADDRESS=I2C1_SLAVE_ADDRESS7|Addr;

if(!I2C_Start())returnFALSE;

I2C_SendByte(FRAM_ADDRESS);//设置器件地址+段地址

if(!I2C_WaitAck())
{
I2C_Stop();
returnFALSE;
}

I2C_SendByte(count);//设置低起始地址
I2C_WaitAck();
I2C_Start();
I2C_SendByte(FRAM_ADDRESS|0x01);
I2C_WaitAck();
while(NumByteToRead)
{
*pBuffer=I2C_ReceiveByte();
if(NumByteToRead==1)I2C_NoAck();
elseI2C_Ack();
pBuffer++;
NumByteToRead--;
}
I2C_Stop();
returnTRUE;
}

=================================

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top