AT24C02相关资料这里不做介绍,可以参考别人的文章去了解,本问针对STM32初学者,快速入手基本操作,直接上干货,有时间再慢慢补充。 【本代码是基于正点原子的驱动基础上利用LL库修改的仅供学习参考,选用芯片不一样,本文选的是STM32f103c8t6】 驱动AT24C02需要的驱动文件 1). LL库的延时函数; 2). i2c驱动; 3). AT24C02驱动; 下面附上本人配置的Cubemx:
- Cubemx相关设置
a) 时钟树配置: b) 模拟I2C配置的芯片引脚(根据自己的芯片自行定义即可): c) 生成代码的时候记得选择LL库 - 相关代码
a) 延时函数(记得在主函数里面初始化)
#include "delay.h"
#include "main.h"
void delay_init(uint8_t SYSCLK)
{
SysTick->CTRL&=0xfffffffb;
fac_us=SYSCLK/8;
fac_ms=(uint16_t)fac_us*1000;
}
void delay_nms(uint16_t nms)
{
uint32_t temp;
SysTick->LOAD=(uint32_t)nms*fac_ms;
SysTick->VAL =0x00;
SysTick->CTRL=0x01 ;
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16)));
SysTick->CTRL=0x00;
SysTick->VAL =0X00;
}
void delay_us(uint32_t Nus)
{
uint32_t temp;
SysTick->LOAD=Nus*fac_us;
SysTick->VAL=0x00;
SysTick->CTRL=0x01 ;
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16)));
SysTick->CTRL=0x00;
SysTick->VAL =0X00;
}
void delay_ms(uint16_t time_cn)
{
uint32_t temp = time_cn*10000;
while(temp--);
}
b)i2c驱动代码
#include "24cxx.h"
#include "delay.h"
#include "myiic.h"
#include "stm32f1xx.h"
#include "delay.h"
void IIC_Start(void)
{
SDA_OUT();
AT24C02_SDA(1);
AT24C02_SCL(1);
delay_us(4);
AT24C02_SDA(0);
delay_us(4);
AT24C02_SCL(0);
}
void IIC_Stop(void)
{
SDA_OUT();
AT24C02_SCL(0);
AT24C02_SDA(0);
delay_us(4);
AT24C02_SCL(1);
delay_us(4);
AT24C02_SDA(1);
}
uint8_t IIC_Wait_Ack(void)
{
uint8_t ucErrTime=0;
SDA_IN();
AT24C02_SDA(1);delay_us(1);
AT24C02_SCL(1);delay_us(1);
while(LL_GPIO_IsInputPinSet(AT24C02_SDA_GPIO_Port,AT24C02_SDA_Pin))
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
AT24C02_SCL(0);
return 0;
}
void IIC_Ack(void)
{
AT24C02_SCL(0);
SDA_OUT();
AT24C02_SDA(0);
delay_us(2);
AT24C02_SCL(1);
delay_us(2);
AT24C02_SCL(0);
}
void IIC_NAck(void)
{
AT24C02_SCL(0);
SDA_OUT();
AT24C02_SDA(1);
delay_us(2);
AT24C02_SCL(1);
delay_us(2);
AT24C02_SCL(0);
}
void IIC_Send_Byte(uint8_t txd)
{
uint8_t t;
SDA_OUT();
AT24C02_SCL(0);
for(t=0;t<8;t++)
{
if((txd&0x80)>>7)
{
AT24C02_SDA(1);
}
else
{
AT24C02_SDA(0);
}
txd<<=1;
delay_us(2);
AT24C02_SCL(1);
delay_us(2);
AT24C02_SCL(0);
delay_us(2);
}
}
uint8_t IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN();
for(i=0;i<8;i++ )
{
AT24C02_SCL(0);
delay_us(2);
AT24C02_SCL(1);
receive<<=1;
if(LL_GPIO_IsInputPinSet(AT24C02_SDA_GPIO_Port,AT24C02_SDA_Pin))receive++;
delay_us(1);
}
if (!ack)
IIC_NAck();
else
IIC_Ack();
return receive;
}
i2c头文件定义
#define SDA_IN() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=((uint32_t)8<<4*7);}
#define SDA_OUT() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=((uint32_t)3<<4*7);}
#define AT24C02_SDA(a) if(a) \
LL_GPIO_SetOutputPin(AT24C02_SDA_GPIO_Port,AT24C02_SDA_Pin); \
else \
LL_GPIO_ResetOutputPin(AT24C02_SDA_GPIO_Port,AT24C02_SDA_Pin);
#define AT24C02_SCL(a) if(a) \
LL_GPIO_SetOutputPin(AT24C02_SCL_GPIO_Port,AT24C02_SCL_Pin); \
else \
LL_GPIO_ResetOutputPin(AT24C02_SCL_GPIO_Port,AT24C02_SCL_Pin);
void IIC_Start(void);
void IIC_Stop(void);
void IIC_Send_Byte(uint8_t txd);
uint8_t IIC_Read_Byte(unsigned char ack);
uint8_t IIC_Wait_Ack(void);
void IIC_Ack(void);
void IIC_NAck(void);
void IIC_Write_One_Byte(uint8_t daddr,uint8_t addr,uint8_t data);
uint8_t IIC_Read_One_Byte(uint8_t daddr,uint8_t addr);
c) 24C02驱动
uint8_t AT24CXX_ReadOneByte(uint16_t ReadAddr)
{
uint8_t temp=0;
IIC_Start();
if(EE_TYPE>AT24C02)
{
IIC_Send_Byte(0XA0);
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr>>8);
}else IIC_Send_Byte(0XA0+((ReadAddr/256)<<1));
IIC_Wait_Ack();
IIC_Send_Byte(ReadAddr%256);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0XA1);
IIC_Wait_Ack();
temp=IIC_Read_Byte(0);
IIC_Stop();
return temp;
}
void AT24CXX_WriteOneByte(uint16_t WriteAddr,uint8_t DataToWrite)
{
IIC_Start();
if(EE_TYPE>AT24C02)
{
IIC_Send_Byte(0XA0);
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr>>8);
}else IIC_Send_Byte(0XA0+((WriteAddr/256)<<1));
IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr%256);
IIC_Wait_Ack();
IIC_Send_Byte(DataToWrite);
IIC_Wait_Ack();
IIC_Stop();
delay_ms(10);
}
void AT24CXX_WriteLenByte(uint16_t WriteAddr,uint32_t DataToWrite,uint8_t Len)
{
uint8_t t;
for(t=0;t<Len;t++)
{
AT24CXX_WriteOneByte(WriteAddr+t,(DataToWrite>>(8*t))&0xff);
}
}
uint32_t AT24CXX_ReadLenByte(uint16_t ReadAddr,uint8_t Len)
{
uint8_t t;
uint32_t temp=0;
for(t=0;t<Len;t++)
{
temp<<=8;
temp+=AT24CXX_ReadOneByte(ReadAddr+Len-t-1);
}
return temp;
}
uint8_t AT24CXX_Check(void)
{
uint8_t temp;
temp=AT24CXX_ReadOneByte(255);
if(temp==0X55)return 0;
else
{
AT24CXX_WriteOneByte(255,0X55);
temp=AT24CXX_ReadOneByte(255);
if(temp==0X55)return 0;
}
return 1;
}
void AT24CXX_Read(uint16_t ReadAddr,uint8_t *pBuffer,uint16_t NumToRead)
{
while(NumToRead)
{
*pBuffer++=AT24CXX_ReadOneByte(ReadAddr++);
NumToRead--;
}
}
void AT24CXX_Write(uint16_t WriteAddr,uint8_t *pBuffer,uint16_t NumToWrite)
{
while(NumToWrite--)
{
AT24CXX_WriteOneByte(WriteAddr,*pBuffer);
WriteAddr++;
pBuffer++;
}
}
AT24C02头文件
#define AT24C01 127
#define AT24C02 255
#define AT24C04 511
#define AT24C08 1023
#define AT24C16 2047
#define AT24C32 4095
#define AT24C64 8191
#define AT24C128 16383
#define AT24C256 32767
#define AT24C512 65535
#define EE_TYPE AT24C02
uint8_t AT24CXX_ReadOneByte(uint16_t ReadAddr);
void AT24CXX_WriteOneByte(uint16_t WriteAddr,uint8_t DataToWrite);
void AT24CXX_WriteLenByte(uint16_t WriteAddr,uint32_t DataToWrite,uint8_t Len);
uint32_t AT24CXX_ReadLenByte(uint16_t ReadAddr,uint8_t Len);
void AT24CXX_Write(uint16_t WriteAddr,uint8_t *pBuffer,uint16_t NumToWrite);
void AT24CXX_Read(uint16_t ReadAddr,uint8_t *pBuffer,uint16_t NumToRead);
uint8_t AT24CXX_Check(void);
- 整理好相关的代码,就可以按照自己的想法编程序了,大家可以试试。
|