資源簡介
STM32F4讀寫SD2405實時時鐘程序 親測可正常讀寫 不卡死總線 不需要重復初始化 可連續讀寫 24小時制計時
代碼片段和文件信息
#include?
//?STM32F407VGT6?or?STM32F407VET6
//?SD2405?use?I2C1
//?I2C1_SCL?->?GPIOB_Pin6
//?I2C1_SDA?->?GPIOB_Pin7
unsigned?int?tm_sec;???/*?seconds?after?the?minute?0?to?60?*/
unsigned?int?tm_min;???/*?minutes?after?the?hour?0?to?59?*/
unsigned?int?tm_hour;??/*?hours?since?midnight?0?to?23?*/
unsigned?int?tm_mday;??/*?day?of?the?month?1?to?31?*/
unsigned?int?tm_mon;???/*?months?since?January?0?to?11?*/
unsigned?int?tm_year;??/*?years?since?1900?*/
unsigned?int?tm_wday;??/*?days?since?Sunday?0?to?6?*/
/**
??*?@brief??Converts?a?2?digit?decimal?to?BCD?format.
??*?@param??Value:?Byte?to?be?converted.
??*?@retval?Converted?byte
??*/
static?uint8_t?RTC_ByteToBcd2(uint8_t?Value)
{
uint8_t?bcdhigh?=?0;
while?(Value?>=?10)
{
bcdhigh++;
Value?-=?10;
}
return??((uint8_t)(bcdhigh?<4)?|?Value);
}
/**
??*?@brief??Convert?from?2?digit?BCD?to?Binary.
??*?@param??Value:?BCD?value?to?be?converted.
??*?@retval?Converted?word
??*/
static?uint8_t?RTC_Bcd2ToByte(uint8_t?Value)
{
uint8_t?tmp?=?0;
tmp?=?((uint8_t)(Value?&?(uint8_t)0xF0)?>>?(uint8_t)0x4)?*?10;
return?(tmp?+?(Value?&?(uint8_t)0x0F));
}
/******************************************************************************
*??Set?Time
******************************************************************************/
void?SetTime(void)
{
????unsigned?char?isj[7];
if ( (tm_sec??<=?60)
&& (tm_min???60)
&& (tm_hour??24)
&& (tm_mday?<=?31)?&&?(tm_mday?>=?1)
&& (tm_mon??<=?12)?&&?(tm_mon??>=?1)
&& (tm_year??100)
&& (tm_wday??7)
)
{
sj[0]?=?RTC_ByteToBcd2(tm_sec); /*?seconds?after?the?minute?0?to?60?*/
sj[1]?=?RTC_ByteToBcd2(tm_min); /*?minutes?after?the?hour?0?to?59?*/
sj[2]?=?RTC_ByteToBcd2(tm_hour)|0x80; /*?hours?since?midnight?0?to?23?*/
sj[4]?=?RTC_ByteToBcd2(tm_mday); /*?day?of?the?month?1?to?31?*/
sj[5]?=?RTC_ByteToBcd2(tm_mon); /*?months?since?January?0?to?11?*/
sj[6]?=?RTC_ByteToBcd2(tm_year); /*?years?since?1900?*/
sj[3]?=?RTC_ByteToBcd2(tm_wday); /*?days?since?Sunday?0?to?6?*/
I2C_GenerateSTART(I2C1ENABLE);
while(!I2C_CheckEvent(I2C1I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C10x65I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1?I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C10x10);
while(!I2C_CheckEvent(I2C1?I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C10x80);
while(!I2C_CheckEvent(I2C1?I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1ENABLE);
while(!I2C_CheckEvent(I2C1I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C10x65I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1?I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C10x0f);
while(!I2C_CheckEvent(I2C1?I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C10x84);
while(!I2C_CheckEvent(I2C1?I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1ENABLE);
評論
共有 條評論