新聞中心

STM32 模擬I2C

作者: 時(shí)間:2016-11-22 來(lái)源:網(wǎng)絡(luò) 收藏
使用STM32來(lái)訪問(wèn)I2C接口的鐵電存儲(chǔ)器,F(xiàn)M24CL16,2K字節(jié)
=================================
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;//這里可以優(yōu)化速度 ,經(jīng)測(cè)試最低到5還能寫(xiě)入
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線為高電平則總線出錯(cuò),退出
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無(wú)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)//數(shù)據(jù)從高位到低位//
{
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)//數(shù)據(jù)從高位到低位//
{
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);//設(shè)置器件地址+段地址
if(!I2C_WaitAck())
{
I2C_Stop();
returnFALSE;
}
I2C_SendByte(count);//設(shè)置段內(nèi)地址
I2C_WaitAck();

while(NumByteToWrite--)
{
I2C_SendByte(*pBuffer);
I2C_WaitAck();
pBuffer++;
}
I2C_Stop();
//注意:因?yàn)檫@里要等待EEPROM寫(xiě)完,可以采用查詢或延時(shí)方式(10ms)
//Systick_Delay_1ms(10);
returnTRUE;
}


//讀出1串?dāng)?shù)據(jù)
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);//設(shè)置器件地址+段地址

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

I2C_SendByte(count);//設(shè)置低起始地址
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;
}

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


關(guān)鍵詞: STM32模擬I2

評(píng)論


技術(shù)專區(qū)

關(guān)閉