void RCC_Init() { RCC->CFGR |= RCC_CFGR_MCOPRE_DIV1 | RCC_CFGR_MCO_HSI48 | RCC_CFGR_PPRE_DIV1 | RCC_CFGR_SW_HSI48 | RCC_CFGR_SWS_HSI48; RCC->CR2 |= RCC_CR2_HSI48ON; while(!RCC->CR2 & RCC_CR2_HSI48RDY); }
void RCC_Init() { RCC->CFGR |= RCC_CFGR_MCOPRE_DIV1 | RCC_CFGR_MCO_HSI48 | RCC_CFGR_PPRE_DIV1 | RCC_CFGR_SW_HSI48 | RCC_CFGR_SWS_HSI48; RCC->CR2 |= RCC_CR2_HSI48ON; while(!RCC->CR2 & RCC_CR2_HSI48RDY); }
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * <h2><center>© Copyright (c) 2019 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */
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); }
/* 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 */