写一个stm32f103使用软件i2c方式驱动at24c02的代码
时间: 2024-01-14 14:05:11 浏览: 100
很高兴为您解答这个问题!以下是使用软件I2C方式驱动AT24C02的STM32F103代码:
```
#include "stm32f10x.h"
#include "delay.h"
#define SDA GPIO_Pin_6
#define SCL GPIO_Pin_7
#define SDA_IN() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(uint32_t)8<<28;}
#define SDA_OUT() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(uint32_t)3<<28;}
#define IIC_SCL PBout(7) //SCL
#define IIC_SDA PBout(6) //SDA
#define READ_SDA PBin(6) //输入SDA
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
IIC_SDA = 1;
IIC_SCL = 1;
}
void IIC_Start(void)
{
SDA_OUT();
IIC_SDA = 1;
IIC_SCL = 1;
delay_us(4);
IIC_SDA = 0;
delay_us(4);
IIC_SCL = 0;
}
void IIC_Stop(void)
{
SDA_OUT();
IIC_SCL = 0;
IIC_SDA = 0;
delay_us(4);
IIC_SCL = 1;
IIC_SDA = 1;
delay_us(4);
}
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime = 0;
SDA_IN();
IIC_SDA = 1;
delay_us(4);
IIC_SCL = 1;
delay_us(4);
while (READ_SDA)
{
ucErrTime++;
if (ucErrTime > 250)
{
IIC_Stop();
return 1;
}
delay_us(1);
}
IIC_SCL = 0;
return 0;
}
void IIC_Ack(void)
{
IIC_SCL = 0;
SDA_OUT();
IIC_SDA = 0;
delay_us(2);
IIC_SCL = 1;
delay_us(2);
IIC_SCL = 0;
}
void IIC_NAck(void)
{
IIC_SCL = 0;
SDA_OUT();
IIC_SDA = 1;
delay_us(2);
IIC_SCL = 1;
delay_us(2);
IIC_SCL = 0;
}
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL = 0;
for (t = 0; t < 8; t++)
{
if ((txd & 0x80) >> 7)
IIC_SDA = 1;
else
IIC_SDA = 0;
txd <<= 1;
delay_us(2);
IIC_SCL = 1;
delay_us(2);
IIC_SCL = 0;
delay_us(2);
}
}
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i, receive = 0;
SDA_IN();
for (i = 0; i < 8; i++)
{
IIC_SCL = 0;
delay_us(2);
IIC_SCL = 1;
receive <<= 1;
if (READ_SDA)
receive++;
delay_us(1);
}
if (ack)
IIC_Ack();
else
IIC_NAck();
return receive;
}
void AT24C02_WriteOneByte(u16 addr, u8 data)
{
IIC_Start();
IIC_Send_Byte(0xA0);
IIC_Wait_Ack();
IIC_Send_Byte(addr >> 8);
IIC_Wait_Ack();
IIC_Send_Byte(addr & 0xFF);
IIC_Wait_Ack();
IIC_Send_Byte(data);
IIC_Wait_Ack();
IIC_Stop();
delay_ms(5);
}
u8 AT24C02_ReadOneByte(u16 addr)
{
u8 temp;
IIC_Start();
IIC_Send_Byte(0xA0);
IIC_Wait_Ack();
IIC_Send_Byte(addr >> 8);
IIC_Wait_Ack();
IIC_Send_Byte(addr & 0xFF);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0xA1);
IIC_Wait_Ack();
temp = IIC_Read_Byte(0);
IIC_Stop();
return temp;
}
int main(void)
{
IIC_Init();
while (1)
{
AT24C02_WriteOneByte(0x00, 0x01); // 在地址0x00写入数据0x01
delay_ms(200);
AT24C02_WriteOneByte(0x01, 0x02); // 在地址0x01写入数据0x02
delay_ms(200);
AT24C02_WriteOneByte(0x02, 0x03); // 在地址0x02写入数据0x03
delay_ms(200);
u8 val_0x00 = AT24C02_ReadOneByte(0x00); // 从地址0x00读取一个字节
u8 val_0x01 = AT24C02_ReadOneByte(0x01); // 从地址0x01读取一个字节
u8 val_0x02 = AT24C02_ReadOneByte(0x02); // 从地址0x02读取一个字节
printf("val_0x00=%d, val_0x01=%d, val_0x02=%d\n", val_0x00, val_0x01, val_0x02); // 打印读取的结果
delay_ms(1000);
}
}
```
希望能够帮助您!如果您有更多问题,请随时和我联系。
阅读全文