网上写的直接抄发现没用,然后自己动手,修修改改就出来了,欢迎大神指点硬件图
io口用的是PC0,PC1,PC2,接了100k下拉电阻,晶振两端接了30pf的电容
ds1302.h
[mw_shl_code=c,true]#ifndef _DS1302_H
#define _DS1302_H
#include "sys.h"
#define DS1302_SDA_IN() {GPIOC->MODER &= 0x0000000f;}
#define DS1302_SDA_OUT() {GPIOC->MODER &= 0x0000000f;GPIOC->MODER |= 1<<4;}
#define RST PCout(0)
#define SCLK PCout(1)
#define DATA_OUT PCout(2)
#define DATA_IN PCin(2)
#define SECOND 0x80
#define MINUTE 0x82
#define HOUR 0x84
#define DATE 0x86
#define MONTH 0x88
#define WEEK 0x8A
#define YEAR 0x8C
#define ENABLE 0x8E
typedef struct
{
u8 second;
u8 minute;
u8 hour;
u8 date;
u8 month;
u8 week;
u8 year;
}TIME;
void DS1302_Init(void);
void Write1302(unsigned char dat);
void WriteSet1302(unsigned char Cmd,unsigned char dat);
unsigned char Read1302(void);
unsigned char ReadSet1302(unsigned char Cmd);
void Time_Init(void);
void Get_Time(TIME* time);
#endif
[/mw_shl_code]
ds1302.c
[mw_shl_code=c,true]#include "ds1302.h"
#include "delay.h"
void DS1302_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
//GPIOC0,C1,C23õê¼»ˉéèÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void Write1302(unsigned char dat)
{
unsigned char i;
SCLK=0;
delay_us(2);
for(i=0;i<8;i++)
{
DATA_OUT=dat&0x01;
delay_us(2);
SCLK=1;
delay_us(2);
SCLK=0;
dat>>=1;
}
}
void WriteSet1302(unsigned char Cmd,unsigned char dat)
{
RST=0;
SCLK=0;
RST=1;
delay_us(2);
Write1302(Cmd);
Write1302(dat);
SCLK=1;
RST=0;
}
unsigned char Read1302(void)
{
unsigned char i,dat;
delay_us(2);
for(i=0;i<8;i++)
{
dat>>=1;
if(DATA_IN==1)
dat|=0x80;
SCLK=1;
delay_us(2);
SCLK=0;
delay_us(2);
}
return dat;
}
unsigned char ReadSet1302(unsigned char Cmd)
{
unsigned char dat;
RST=0;
SCLK=0;
RST=1;
Write1302(Cmd);
dat=Read1302();
SCLK=1;
RST=0;
return dat;
}
void Time_Init()
{
WriteSet1302(SECOND,0);
WriteSet1302(MINUTE,0);
WriteSet1302(HOUR,6);
WriteSet1302(DATE,11);
WriteSet1302(MONTH,11);
WriteSet1302(WEEK,1);
WriteSet1302(YEAR,0x16);
}
void Get_Time(TIME* time)
{
u8 temp;
time->second=ReadSet1302(SECOND|1);
temp=time->second;
time->second=(temp&=0x0f)+(time->second>>4)*10;
time->minute=ReadSet1302(MINUTE|1);
temp=time->minute;
time->minute=(temp&=0x0f)+(time->minute>>4)*10;
time->hour=ReadSet1302(HOUR|1);
temp=time->hour;
time->hour=(temp&=0x0f)+(time->hour>>4)*10;
time->date=ReadSet1302(DATE|1);
temp=time->date;
time->date=(temp&=0x0f)+(time->date>>4)*10;
time->month=ReadSet1302(MONTH|1);
temp=time->month;
time->month=(temp&=0x0f)+(time->month>>4)*10;
time->week=ReadSet1302(WEEK|1);
temp=time->week;
time->week=(temp&=0x0f)+(time->week>>4)*10;
time->year=ReadSet1302(YEAR|1);
temp=time->year;
time->year=(temp&=0x0f)+(time->year>>4)*10;
}
[/mw_shl_code]
main.c
[mw_shl_code=c,true]#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "ds1302.h"
#include "lcd.h"
int main(void)
{
TIME time;
delay_init(168);
uart_init(115200);
DS1302_Init();
Time_Init();
while(1)
{
Get_Time(&time);
printf("%d",time.second);
delay_ms(1000);
}
}
[/mw_shl_code]
写得不好,欢迎指点,就我知道的缺点
①没有充电函数
②没有开机自检
③没有写保护
不过基本的读写时间是可以进行了
第一次发帖,向原子哥问好,原子哥爆个照呗~
一周热门 更多>