STM32基于标准库软件I2C方式操作AT24C32+DS1307时钟模块
- ✨申明:本文章仅发表在
CSDN
网站,任何其他网站,未注明来源,见此内容均为盗链爬取,请多多尊重和支持原创!
- 对于文中所提供的相关资源链接将作不定期更换。
- 说明:本人未开通打赏功能,谨防上当受骗!
- 相关篇《STM32基于STM32CubeMX DMA + EXTI读取DS1307数据》
本文是采用软件I2C方式,实现驱动的,如果移植到STM32其他型号单片机上,同是使用标准库下,移植难度不大,本工程是基于STM32F103VE.
-
本工程经实物验证没有问题。
-
DS1307时钟模块
![STM32基于标准库软件I2C方式操作AT24C32+DS1307时钟模块_第1张图片](http://img.e-com-net.com/image/info8/fb39bbcb6e59421894e4238e5a5d1a1c.jpg)
![STM32基于标准库软件I2C方式操作AT24C32+DS1307时钟模块_第2张图片](http://img.e-com-net.com/image/info8/3ec17836585243af8047c8e05fe01e4c.jpg)
-
串口打印信息:
![STM32基于标准库软件I2C方式操作AT24C32+DS1307时钟模块_第3张图片](http://img.e-com-net.com/image/info8/9ba88ac7120546d29d484b24eb59d197.png)
-
软件I2C引脚定义
#define IIC_SCL PCout(12)
#define IIC_SDA PCout(11)
驱动AT24Cxx功能
#include "24cxx.h"
#include "delay.h"
void AT24CXX_Init(void)
{
IIC_Init();
IIC_SDA = 1;
IIC_SCL = 1;
}
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
u8 temp = 0;
IIC_Start();
if (EE_TYPE > AT24C16)
{
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(u16 WriteAddr, u8 DataToWrite)
{
IIC_Start();
if (EE_TYPE > AT24C16)
{
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(u16 WriteAddr, u32 DataToWrite, u8 Len)
{
u8 t;
for (t = 0; t < Len; t++)
{
AT24CXX_WriteOneByte(WriteAddr + t, (DataToWrite >> (8 * t)) & 0xff);
}
}
u32 AT24CXX_ReadLenByte(u16 ReadAddr, u8 Len)
{
u8 t;
u32 temp = 0;
for (t = 0; t < Len; t++)
{
temp <<= 8;
temp += AT24CXX_ReadOneByte(ReadAddr + Len - t - 1);
}
return temp;
}
u8 AT24CXX_Check(void)
{
u8 temp;
temp = AT24CXX_ReadOneByte(255);
if (temp == 0x36)return 0;
else
{
AT24CXX_WriteOneByte(255, 0x36);
temp = AT24CXX_ReadOneByte(255);
if (temp == 0x36)return 0;
}
return 1;
}
void AT24CXX_Read(u16 ReadAddr, u8 *pBuffer, u16 NumToRead)
{
while (NumToRead)
{
*pBuffer++ = AT24CXX_ReadOneByte(ReadAddr++);
NumToRead--;
}
}
void AT24CXX_Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite)
{
while (NumToWrite--)
{
AT24CXX_WriteOneByte(WriteAddr, *pBuffer);
WriteAddr++;
pBuffer++;
}
}
#ifndef __24CXX_H
#define __24CXX_H
#include "myiic.h"
#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 EE_TYPE AT24C32
u8 AT24CXX_ReadOneByte(u16 ReadAddr);
void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite);
void AT24CXX_WriteLenByte(u16 WriteAddr,u32 DataToWrite,u8 Len);
u32 AT24CXX_ReadLenByte(u16 ReadAddr,u8 Len);
void AT24CXX_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite);
void AT24CXX_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead);
u8 AT24CXX_Check(void);
void AT24CXX_Init(void);
#endif
软件I2C驱动代码以及ds1307驱动
#include "myiic.h"
#include "delay.h"
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_11|GPIO_Pin_12);
}
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(1);
IIC_SCL=1;delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 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(2);
}
if (!ack)
IIC_NAck();
else
IIC_Ack();
return receive;
}
unsigned char Write1307(unsigned char add,unsigned char dat)
{
unsigned char temp;
temp=dat/10;
temp<<=4;
temp=dat%10+temp;
IIC_Start();
IIC_Send_Byte(0xD0);
IIC_Wait_Ack();
IIC_Send_Byte(add);
IIC_Wait_Ack();
IIC_Send_Byte(temp);
IIC_Stop();
return (0);
}
unsigned char Read1307(unsigned char add)
{
unsigned char temp;
unsigned char dat;
IIC_Start();
IIC_Send_Byte(0xD0);
IIC_Wait_Ack();
IIC_Send_Byte( add);
IIC_Wait_Ack();
IIC_Stop();
IIC_Start();
IIC_Send_Byte(0xD1);
IIC_Wait_Ack();
dat=IIC_Read_Byte(add);
IIC_NAck();
IIC_Stop();
temp=dat/16;
dat=dat%16;
dat=dat+temp*10;
return(dat);
}
#ifndef __MYIIC_H
#define __MYIIC_H
#include "sys.h"
#define SDA_IN() {GPIOC->CRH&=0XFFFF0FFF;GPIOC->CRH|=8<<12;}
#define SDA_OUT() {GPIOC->CRH&=0XFFFF0FFF;GPIOC->CRH|=3<<12;}
#define IIC_SCL PCout(12)
#define IIC_SDA PCout(11)
#define READ_SDA PCin(11)
void IIC_Init(void);
void IIC_Start(void);
void IIC_Stop(void);
void IIC_Send_Byte(u8 txd);
u8 IIC_Read_Byte(unsigned char ack);
u8 IIC_Wait_Ack(void);
void IIC_Ack(void);
void IIC_NAck(void);
void IIC_Write_One_Byte(u8 daddr,u8 addr,u8 data);
u8 IIC_Read_One_Byte(u8 daddr,u8 addr);
unsigned char Read1307(unsigned char add);
unsigned char Write1307(unsigned char add,unsigned char dat);
#endif
主程序代码
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "24cxx.h"
#include "myiic.h"
u8 WriteBuffer[] = "Hi!STM32f103ve";
#define WriteAddr ((u16)0x0000)
#define countof(a) (sizeof(a) / sizeof(*(a)))
#define BufferSize1 (countof(WriteBuffer)-1)
u8 Read_Buffer[BufferSize1];
int main(void)
{
int second, minute, hour, week, day, month, year;
delay_init();
NVIC_Configuration();
uart_init(9600);
AT24CXX_Init();
if (AT24CXX_Check())
{
printf("AT24Cxx检测不正常!\r\n");
delay_ms(500);
}
else
{
printf("AT24Cxx检测正常!\r\n");
AT24CXX_Write(WriteAddr,WriteBuffer,BufferSize1);
delay_ms(500);
AT24CXX_Read(WriteAddr,Read_Buffer,BufferSize1);
}
while (1)
{
second = Read1307(0x00);
minute = Read1307(0x01);
hour = Read1307(0x02);
week = Read1307(0x03);
day = Read1307(0x04);
month = Read1307(0x05);
year = Read1307(0x06);
printf("%02d-02%d-%02d %02d:%02d:%02d \r\n", year, month, day, hour, minute, second);
printf("AT24CXX_Read= %s \r\n", Read_Buffer);
delay_ms(1000);
}
}
工程源码
- ✨申明:本文章仅发表在
CSDN
网站,任何其他网站,未注明来源,见此内容均为盗链爬取,请多多尊重和支持原创!
- 对于文中所提供的相关资源链接将作不定期更换。
- 说明:本人未开通打赏功能,谨防上当受骗!
复制这段内容后打开百度网盘手机App,操作更方便哦
链接: https:
提取码: 3sax