CSDN
网站,任何其他网站,未注明来源,见此内容均为盗链和爬取,请多多尊重和支持原创!https://github.com/eepj/DS1307_for_STM32_HAL
基于STM32CubeMX配置工程,当然不局限与STM32其他型号的芯片的使用,只要是stm32芯片都可以使用该源文件进行驱动,方便适配移植,减少不必要的重复开发工作。
以上配置可以根据个人需求进行引脚切换,以及外设选择。
MicroLib
选项。#include "stdio.h"
/*可调用printf*/
int fputc(int ch,FILE *f)
{
/*&huart1指的是串口1,如果用别的串口就修改数字*/
HAL_UART_Transmit(&huart1 , (uint8_t *)&ch , 1 , 1000);
return ch;
}
DS1307_SetTimeZone(+8, 00);
DS1307_SetDate(31);
DS1307_SetMonth(3);
DS1307_SetYear(2023);
DS1307_SetDayOfWeek(5);
DS1307_SetHour(11);
DS1307_SetMinute(40);
DS1307_SetSecond(30);
https://github.com/eepj/DS1307_for_STM32_HAL
ds1307_for_stm32_hal.h
/* An STM32 HAL library written for the DS1307 real-time clock IC. */
/* Library by @eepj www.github.com/eepj */
#ifndef DS1307_FOR_STM32_HAL_H
#define DS1307_FOR_STM32_HAL_H
#include "main.h"
/*----------------------------------------------------------------------------*/
#define DS1307_I2C_ADDR 0x68
#define DS1307_REG_SECOND 0x00
#define DS1307_REG_MINUTE 0x01
#define DS1307_REG_HOUR 0x02
#define DS1307_REG_DOW 0x03
#define DS1307_REG_DATE 0x04
#define DS1307_REG_MONTH 0x05
#define DS1307_REG_YEAR 0x06
#define DS1307_REG_CONTROL 0x07
#define DS1307_REG_UTC_HR 0x08
#define DS1307_REG_UTC_MIN 0x09
#define DS1307_REG_CENT 0x10
#define DS1307_REG_RAM 0x11
#define DS1307_TIMEOUT 1000
/*----------------------------------------------------------------------------*/
extern I2C_HandleTypeDef *_ds1307_ui2c;
typedef enum DS1307_Rate{
DS1307_1HZ, DS1307_4096HZ, DS1307_8192HZ, DS1307_32768HZ
} DS1307_Rate;
typedef enum DS1307_SquareWaveEnable{
DS1307_DISABLED, DS1307_ENABLED
} DS1307_SquareWaveEnable;
void DS1307_Init(I2C_HandleTypeDef *hi2c);
void DS1307_SetClockHalt(uint8_t halt);
uint8_t DS1307_GetClockHalt(void);
void DS1307_SetRegByte(uint8_t regAddr, uint8_t val);
uint8_t DS1307_GetRegByte(uint8_t regAddr);
void DS1307_SetEnableSquareWave(DS1307_SquareWaveEnable mode);
void DS1307_SetInterruptRate(DS1307_Rate rate);
uint8_t DS1307_GetDayOfWeek(void);
uint8_t DS1307_GetDate(void);
uint8_t DS1307_GetMonth(void);
uint16_t DS1307_GetYear(void);
uint8_t DS1307_GetHour(void);
uint8_t DS1307_GetMinute(void);
uint8_t DS1307_GetSecond(void);
int8_t DS1307_GetTimeZoneHour(void);
uint8_t DS1307_GetTimeZoneMin(void);
void DS1307_SetDayOfWeek(uint8_t dow);
void DS1307_SetDate(uint8_t date);
void DS1307_SetMonth(uint8_t month);
void DS1307_SetYear(uint16_t year);
void DS1307_SetHour(uint8_t hour_24mode);
void DS1307_SetMinute(uint8_t minute);
void DS1307_SetSecond(uint8_t second);
void DS1307_SetTimeZone(int8_t hr, uint8_t min);
uint8_t DS1307_DecodeBCD(uint8_t bin);
uint8_t DS1307_EncodeBCD(uint8_t dec);
#endif
ds1307_for_stm32_hal.c
文件:/* An STM32 HAL library written for the DS1307 real-time clock IC. */
/* Library by @eepj www.github.com/eepj */
#include "ds1307_for_stm32_hal.h"
#include "main.h"
#ifdef __cplusplus
extern "C"{
#endif
I2C_HandleTypeDef *_ds1307_ui2c;
/**
* @brief Initializes the DS1307 module. Sets clock halt bit to 0 to start timing.
* @param hi2c User I2C handle pointer.
*/
void DS1307_Init(I2C_HandleTypeDef *hi2c) {
_ds1307_ui2c = hi2c;
DS1307_SetClockHalt(0);
}
/**
* @brief Sets clock halt bit.
* @param halt Clock halt bit to set, 0 or 1. 0 to start timing, 0 to stop.
*/
void DS1307_SetClockHalt(uint8_t halt) {
uint8_t ch = (halt ? 1 << 7 : 0);
DS1307_SetRegByte(DS1307_REG_SECOND, ch | (DS1307_GetRegByte(DS1307_REG_SECOND) & 0x7f));
}
/**
* @brief Gets clock halt bit.
* @return Clock halt bit, 0 or 1.
*/
uint8_t DS1307_GetClockHalt(void) {
return (DS1307_GetRegByte(DS1307_REG_SECOND) & 0x80) >> 7;
}
/**
* @brief Sets the byte in the designated DS1307 register to value.
* @param regAddr Register address to write.
* @param val Value to set, 0 to 255.
*/
void DS1307_SetRegByte(uint8_t regAddr, uint8_t val) {
uint8_t bytes[2] = { regAddr, val };
HAL_I2C_Master_Transmit(_ds1307_ui2c, DS1307_I2C_ADDR << 1, bytes, 2, DS1307_TIMEOUT);
}
/**
* @brief Gets the byte in the designated DS1307 register.
* @param regAddr Register address to read.
* @return Value stored in the register, 0 to 255.
*/
uint8_t DS1307_GetRegByte(uint8_t regAddr) {
uint8_t val;
HAL_I2C_Master_Transmit(_ds1307_ui2c, DS1307_I2C_ADDR << 1, ®Addr, 1, DS1307_TIMEOUT);
HAL_I2C_Master_Receive(_ds1307_ui2c, DS1307_I2C_ADDR << 1, &val, 1, DS1307_TIMEOUT);
return val;
}
/**
* @brief Toggle square wave output on pin 7.
* @param mode DS1307_ENABLED (1) or DS1307_DISABLED (0);
*/
void DS1307_SetEnableSquareWave(DS1307_SquareWaveEnable mode){
uint8_t controlReg = DS1307_GetRegByte(DS1307_REG_CONTROL);
uint8_t newControlReg = (controlReg & ~(1 << 4)) | ((mode & 1) << 4);
DS1307_SetRegByte(DS1307_REG_CONTROL, newControlReg);
}
/**
* @brief Set square wave output frequency.
* @param rate DS1307_1HZ (0b00), DS1307_4096HZ (0b01), DS1307_8192HZ (0b10) or DS1307_32768HZ (0b11).
*/
void DS1307_SetInterruptRate(DS1307_Rate rate){
uint8_t controlReg = DS1307_GetRegByte(DS1307_REG_CONTROL);
uint8_t newControlReg = (controlReg & ~0x03) | rate;
DS1307_SetRegByte(DS1307_REG_CONTROL, newControlReg);
}
/**
* @brief Gets the current day of week.
* @return Days from last Sunday, 0 to 6.
*/
uint8_t DS1307_GetDayOfWeek(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_DOW));
}
/**
* @brief Gets the current day of month.
* @return Day of month, 1 to 31.
*/
uint8_t DS1307_GetDate(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_DATE));
}
/**
* @brief Gets the current month.
* @return Month, 1 to 12.
*/
uint8_t DS1307_GetMonth(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_MONTH));
}
/**
* @brief Gets the current year.
* @return Year, 2000 to 2099.
*/
uint16_t DS1307_GetYear(void) {
uint16_t cen = DS1307_GetRegByte(DS1307_REG_CENT) * 100;
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_YEAR)) + cen;
}
/**
* @brief Gets the current hour in 24h format.
* @return Hour in 24h format, 0 to 23.
*/
uint8_t DS1307_GetHour(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_HOUR) & 0x3f);
}
/**
* @brief Gets the current minute.
* @return Minute, 0 to 59.
*/
uint8_t DS1307_GetMinute(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_MINUTE));
}
/**
* @brief Gets the current second. Clock halt bit not included.
* @return Second, 0 to 59.
*/
uint8_t DS1307_GetSecond(void) {
return DS1307_DecodeBCD(DS1307_GetRegByte(DS1307_REG_SECOND) & 0x7f);
}
/**
* @brief Gets the stored UTC hour offset.
* @note UTC offset is not updated automatically.
* @return UTC hour offset, -12 to 12.
*/
int8_t DS1307_GetTimeZoneHour(void) {
return DS1307_GetRegByte(DS1307_REG_UTC_HR);
}
/**
* @brief Gets the stored UTC minute offset.
* @note UTC offset is not updated automatically.
* @return UTC time zone, 0 to 59.
*/
uint8_t DS1307_GetTimeZoneMin(void) {
return DS1307_GetRegByte(DS1307_REG_UTC_MIN);
}
/**
* @brief Sets the current day of week.
* @param dayOfWeek Days since last Sunday, 0 to 6.
*/
void DS1307_SetDayOfWeek(uint8_t dayOfWeek) {
DS1307_SetRegByte(DS1307_REG_DOW, DS1307_EncodeBCD(dayOfWeek));
}
/**
* @brief Sets the current day of month.
* @param date Day of month, 1 to 31.
*/
void DS1307_SetDate(uint8_t date) {
DS1307_SetRegByte(DS1307_REG_DATE, DS1307_EncodeBCD(date));
}
/**
* @brief Sets the current month.
* @param month Month, 1 to 12.
*/
void DS1307_SetMonth(uint8_t month) {
DS1307_SetRegByte(DS1307_REG_MONTH, DS1307_EncodeBCD(month));
}
/**
* @brief Sets the current year.
* @param year Year, 2000 to 2099.
*/
void DS1307_SetYear(uint16_t year) {
DS1307_SetRegByte(DS1307_REG_CENT, year / 100);
DS1307_SetRegByte(DS1307_REG_YEAR, DS1307_EncodeBCD(year % 100));
}
/**
* @brief Sets the current hour, in 24h format.
* @param hour_24mode Hour in 24h format, 0 to 23.
*/
void DS1307_SetHour(uint8_t hour_24mode) {
DS1307_SetRegByte(DS1307_REG_HOUR, DS1307_EncodeBCD(hour_24mode & 0x3f));
}
/**
* @brief Sets the current minute.
* @param minute Minute, 0 to 59.
*/
void DS1307_SetMinute(uint8_t minute) {
DS1307_SetRegByte(DS1307_REG_MINUTE, DS1307_EncodeBCD(minute));
}
/**
* @brief Sets the current second.
* @param second Second, 0 to 59.
*/
void DS1307_SetSecond(uint8_t second) {
uint8_t ch = DS1307_GetClockHalt();
DS1307_SetRegByte(DS1307_REG_SECOND, DS1307_EncodeBCD(second | ch));
}
/**
* @brief Sets UTC offset.
* @note UTC offset is not updated automatically.
* @param hr UTC hour offset, -12 to 12.
* @param min UTC minute offset, 0 to 59.
*/
void DS1307_SetTimeZone(int8_t hr, uint8_t min) {
DS1307_SetRegByte(DS1307_REG_UTC_HR, hr);
DS1307_SetRegByte(DS1307_REG_UTC_MIN, min);
}
/**
* @brief Decodes the raw binary value stored in registers to decimal format.
* @param bin Binary-coded decimal value retrieved from register, 0 to 255.
* @return Decoded decimal value.
*/
uint8_t DS1307_DecodeBCD(uint8_t bin) {
return (((bin & 0xf0) >> 4) * 10) + (bin & 0x0f);
}
/**
* @brief Encodes a decimal number to binaty-coded decimal for storage in registers.
* @param dec Decimal number to encode.
* @return Encoded binary-coded decimal value.
*/
uint8_t DS1307_EncodeBCD(uint8_t dec) {
return (dec % 10 + ((dec / 10) << 4));
}
#ifdef __cplusplus
}
#endif
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "ds1307_for_stm32_hal.h"
#include
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
const char *DAYS_OF_WEEK[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
DS1307_Init(&hi2c1);
// DS1307_SetTimeZone(+8, 00);
// DS1307_SetDate(31);
// DS1307_SetMonth(3);
// DS1307_SetYear(2023);
// DS1307_SetDayOfWeek(5);
// DS1307_SetHour(11);
// DS1307_SetMinute(40);
// DS1307_SetSecond(30);
uint32_t TimerUART = HAL_GetTick();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if ((HAL_GetTick() - TimerUART) > 1000)
{
uint8_t date = DS1307_GetDate();
uint8_t month = DS1307_GetMonth();
uint16_t year = DS1307_GetYear();
uint8_t dow = DS1307_GetDayOfWeek();
uint8_t hour = DS1307_GetHour();
uint8_t minute = DS1307_GetMinute();
uint8_t second = DS1307_GetSecond();
int8_t zone_hr = DS1307_GetTimeZoneHour();
uint8_t zone_min = DS1307_GetTimeZoneMin();
char buffer[100] = { 0 };
// sprintf(buffer, "ISO8601 FORMAT: 22%02d-%02d-%02d T%02d:%02d:%02d%+03d:%02d %s\r\n",
// year, month, date, hour, minute, second, zone_hr, zone_min, DAYS_OF_WEEK[dow]);
// HAL_UART_Transmit(&huart1, buffer, strlen(buffer), 1000);
printf("Time:20%02d-%02d-%02d\t%02d:%02d:%02d %s\r\n",year-8800, month, date, hour, minute, second, DAYS_OF_WEEK[dow]);
TimerUART = HAL_GetTick();
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 84;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
CSDN
网站,任何其他网站,未注明来源,见此内容均为盗链和爬取,请多多尊重和支持原创!复制这段内容后打开百度网盘手机App,操作更方便哦
链接: https://pan.baidu.com/s/18GXdSB7ZjsGa6ZYXmpQugg
提取码: g533