IIC.c
#include "Driver_I2C2.h"
#include "Delay.h"
#define I2C_DELAY Delay_us(10)
void Driver_I2C2_Init(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
GPIOB->CRH |= (GPIO_CRH_MODE10 | GPIO_CRH_MODE11 | GPIO_CRH_CNF10_0 | GPIO_CRH_CNF11_0);
GPIOB->CRH &= ~(GPIO_CRH_CNF10_1 | GPIO_CRH_CNF11_1);
}
void Driver_I2C2_Start(void)
{
SDA_HIGH;
SCL_HIGH;
I2C_DELAY;
SDA_LOW;
I2C_DELAY;
}
void Driver_I2C2_Stop(void)
{
SCL_HIGH;
SDA_LOW;
I2C_DELAY;
SDA_HIGH;
I2C_DELAY;
}
void Driver_I2C2_Ack(void)
{
SDA_HIGH;
SCL_LOW;
I2C_DELAY;
SDA_LOW;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
SCL_LOW;
I2C_DELAY;
SDA_HIGH;
I2C_DELAY;
}
void Driver_I2C2_NAck(void)
{
SDA_HIGH;
SCL_LOW;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
SCL_LOW;
I2C_DELAY;
}
uint8_t Driver_I2C2_WaitAck(void)
{
SDA_HIGH;
SCL_LOW;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
uint8_t ack = ACK;
if (READ_SDA)
{
ack = NACK;
}
SCL_LOW;
I2C_DELAY;
return ack;
}
void Driver_I2C_SendByte(uint8_t byte)
{
for (uint8_t i = 0; i < 8; i++)
{
SDA_LOW;
SCL_LOW;
I2C_DELAY;
if (byte & 0x80)
{
SDA_HIGH;
}
else
{
SDA_LOW;
}
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
SCL_LOW;
I2C_DELAY;
byte <<= 1;
}
}
uint8_t Driver_I2C_ReadByte(void)
{
uint8_t data = 0;
for (uint8_t i = 0; i < 8; i++)
{
SCL_LOW;
I2C_DELAY;
SCL_HIGH;
I2C_DELAY;
data <<= 1;
if (READ_SDA)
{
data |= 0x01;
}
SCL_LOW;
I2C_DELAY;
}
return data;
}
IIC.h
#ifndef __DRIVER_I2C2_H
#define __DRIVER_I2C2_H
#include "Delay.h"
#include "stm32f10x.h"
#include "Driver_USART.h"
#define ACK 0
#define NACK 1
#define SCL_HIGH (GPIOB->ODR |= GPIO_ODR_ODR10)
#define SCL_LOW (GPIOB->ODR &= ~GPIO_ODR_ODR10)
#define SDA_HIGH (GPIOB->ODR |= GPIO_ODR_ODR11)
#define SDA_LOW (GPIOB->ODR &= ~GPIO_ODR_ODR11)
#define READ_SDA (GPIOB->IDR & GPIO_IDR_IDR11)
void Driver_I2C2_Init(void);
void Driver_I2C2_Start(void);
void Driver_I2C2_Stop(void);
void Driver_I2C2_Ack(void);
void Driver_I2C2_NAck(void);
uint8_t Driver_I2C2_WaitAck(void);
void Driver_I2C_SendByte(uint8_t byte);
uint8_t Driver_I2C_ReadByte(void);
#endif
读写操作
Inf_W24C02.c
#include "Inf_W24C02.h"
void Inf_W24C02_Init(void)
{
Driver_I2C2_Init();
}
void Inf_W24C02_WriteByte(uint8_t innerAddr, uint8_t byte)
{
Driver_I2C2_Start();
Driver_I2C_SendByte(ADDR);
uint8_t ack = Driver_I2C2_WaitAck();
if (ack == ACK)
{
Driver_I2C_SendByte(innerAddr);
Driver_I2C2_WaitAck();
Driver_I2C_SendByte(byte);
Driver_I2C2_WaitAck();
Driver_I2C2_Stop();
}
Delay_ms(5);
}
uint8_t Inf_W24C02_ReadByte(uint8_t innerAddr)
{
Driver_I2C2_Start();
Driver_I2C_SendByte(ADDR);
Driver_I2C2_WaitAck();
Driver_I2C_SendByte(innerAddr);
Driver_I2C2_WaitAck();
Driver_I2C2_Start();
Driver_I2C_SendByte(ADDR + 1);
Driver_I2C2_WaitAck();
uint8_t byte = Driver_I2C_ReadByte();
Driver_I2C2_NAck();
Driver_I2C2_Stop();
return byte;
}
void Inf_W24C02_WriteBytes(uint8_t innerAddr, uint8_t *bytes, uint8_t len)
{
Driver_I2C2_Start();
Driver_I2C_SendByte(ADDR);
uint8_t ack = Driver_I2C2_WaitAck();
if (ack == ACK)
{
Driver_I2C_SendByte(innerAddr);
Driver_I2C2_WaitAck();
for (uint8_t i = 0; i < len; i++)
{
Driver_I2C_SendByte(bytes[i]);
Driver_I2C2_WaitAck();
}
Driver_I2C2_Stop();
}
Delay_ms(5);
}
void Inf_W24C02_ReadBytes(uint8_t innerAddr, uint8_t *bytes, uint8_t len)
{
Driver_I2C2_Start();
Driver_I2C_SendByte(ADDR);
Driver_I2C2_WaitAck();
Driver_I2C_SendByte(innerAddr);
Driver_I2C2_WaitAck();
Driver_I2C2_Start();
Driver_I2C_SendByte(ADDR + 1);
Driver_I2C2_WaitAck();
for (uint8_t i = 0; i < len; i++)
{
bytes[i] = Driver_I2C_ReadByte();
if (i < len - 1)
{
Driver_I2C2_Ack();
}
else
{
Driver_I2C2_NAck();
}
}
Driver_I2C2_Stop();
}
void Inf_W24C02_WriteBytesAutoPage(uint8_t innerAddr, uint8_t *bytes, uint8_t len)
{
if ((innerAddr % 16) + len <= 16)
{
Inf_W24C02_ReadBytes(innerAddr, bytes, len);
return;
}
}
Inf_W24C02.h
#ifndef __INF_W24C02_H
#define __INF_W24C02_H
#include "Driver_I2C2.h"
#include "string.h"
#define ADDR 0xA0
void Inf_W24C02_Init(void);
void Inf_W24C02_WriteByte(uint8_t innerAddr, uint8_t byte);
uint8_t Inf_W24C02_ReadByte(uint8_t innerAddr);
void Inf_W24C02_WriteBytes(uint8_t innerAddr, uint8_t *bytes, uint8_t len);
void Inf_W24C02_ReadBytes(uint8_t innerAddr, uint8_t *bytes, uint8_t len);
#endif