ds3231.c dosyası
#include "ds3231.h"
//--------------------------------------
char str[100];
uint8_t data[256];
uint16_t size;
uint8_t readBuf[7];
char * days[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thr",
"Fri",
"Sat"
};
struct Calendar {
uint8_t sec, min, hour, day, date, month, year;
}
calendar;
uint8_t BCD2DEC(uint8_t c) {
uint8_t ch = ((c >> 4) * 10 + (0x0F & c));
return ch;
}
uint8_t DEC2BCD(uint8_t c) {
uint8_t ch = ((c / 10) << 4) | (c % 10);
return ch;
}
//--------------------------------------
void DS3231_sendData(I2C_HandleTypeDef hi, uint8_t DEV_ADDR) {
uint8_t writeBuf[1];
writeBuf[0] = 0;
while (HAL_I2C_Master_Transmit( & hi, (uint16_t) DEV_ADDR,
(uint8_t * ) & writeBuf, sizeof(writeBuf), (uint32_t) 1000) != HAL_OK) {
if (HAL_I2C_GetError( & hi) != HAL_I2C_ERROR_AF) {
size = sprintf((char * ) data, "DS3231 write failed\n");
}
}
}
//--------------------------------------
void DS3231_setDate(I2C_HandleTypeDef hi, uint8_t DEV_ADDR) {
uint8_t data[8];
data[DS3231_CONTROL] = 0; // should be zero REGISTER SELECT from that will start
data[DS3231_SECONDS] = calendar.sec; //sec
data[DS3231_MINUTES] = calendar.min; //min
data[DS3231_HOURS] = calendar.hour; //hour
data[DS3231_DAY] = calendar.day; //day
data[DS3231_YEAR] = calendar.year; //year
data[DS3231_MONTH] = calendar.month; //month
data[DS3231_DATE] = calendar.date; //date
while (HAL_I2C_Master_Transmit( & hi, (uint16_t) DEV_ADDR, (uint8_t * ) & data,
8, (uint32_t) 1000) != HAL_OK) {
if (HAL_I2C_GetError( & hi) != HAL_I2C_ERROR_AF) {
size = sprintf((char * ) data, "DS3231 write failed\n");
}
}
}
//--------------------------------------
uint8_t * I2C_ReadRawData(I2C_HandleTypeDef hi, uint8_t DEV_ADDR) {
while (HAL_I2C_Master_Receive( & hi, (uint16_t) DEV_ADDR, (uint8_t * ) & readBuf,
(uint16_t) 7, (uint32_t) 1000) != HAL_OK) {
if (HAL_I2C_GetError( & hi) != HAL_I2C_ERROR_AF) {
size = sprintf((char * ) data, "DS3231 read failed\n");
}
}
return readBuf;
}
void I2C_ReadCalendarData(I2C_HandleTypeDef hi, uint8_t DEV_ADDR) {
while (HAL_I2C_Master_Receive( & hi, (uint16_t) DEV_ADDR, (uint8_t * ) & readBuf,
(uint16_t) 7, (uint32_t) 1000) != HAL_OK) {
if (HAL_I2C_GetError( & hi) != HAL_I2C_ERROR_AF) {
size = sprintf((char * ) data, "DS3231 read failed\n");
}
}
calendar.sec = readBuf[0];
calendar.min = readBuf[1];
calendar.hour = readBuf[2];
calendar.day = readBuf[3];
calendar.date = readBuf[4];
calendar.month = readBuf[5];
calendar.year = readBuf[6];
}
char * retrnAsString(uint8_t data) {
static char res[2];
uint8_t tempVal = BCD2DEC(data);
sprintf(res, "%c%c", ((tempVal / 10) % 10) + 0x30, (tempVal % 10) + 0x30);
return res;
}
uint8_t readIntSeconds() {
return calendar.sec;
}
char * readSeconds() {
return retrnAsString(calendar.sec);
}
char * readMinutes() {
return retrnAsString(calendar.min);
}
char * readHours() {
return retrnAsString(calendar.hour);
}
// Day Pazartesi ->Pazar
char * readDay() {
static char res[2];
uint8_t tempVal = BCD2DEC(calendar.day);
sprintf(res, "%c", (tempVal % 10) + 0x30);
return res;
}
char * readDate() {
return retrnAsString(calendar.date);
}
char * readMonth() {
return retrnAsString(calendar.month);
}
char * readYear() {
return retrnAsString(calendar.year);
}
void setSeconds(uint8_t sec) {
calendar.sec = DEC2BCD(sec);
}
void setMinutes(uint8_t min) {
calendar.min = DEC2BCD(min);
}
void setHour(uint8_t hour) {
calendar.hour = DEC2BCD(hour);
}
void setDay(uint8_t day) {
calendar.day = DEC2BCD(day);
}
void setDate(uint8_t date) {
calendar.date = DEC2BCD(date);
}
void setMonth(uint8_t month) {
calendar.month = DEC2BCD(month);
}
void setYear(uint8_t year) {
calendar.year = DEC2BCD(year);
}
ds3231.h dosyası
#ifndef ds3231_H_
#define ds3231_H_
#include "stm32f1xx_hal.h"
/* Registers location */
#define DS3231_CONTROL 0
#define DS3231_SECONDS 1
#define DS3231_MINUTES 2
#define DS3231_HOURS 3
#define DS3231_DAY 4
#define DS3231_DATE 5
#define DS3231_MONTH 6
#define DS3231_YEAR 7
uint8_t BCD2DEC(uint8_t c);
uint8_t DEC2BCD(uint8_t c);
void DS3231_sendData(I2C_HandleTypeDef hi2, uint8_t DEV_ADDR);
void DS3231_setDate(I2C_HandleTypeDef hi2, uint8_t DEV_ADDR);
uint8_t * I2C_ReadRawData(I2C_HandleTypeDef hi2, uint8_t DEV_ADDR);
void I2C_ReadCalendarData(I2C_HandleTypeDef hi2, uint8_t DEV_ADDR);
char *readSeconds(void);
char *readMinutes(void);
char *readHours(void);
char *readDay(void);
char *readDate(void);
char *readMonth(void);
char *readYear(void);
void setSeconds(uint8_t sec);
void setMinutes(uint8_t min);
void setHour(uint8_t hour);
void setDay(uint8_t day);
void setDate(uint8_t date);
void setMonth(uint8_t month);
void setYear(uint8_t year);
/*
uint8_t readIntSeconds();
*/
#endif /* ds3231_H_ */
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
** This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* COPYRIGHT(c) 2019 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdlib.h"
#include "string.h"
#include "LCD.h"
#include "ds3231.h"
/* 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 ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(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 */
uint8_t ds3231Addr = (uint8_t) 0xD0;
uint32_t saniye, dakika, saat;
uint32_t gun, ay, yil;
char line1[32],line2[32];
/* 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();
/* USER CODE BEGIN 2 */
lcd_init(_LCD_4BIT, _LCD_FONT_5x8, _LCD_2LINE);
lcd_print(1, 1, " DS3231 RTC");
HAL_Delay(500);
// DS3231_sendData(hi2c1, ds3231Addr);
// I2C_ReadCalendarData(hi2c1, ds3231Addr);
setSeconds(10);
setMinutes(21);
setHour(12);
setDate(30);
setMonth(7);
setYear(19);
DS3231_sendData(hi2c1, ds3231Addr);
while (HAL_I2C_GetState( & hi2c1) != HAL_I2C_STATE_READY){}
DS3231_setDate(hi2c1, ds3231Addr);
lcd_clear();
HAL_Delay(500);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
DS3231_sendData(hi2c1, ds3231Addr);
while (HAL_I2C_GetState( & hi2c1) != HAL_I2C_STATE_READY) {}
//Read
I2C_ReadCalendarData(hi2c1, ds3231Addr);
/*
atoi komutu içerdigi string ifadeyi integera çevirir.
*/
saniye = atoi(readSeconds());
dakika = atoi(readMinutes());
saat = atoi(readHours());
gun = atoi(readDate());
ay = atoi(readMonth());
yil = atoi(readYear());
sprintf(line1, " %0.2u:%0.2u:%0.2u", saat, dakika, saniye);
sprintf(line2, " %0.2u/%0.2u/%0.2u", gun, ay, yil);
lcd_print(1, 1, line1);
lcd_print(2, 1, line2);
HAL_Delay(250);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**Initializes the CPU, AHB and APB busses 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_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 32768;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, LCD_EN_Pin|LCD_RS_Pin|LCD_D4_Pin|LCD_D5_Pin
|LCD_D6_Pin|LCD_D7_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : LCD_EN_Pin LCD_RS_Pin LCD_D4_Pin LCD_D5_Pin
LCD_D6_Pin LCD_D7_Pin */
GPIO_InitStruct.Pin = LCD_EN_Pin|LCD_RS_Pin|LCD_D4_Pin|LCD_D5_Pin
|LCD_D6_Pin|LCD_D7_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* 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 */
/* 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,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/