/********* file name: i2c_ee.h**********/
/* Define to prevent recursive inclusion------------------------------------*/
#ifndef __i2c_ee_h
#define __i2c_ee_h
/* Includes------------------------------------------------------------------*/
#include "stm32f10x.h"
/* Exported Macro------------------------------------------------------------*/
#define ADDR_24CXX 0xA0
#define SCLH GPIOB->BSRR = gpio_pin_6
#define SCLL GPIOB->BRR = gpio_pin_6
#define Sdah GPIOB->BSRR = gpio_pin_7
#define Sdal GPIOB->BRR = gpio_pin_7
#define Sclread GPIOB->IDR & Gpio_pin_6
#define Sdaread GPIOB->IDR & gpio_pin_7
/* Exported Functions-------------------------------------------------------*/
void I2c_ee_init (void);
uint8_t i2c_ee_bufferwrite (uint8_t *psrc_data,uint8_t adr,uint8_t nbyte);
uint8_t i2c_ee_bufferread (uint8_t *pdin_data,uint8_t adr,uint8_t nbyte);
#endif/* __i2c_ee_h */
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/********* file name: i2c_ee.c**********/
#include "i2c_ee.h"
Enum enum_twi_reply
{
Twi_nack=0
, twi_ack=1
};
Enum Enum_twi_bus_state
{
Twi_ready=0
, Twi_bus_busy=1
, twi_bus_error=2
};
void I2c_ee_init (void)
{
Gpio_inittypedef gpio_initstructure;
Rcc_apb2periphclockcmd (Rcc_apb2periph_gpiob, ENABLE);
Configure i2c1 PINS:SCL and SDA
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);
}
void Twi_delay (void)
{
uint8_t i=10;//i=10 delay 1.5us//Here you can optimize the speed, tested as low as 5 to write
while (i--);
}
/**************************************************************************
Delay
MS: Number of milliseconds to delay
cyclecounter/72000000
************************************************************************ /
void delayms (uint16_t ms)
{
uint16_t iq0;
uint16 _t iq1;
for (iq0 = ms; iq0 > 0; iq0--)
{
for (iq1 = 11998; iq1 > 0; iq1--);//((6*iq1+9 ) *iq0+15)/72000000
}
}
uint8_t Twi_start (void)
{
Sdah;
SCLH;
Twi_delay ();
if (! Sdaread) return twi_bus_busy; SDA line low level bus busy, exit
Sdal;
Twi_delay ();
if (sdaread) return twi_bus_error; SDA line is high, bus error, exit
SCLL;
Twi_delay ();
return twi_ready;
}
/*void twi_stop (void)
{
SCLL;
Twi_delay ();
Sdal;
Twi_delay ();
SCLH;
Twi_delay ();
Sdah;
Twi_delay ();
}*/
void Twi_stop (void)
{
Sdal;
SCLL;
Twi_delay ();
SCLH;
Twi_delay ();
Sdah;
Twi_delay ();
}
void Twi_ack (void)
{
SCLL;
Twi_delay ();
Sdal;
Twi_delay ();
SCLH;
Twi_delay ();
SCLL;
Twi_delay ();
}
void Twi_noack (void)
{
SCLL;
Twi_delay ();
Sdah;
Twi_delay ();
SCLH;
Twi_delay ();
SCLL;
Twi_delay ();
}
uint8_t twi_waitack (void)//return: =1 with ack,=0 no ACK
{
SCLL;
Twi_delay ();
Sdah;
Twi_delay ();
SCLH;
Twi_delay ();
if (Sdaread)
{
SCLL;
return 0;
}
SCLL;
return 1;
}
void Twi_sendbyte (uint8_t sendbyte)//data from high to low//
{
uint8_t i=8;
while (i--)
{
SCLL;
Twi_delay ();
if (sendbyte&0x80)
Sdah;
Else
Sdal;
sendbyte<<=1;
Twi_delay ();
SCLH;
Twi_delay ();
}
SCLL;
}
uint8_t twi_receivebyte (void)//data from high to low//
{
uint8_t i=8;
uint8_t receivebyte=0;
Sdah;
while (i--)
{
Receivebyte <<= 1;
SCLL;
Twi_delay ();
SCLH;
Twi_delay ();
if (Sdaread)
{
Receivebyte |= 0x01;
}
}
SCLL;
return receivebyte;
}
Return: 3 Write successful, 0 write device address error, 1 bus busy, 2 error
Write 1-byte data sendbyte: Data to be written Writeaddress: address to write to
uint8_t Twi_writebyte (uint8_t sendbyte, uint8_t writeaddress)
{
uint8_t i;
Uint16_t J;
i = Twi_start ();
if (i)
return i;
Twi_sendbyte (Addr_24cxx & 0xFE);//write device Address write: Address the lowest bit is 0, read: Address the lowest bit is 1
if (! Twi_waitack ())
{
Twi_stop ();
return 0;
}
Twi_sendbyte (writeaddress); Set Start Address
Twi_waitack ();
Twi_sendbyte (Sendbyte); Write Data
Twi_waitack ();
Twi_stop ();
Note: Because here to wait for the EEPROM to finish writing, you can use the query or delay mode (10MS)
Delayms (12); Write delay 12ms write period greater than 10ms
return 3;
}
//return: 0 Write device address error, 1 bus busy, 2 error,
//read out 1 bytes of data
//readaddress: Address to be read
uint8_t twi_readbyte (uint8_t readaddress)
{
uint8_t i,temp;
i = Twi_start ();
if (i)
& nbsp; return i;
twi_sendbyte ((Addr_24cxx & 0xFE));//write the device address, perform a pseudo-write operation first
if (! Twi_waitack ())
{
twi_stop ();
return 0;
}
Twi_sendbyte (readaddress); Set Start Address
Twi_waitack ();
Twi_start ();
Twi_sendbyte ((Addr_24cxx & 0xFE) |0x01); Read device Address write: Address the lowest bit is 0, read: Address the lowest bit is 1
Twi_waitack ();
*pdat = Twi_receivebyte ();
temp = Twi_receivebyte ();
Twi_noack ();
Twi_stop ();
Return temp;//returned if the 0,1,2 is the same as the error code, and then consider
}
/***************************************************************************
Write multiple bytes to the 24c256
Psrc_data: Pointer to a data array to write to
The first address in the adr:24c256 to write data to
Nbyte: Number of bytes written
Return value: 0: Execution completed; 1: Execution error occurred
Formal parameters: C02 only one address adr;c256 has a high address of HADR and a low address ladr
***************************************************************************/
uint8_t i2c_ee_bufferwrite (uint8_t *psrc_data,uint8_t adr,uint8_t nbyte)
{
uint8_t i;
for (; nbyte!=0;nbyte--)
{
i = Twi_start ();
if (i)
return i;
Twi_sendbyte (Addr_24cxx & 0xFE);//write device Address
if (! Twi_waitack ())
{
Twi_stop ();
return 0;
}
Twi_sendbyte (ADR); Set Start Address
Twi_waitack ();
Twi_sendbyte (*psrc_data); Write Data
Twi_waitack ();
psrc_data++; Pointer to the data to be written plus 1
adr++; Add 1 to the operating address of the 24C08
Twi_stop ();
Note: Because here to wait for the EEPROM to finish writing, you can use the query or delay mode (10MS)
Delayms (12); Write delay 12ms write period greater than 10ms
}
return 0;
}
/***************************************************************************
Read multiple bytes from 24C02
Pdin_ Data: Pointer to the array to save the read data to
ADR:24C02 the first address of the data to be read
Nbyte: Number of bytes read
return value: 0: Execution completed; 1: Error occurred
****************** /
uint8_t i2c_ee_bufferread (uint8_t *pdin_data,uint8 _t adr,uint8_t nbyte)
{
uint8_t i;
i = Twi_start ();
if (i)
return i;
twi_sendbyte ((Addr_24cxx & 0xFE));//write the device address, perform a pseudo-write operation first
if (! Twi_waitack ())
{
twi_stop ();
return 0;
}
Twi_sendbyte (ADR); Set Start Address
Twi_waitack ();
Twi_start ();
Twi_sendbyte ((Addr_24cxx & 0xFE) |0x01); Read device Address write: Address the lowest bit is 0, read: Address the lowest bit is 1
Twi_waitack ();
while (nbyte!=1)//Read in front (nbyte-1) bytes
{
*pdin_data = Twi_receivebyte (); The loop reads the data from the 24C02 and stores it in the memory referred to by Pdin_data.
Twi_ack (); IIC response
pdin_data++; Memory pointer to store read-in data plus 1
nbyte--; The remaining bytes to be read minus 1
};
*pdin_data = Twi_receivebyte (); Read in last Byte
Twi_noack (); IIC No answer operation
Twi_stop ();
return 0;
}
/*
void Twi_24cxx_write (uint8_t* pdat, uint8_t naddr, uint8_t nlen)
{
uint16_t i;
for (i=0;i<nlen;i++)
{
Twi_writebyte (* (pdat+i), naddr+i);
}
}
void Twi_24cxx_read (uint8_t* pdat, uint8_t naddr, uint8_t nlen)
{
uint16_t i;
for (i=0; i<nlen; i++)
* (pdat+i) = Twi_readbyte (naddr+i);
}
*/