资源简介
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);
相关资源
- STM32F103RC+ADC+DMA多通道采样LCD显示
- I2C读写AT24C02 基于STM32F103 cube116540
- 基于stm32f103ve的程序——跑马灯实验
- 基于STM32RCT6的步进电机驱动程序
- stm32f407上的两个can发送和接收例程
- STM32 led 时钟
- STM32 2.4G通信例程
- 直流无刷电机方波驱动 stm32 例程代码
- STM32中文资料
- STM32蓝牙和串口程序
- STM32f103超声波模块例程
- stm32f103c8t6 4 oled.rar
- stm32f030 IAP Demo(原创)
- STM32基于rt_thread操作系统的SDHC卡文件
- 用DIO 做的I2C程式
- NRF24L01实现51与STM32双向通讯
- STM32F103 串口程序(完整版)
- stm32 ds18b20 温度传感器 测试通过
- stm32官方例程
- STM32F103定时器中断程序
- [免费]基于stm32f103ze 的OLED驱动代码
- STM32F103RBT6驱动UC1698控制芯片的160160黑
- STM32F103 DS18B20 V3.5.0固件库驱动程序工
- STM32定时器使用入门。看了这个程序会
- SIM908 SDIO FSMC STM32 FIFO
- STM32F103 CC2500完整驱动(模拟SPI)
- I2C总线接口设计
- AD7606采集程序
- 8051F040SMBUS多机通信
- stm32 用SPI 方式读写 SDHC
评论
共有 条评论