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 */
// Delay functions using SysTick timer // Minimal delay length is 1ms #include "delay.h" // Delay counter static __IO uint32_t DelayCounter; // SysTick interrupt handler void SysTick_Handler(void) { DelayCounter++; } // Initialize delay functions // note: configures the SysTick counter, must be called each time when the core // clock has been changed void delay_init(void) { // Set reload register to generate IRQ every millisecond SysTick->LOAD = (uint32_t)(SystemCoreClock / (1000UL - 1UL)); // Set priority for SysTick IRQ NVIC_SetPriority(SysTick_IRQn,(1 << __NVIC_PRIO_BITS) - 1); // Set the SysTick counter value SysTick->VAL = 0UL; // Set SysTick source and IRQ SysTick->CTRL = (SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk); } // Do a delay for a specified number of milliseconds // input: // ms - number of milliseconds to wait void delay_ms(uint32_t ms) { // Enable the SysTick timer SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // Wait for a specified number of milliseconds DelayCounter = 0; while (DelayCounter < ms); // Disable the SysTick timer SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; } // Do a delay for a specified number of milliseconds, MCU in sleep mode // input: // ms - number of milliseconds to wait // note: all wake-up flags must be cleared before calling this function // to ensure proper sleep during delay void delaysleep_ms(uint32_t ms) { // Enable the SysTick timer SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // Set delay counter DelayCounter = 0; // Wait for a specified number of milliseconds while (DelayCounter < ms) { __WFI(); } // Disable the SysTick timer SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; }
#ifndef __DELAY_H #define __DELAY_H #include "stm32f10x.h" // Device header // Function prototypes void delay_init(void); void delay_ms(uint32_t ms); void delaysleep_ms(uint32_t ms); #endif // __DELAY_H
#include "stm32f0xx.h" // Device header #ifndef __DELAY_H #define __DELAY_H void SysTick_Handler(void); void delay_ms(__IO uint32_t time); void delay_init(void); #endif // __DELAY_H
#include "delay.h" uint32_t TimingDelay; void SysTick_Handler(void) // Her 1ms'de bir olusacak kesmeyi yakaladigimiz fonksiyonumuz { if (TimingDelay != 0) // TimingDelay'in degeri sifirdan farkli ise çalistir. { TimingDelay --; //TimingDelay'in degeri bir azaltir. } } void delay_ms(__IO uint32_t time) // Delay Fonksiyonumuz { TimingDelay = time; //Kesme fonksiyonunda kullanacagimiz degiskenin degerini ayarliyoruz while(TimingDelay != 0); //TimingDelay degiskeninin degeri sifirdan farkli oldugu sürece döngüde kal. } void delay_init(void) { SystemInit(); SysTick_Config(SystemCoreClock/1000); }
#include "stm32f10x.h" void delay(uint32_t time) { time=time*2318; while(time) time--; } int main() { RCC->APB2ENR |= 1<<4; GPIOC->CRH |=0x00000002; while(1) { GPIOC->BSRR= 1<<8; delay(1000); GPIOC->BRR = 1<<8; } }
#include "stm32f10x.h" // Device header _Bool x=0; int main() { RCC->APB2ENR |= (1 << 2); /* Enable GPIOA clock */ RCC->APB2ENR |= (1 << 4); /* Enable GPIOC clock */ GPIOA->CRL &= 0x00000000; // Set to Zero Port A GPIOA->CRL |= 0x00000008; // Set as a input PA0 GPIOC->CRH = 0x00000022; // Set as a output PC8 & PC9 while (1) { x=(GPIOA->IDR & 0x0000001); if(x) // PA0 ON (Led4) // { GPIOC->BSRR = 1<<8; // LED ON PC8 } else { GPIOC->BRR = 1<<8; // LED OFF PC8 } } }
#include <stm32f10x.h> // Device header #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h" void delay_ms(uint32_t the_time) { the_time=the_time*2381; while(the_time) { the_time--; } } int main() { GPIO_InitTypeDef GPIO_InitStructure; uint8_t input_button; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //Led Configuration GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); //Button Configuration GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); while(1) { input_button = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0); if(input_button) { GPIO_SetBits(GPIOC, GPIO_Pin_9); GPIO_ResetBits(GPIOC, GPIO_Pin_8); } else { GPIO_ResetBits(GPIOC, GPIO_Pin_9); GPIO_SetBits(GPIOC, GPIO_Pin_8); } } }
void delay_ms(uint32_t the_time) { the_time=the_time*2381; while(the_time) { the_time--; } }