原创
【原创】stm32f的ht1380日历芯片驱动
ht1380和ds1302的时序基本一样,送给和我一样不太愿意用rtc的朋友!
#include "ht1380.h"
GPIO_InitTypeDef GPIO_InitStructure;
unsigned char week ;//?ü
void HT1380_write(unsigned char data);
int HT1380_read(void);
void dly_HT1380(void);
/*************?ìê±???óê±***********************************/
void dly_HT1380(void)
{
unsigned long i ;
for(i = 32;i > 0;i--);
}
/*************?òHT1380?DD′ò???×??ú*************************/
void HT1380_write(unsigned char data)
{
unsigned char i="0";
clr_HT1380_io();
for(i=8;i>0;i--)
{
dly_HT1380();
if(data&0x01)
{
set_HT1380_io();
}
dly_HT1380();
set_HT1380_sclk();
dly_HT1380();
clr_HT1380_sclk();
dly_HT1380();
clr_HT1380_io();
data>>=1;
}
}
/************************************************************
oˉêy1|?ü£o′óHT1380?D?áò???×??ú
ê?è?2?êy£o?T
·μ??2?êy£odata
*************************************************************/
int HT1380_read(void)
{
unsigned char data="0";
unsigned char i="0";
set_HT1380_io();
for(i=8;i>0;i--)
{
set_HT1380_sclk();
dly_HT1380();
data>>=1;
if(in_HT1380_io())
{
data |= 0x80;
}
else
{
data &= 0x07f;
}
clr_HT1380_sclk();
dly_HT1380();
}
return(data);
}
/**********′óHT1380?D?áè??úoíê±??******************************/
void HT1380_read_time(void)
{
clr_HT1380_rst();
dly_HT1380();
set_HT1380_rst(); //
dly_HT1380();
clr_HT1380_sclk();
dly_HT1380();
HT1380_write(0xbf);
dly_HT1380();
second="HT1380"_read(); //??
minute="HT1380"_read(); //·?
hour="HT1380"_read(); //ê±
day="HT1380"_read(); //è?
month="HT1380"_read();//??
week="HT1380"_read(); //?ü
year="HT1380"_read(); //?ê
dly_HT1380();
clr_HT1380_rst();
}
/**********?òHT1380D′è??úoíê±??******************************/
void HT1380_write_time(void)
{
clr_HT1380_rst();
dly_HT1380();
set_HT1380_rst(); //
dly_HT1380();
clr_HT1380_sclk();
dly_HT1380();
HT1380_write(0x8e);
HT1380_write(0x00);
dly_HT1380();
clr_HT1380_rst();
dly_HT1380();
set_HT1380_rst(); //
HT1380_write(0x80);
HT1380_write(0x00);
dly_HT1380();
clr_HT1380_rst();
dly_HT1380();
set_HT1380_rst(); //
HT1380_write(0xbe);
HT1380_write(second); // ??
HT1380_write(minute); //·?
HT1380_write(hour); //ê±
HT1380_write(day); //è?
HT1380_write(month); //??
HT1380_write(week); //?ü
HT1380_write(year); //?ê
HT1380_write(0x00);
dly_HT1380();
clr_HT1380_rst();
}
用户171514 2008-12-14 16:32