看到很多小伙伴在测试这款模块
就测试了一下
我用的stm32 f103系列单片机
模块就是套包买的
#include "ds1307.h"
#include "sys.h"
#define uchar unsigned char
int year;
u8 month,day,hour,minutes,seconds;
//产生IIC 起始信号
void IIC_Start()
{
SDA_OUT();
IIC_SDA =1;
IIC_SCL = 1;
delay_us(4);
IIC_SDA = 0;
delay_us(4);
IIC_SCL = 0;
}
//产生IIC 停止信号
void IIC_Stop()
{
SDA_OUT();
IIC_SCL = 0;
IIC_SDA = 0;
delay_us(4);
IIC_SCL = 1;
delay_us(4);
IIC_SDA = 1;
}
//等待应答信号
//1 接收失败 0 接收成功
u8 IIC_Wait_Ack()
{
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()
{
IIC_SCL = 0;
SDA_OUT();
IIC_SDA = 0;
delay_us(2);
IIC_SCL =1;
delay_us(2);
IIC_SCL = 0;
}
void IIC_NAck()
{
IIC_SCL = 0;
SDA_OUT();
IIC_SDA =1;
delay_us(2);
IIC_SCL =1;
delay_us(2);
IIC_SCL = 0;
}
u8 IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL = 0; //时钟信号拉低 开始数据传输
for(t=0;t<8;t++)
{
IIC_SDA = (txd&0x80)>>7;
txd<<=1; delay_us(2);
IIC_SCL = 1;delay_us(2);
IIC_SCL = 0;delay_us(2);
}
}
u8 IIC_Read_Byte(unsigned char ack)
{
u8 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_NAck();
else
IIC_Ack();
return receive;
}
unsigned char DS1307_WriteOneByte(u8 addr,u8 data)
{
u8 temp;
//temp = (data/10*16)+(data%10);
temp = data/10;
temp<<=4;
temp = data%10+temp;
IIC_Start();
IIC_Send_Byte(0xd0);
IIC_Wait_Ack();
IIC_Send_Byte(addr);
IIC_Wait_Ack();
IIC_Send_Byte(temp);
IIC_Stop();
return 0;
}
u8 DS1307_ReadOneByte(u8 addr)
{
u8 temp,dat;
IIC_Start(); //IIC 硬件 线路准备
IIC_Send_Byte(0xd0); //发送设备地址 写命令
IIC_Wait_Ack(); //问答
IIC_Send_Byte(addr); //发送期间地址
IIC_Wait_Ack();
IIC_Stop();
IIC_Start();
IIC_Send_Byte(0xd1);
IIC_Wait_Ack();
dat = IIC_Read_Byte(addr);
IIC_NAck();
IIC_Stop();
//temp = dat/16;
//dat = dat%16;
//dat = dat+temp*10;
dat = (dat/16)*10+dat%16;
return (dat);
}
void shizhong_WriteOneByte(u8 addr,u8 data)
{
u8 temp;
temp = (data/10*16)+(data%10);
IIC_Start();
IIC_Send_Byte(0XD0);
IIC_Wait_Ack();
IIC_Send_Byte(addr);
IIC_Wait_Ack();
IIC_Send_Byte(temp);
IIC_Stop();
delay_ms(10);
}
u8 shizhong_ReadOneByte(u8 addr)
{
u8 temp,dat;
IIC_Start();
IIC_Send_Byte(0xd0);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0xd1);
IIC_Wait_Ack();
//temp = IIC_Read_Byte(addr);
temp = IIC_Read_Byte(0);
IIC_NAck();
IIC_Stop();
dat = (temp/16*10)+(temp%16);
return dat;
}
void DS1307_NOW_TIME()
{
seconds = DS1307_ReadOneByte(0x00);
minutes = DS1307_ReadOneByte(0x01);
hour = DS1307_ReadOneByte(0x02);
day = DS1307_ReadOneByte(0x04);
month = DS1307_ReadOneByte(0x05);
year = DS1307_ReadOneByte(0x06);
printf("%d:%d:%d %d-%d-%d\r\n",year, month, day, hour, minutes, seconds);
}
void DS1307_Read()
{
seconds = DS1307_ReadOneByte(0X00); //秒
minutes = DS1307_ReadOneByte(0X01); //分
hour = DS1307_ReadOneByte(0X02); //时
day = DS1307_ReadOneByte(0X04); //日期
month = DS1307_ReadOneByte(0X05); //月
year = DS1307_ReadOneByte(0X06)+2000; //年
printf("%d:%d:%d %d-%d-%d\r\n",year, month, day, hour, minutes, seconds);
}
void DS1307_GPIO_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2; //PB1 PB2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_1|GPIO_Pin_2);
//DS1307_WriteOneByte(u8 addr,u8 data)
// DS1307_WriteOneByte(0x00,0x1e); //
// DS1307_WriteOneByte(0x01,0x1f);
// DS1307_WriteOneByte(0x02,0x12);
// //DS1307_WriteOneByte(0x03,0x00);
// DS1307_WriteOneByte(0x04,0x1f);
// DS1307_WriteOneByte(0x05,0x03);
// DS1307_WriteOneByte(0x06,0x17);
shizhong_WriteOneByte(0x00,0x00);
shizhong_WriteOneByte(0x00,0x1e); //
shizhong_WriteOneByte(0x01,0x38);
shizhong_WriteOneByte(0x02,0x12);
shizhong_WriteOneByte(0x04,0x1f);
shizhong_WriteOneByte(0x05,0x03);
shizhong_WriteOneByte(0x06,0x17);
}
这是 C 文件 ,下面是 H 文件
#ifndef __DS1307_H
#define __DS1307_H
#include "sys.h"
#include "delay.h"
#include "usart.h"
//#define SDA(x) x?GPIO_ResetBits(GPIOB,GPIO_Pin_1): GPIO_SetBits(GPIOB,GPIO_Pin_1) //根据x,来让管脚B-8,输出0或者1
//#define SCL(x) x?GPIO_ResetBits(GPIOB,GPIO_Pin_2): GPIO_SetBits(GPIOB,GPIO_Pin_2) //根据x,来让管脚B-5,输出0或者1
//#define SDA_IN() {GPIOB->CRL &=0xffffff0f;GPIOB->CRL |=8<<4;}
//#define SDA_OUT() {GPIOB->CRL &=0xffffff0f;GPIOB->CRL |=3<<4;}
#define IIC_SCL PBout(2)
#define IIC_SDA PBout(1)
#define READ_SDA PBin(1)
//SDA PB1
//SCL PB2
void DS1307_GPIO_Init();
void DS1307_Read();
#define IIC_SDA_PORT GPIOB
#define IIC_SDA_IO GPIO_Pin_1
#define IIC_SDA_RCC RCC_APB2Periph_GPIOB
#define SDA_IN() {GPIOB->CRL &=0xffffff0f;GPIOB->CRL |=0x00000080;}
#define SDA_OUT() {GPIOB->CRL &=0xffffff0f;GPIOB->CRL |=0x00000030;}
extern int year;
extern u8 month,day,hour,minutes,seconds;
#endif
|