资源简介
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);
相关资源
- ALIENTEK MINISTM32 实验3 串口实验
- STM32驱动OV2710
- TCS-34725 stm32程序
- rt_thread_nano_stm32f30x
- adc实验,用于在LCD模块上面显示ADC转
- 一对多 多字节的can总线通信
- stm32f4温度补偿超声波测距
- Qt_USB_I2C_MPU6050.rar
- STM32CubeMX培训课程
- I2C飞利浦官方详细说明文档
- Arduino I2Cdev库
- 基于STM32的医院点滴系统
- 带移植说明的xmodem源码STM32
- STM32F429的UCOSII工程
- STM32F103C8T6脚位分布图.pdf
- stm32的DXP元件库
- STM32 hx711电子称程序
- STM32+ch375读取U盘源代码
- STM32F10x_SPI硬件接口读写Flash25Q16.zip
- STM32CUBE芯片库.txt
- 红外收发模块-学习手册基于STM32
- SD2405+STM32 程序,已经测试通过
- 基于IAR的STM32F4项目模板
- BS8112A-3 BS8116A-3 I2C IO模拟程序
- AD7705初始化基于STM32
- CC2530-ADXL345 IIC通信
- CC2530-HMC5883L通过I2C通信
- MCP3421 i2c程序
- STM32驱动DA芯片DAC7617.doc
- i2c verilog
评论
共有 条评论