Gönderen Konu: 16x2 lcd de RTC ile saat  (Okunma sayısı 192 defa)

Çevrimdışı bunymert

  • Yeni Üye
  • *
  • İleti: 16
16x2 lcd de RTC ile saat
« : 22 Kasım 2017, 14:18:31 »
        Arkadaşlar uzun süre pic ile uğraştıktan sonra çıtayı yükseltip stm32f4 programlamaya karar verdim. adaptasyon sürecinden sonra baya bi uğraşarak yaptım.
Sonunda çalıştırdım.şimdi sizden ricam aşağıda verdiğim kodların profesyonel veya uzman bir gözle inceleyip bana veya benim gibi acemi kişilere faydalı olması açısından yapılan yanlışlıkları,  hataları, daha kolay veya daha değişik yollardan nasıl yapılacağı yönünde bilgi vermeniz. Bu sayede daha sonra yazacağımız kodlarda bize faydalı olacağını düşünüyorum. Zira ben internet üzerinde video izleyerek kitapları karıştırarak öğrenmeye çalıştım. Dolayısıyla bu işin pratiğini pek bilmiyorum. Fikirlerinizi esirgemezseniz çok sevinirim. Şimdiden vakit ayıran arkadaşlara teşekkür ediyorum.

Kod: [Seç]
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_hal.h"

/* USER CODE BEGIN Includes */
#include "stm32fxxx_hal.h"
#include "defines.h"
#include "tm_stm32_hd44780.h"

#define Set GPIO_PIN_2
#define Arrtir GPIO_PIN_3
#define Eksilt GPIO_PIN_0

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
RTC_HandleTypeDef hrtc;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
uint8_t Time[3],Date[4];
char Zaman[10],Tarih[10];

uint8_t s=1;
int8_t saat=12,dakika=0;
uint8_t gun=1, ay=1, yil=17,hafgun=1;

RTC_DateTypeDef sdatestractureget;
RTC_TimeTypeDef stimestractuteget;
RTC_TimeTypeDef sTime;
 RTC_DateTypeDef sDate;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_RTC_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
void ayar()
{
while (HAL_GPIO_ReadPin(GPIOE,Set)) ;
Delayms(50); // ark söndürmek için
uint8_t s=1;

saat=sTime.Hours; // şimdiki saati ayar değişkenlerine atıyoruz. ayar fonksiyonuna girdiğimizde
dakika=sTime.Minutes; // farklı bir zaman görmemek için.
ay=sDate.Month;
gun=sDate.Date;
yil=sDate.Year ;
hafgun=sDate.WeekDay;
     
while (s !=  0)
   {
   if (s == 1)
{
char st[5]; // LCD ekranda ayarlanacak kısımın yanıp sönmesini göstermek için değişken tanımlıyoruz.
for (uint16_t i=0;i<=253;i++)
{ // gecikme sağlayıp yanıp sönme süreleri için döngü kurduk
if (HAL_GPIO_ReadPin(GPIOE,Arrtir))
{

while (HAL_GPIO_ReadPin(GPIOE,Arrtir)) ;
Delayms(50); // ark söndürmek için
saat = saat + 1;
if (saat > 23 ) saat = 0;
}//if (HAL_GPIO_ReadPin(GPIOE,Arrtir))

if (HAL_GPIO_ReadPin(GPIOA,Eksilt))
{
while (HAL_GPIO_ReadPin(GPIOA,Eksilt)) ;
Delayms(50);
saat = saat - 1;
if (saat < 0) saat = 23;
}//if (HAL_GPIO_ReadPin(GPIOA,Eksilt))
 
sprintf (st,"%02d",saat);
if(i==250){TM_HD44780_Puts(4, 0, st);}
if(i==125){TM_HD44780_Puts(4, 0, "  ");}
Delayms(2); // yanıp sönme süresi 2ms * 125 = 250ms civarı

if (HAL_GPIO_ReadPin(GPIOE,Set))
{
while (HAL_GPIO_ReadPin(GPIOE,Set)) ;
Delayms(50);
s = 2;
}//if (HAL_GPIO_ReadPin(GPIOE,Set))

}//for (uint16_t i=0;i<=253;i++)
}//if (s == 1)

if (s == 2)
{
char st[5];
for (uint8_t i=0;i<=253;i++)
{
if (HAL_GPIO_ReadPin(GPIOE,Arrtir))
{
           
while (HAL_GPIO_ReadPin(GPIOE,Arrtir)) ;
Delayms(50); // ark söndürmek için
dakika = dakika + 1;
if (dakika > 59 ) dakika = 0;
}//if (HAL_GPIO_ReadPin(GPIOE,Arrtir))

if (HAL_GPIO_ReadPin(GPIOA,Eksilt))
{
while (HAL_GPIO_ReadPin(GPIOA,Eksilt)) ;
Delayms(50);
dakika = dakika - 1;
if (dakika < 0 ) dakika = 59;
}//if (HAL_GPIO_ReadPin(GPIOA,Eksilt))

sprintf (st,"%02d",dakika);
if(i==250){TM_HD44780_Puts(7, 0, st);}
if(i==125){TM_HD44780_Puts(7, 0, "  ");}
Delayms(2);
         
if (HAL_GPIO_ReadPin(GPIOE,Set))
{
while (HAL_GPIO_ReadPin(GPIOE,Set)) ;
Delayms(50);
s = 3;
}//if (HAL_GPIO_ReadPin(GPIOE,Set))
}//for (uint8_t i=0;i<=253;i++)
}//if (s == 2)

if (s == 3)
{
char st[5];
for (uint8_t i=0;i<=253;i++)
{
if (HAL_GPIO_ReadPin(GPIOE,Arrtir))
{
           
while (HAL_GPIO_ReadPin(GPIOE,Arrtir)) ;
Delayms(50); // ark söndürmek için
gun = gun + 1;
if (gun > 31 ) gun = 1;
}//if (HAL_GPIO_ReadPin(GPIOE,Arrtir))

if (HAL_GPIO_ReadPin(GPIOA,Eksilt))
{
while (HAL_GPIO_ReadPin(GPIOA,Eksilt)) ;
Delayms(50);
gun = gun - 1;
if (gun < 1 ) gun = 31; 
}//if (HAL_GPIO_ReadPin(GPIOA,Eksilt))

sprintf (st,"%02d",gun);
if(i==250){TM_HD44780_Puts(1, 1, st);}
if(i==125){TM_HD44780_Puts(1, 1, "  ");}
Delayms(2);

if (HAL_GPIO_ReadPin(GPIOE,Set))
{
while (HAL_GPIO_ReadPin(GPIOE,Set)) ;
Delayms(50);
s = 4;
}//if (HAL_GPIO_ReadPin(GPIOE,Set))
}//for (uint8_t i=0;i<=253;i++)
}//if (s == 3)

if (s == 4)
{
char st[5];
for (uint8_t i=0;i<=253;i++)
{
if (HAL_GPIO_ReadPin(GPIOE,Arrtir))
{
while (HAL_GPIO_ReadPin(GPIOE,Arrtir)) ;
Delayms(50); // ark söndürmek için
ay = ay + 1;
if (ay > 12 ) ay = 1;
}//if (HAL_GPIO_ReadPin(GPIOE,Arrtir))

if (HAL_GPIO_ReadPin(GPIOA,Eksilt))
{
while (HAL_GPIO_ReadPin(GPIOA,Eksilt)) ;
Delayms(50);
ay = ay - 1;
if (ay < 1 ) ay = 12; 
}//if (HAL_GPIO_ReadPin(GPIOA,Eksilt))

sprintf (st,"%02d",ay);
if(i==250){TM_HD44780_Puts(4, 1, st);}
if(i==125){TM_HD44780_Puts(4, 1, "  ");}
Delayms(2);

if (HAL_GPIO_ReadPin(GPIOE,Set))
{
while (HAL_GPIO_ReadPin(GPIOE,Set)) ;
Delayms(50);
s = 5;
}//if (HAL_GPIO_ReadPin(GPIOE,Set))
}//for (uint8_t i=0;i<=253;i++)
}//if (s == 4)

if (s == 5)
{
char st[5];
for (uint8_t i=0;i<=253;i++)
{
if (HAL_GPIO_ReadPin(GPIOE,Arrtir))
{

while (HAL_GPIO_ReadPin(GPIOE,Arrtir)) ;
Delayms(50); // ark söndürmek için
yil = yil + 1;
if (yil > 99 ) yil = 99;
}//if (HAL_GPIO_ReadPin(GPIOE,Arrtir))

if (HAL_GPIO_ReadPin(GPIOA,Eksilt))
{
while (HAL_GPIO_ReadPin(GPIOA,Eksilt)) ;
Delayms(50);
yil = yil - 1;
if (yil < 17 ) yil = 17; 
}//(HAL_GPIO_ReadPin(GPIOA,Eksilt))

sprintf (st,"%02d",yil+2000);
if(i==250){TM_HD44780_Puts(7, 1, st);}
if(i==125){TM_HD44780_Puts(7, 1, "    ");}
Delayms(2);

if (HAL_GPIO_ReadPin(GPIOE,Set))
{
while (HAL_GPIO_ReadPin(GPIOE,Set)) ;
Delayms(50);
s = 6;
}//if (HAL_GPIO_ReadPin(GPIOE,Set))
}//for (uint8_t i=0;i<=253;i++)
}//if (s == 5)

if (s == 6)
{
for (uint8_t i=0;i<253;i++)
{
if (HAL_GPIO_ReadPin(GPIOE,Arrtir))
{

while (HAL_GPIO_ReadPin(GPIOE,Arrtir)) ;
Delayms(50); // ark söndürmek için
hafgun = hafgun + 1;
if (hafgun > 7 ) hafgun = 1;
}//if (HAL_GPIO_ReadPin(GPIOE,Arrtir))

if (HAL_GPIO_ReadPin(GPIOA,Eksilt))
{
while (HAL_GPIO_ReadPin(GPIOA,Eksilt)) ;
Delayms(50);
hafgun = hafgun - 1;
if (hafgun < 1 ) hafgun = 7; 
}//if (HAL_GPIO_ReadPin(GPIOA,Eksilt))

Date[2]= hafgun;
if (i==250)
{
switch (Date[2])
{
case 1:TM_HD44780_Puts(13, 1, "PZT");break;
case 2:TM_HD44780_Puts(13, 1, "SAL");break;
case 3:TM_HD44780_Puts(13, 1, "CAR");break;
case 4:TM_HD44780_Puts(13, 1, "PER");break;
case 5:TM_HD44780_Puts(13, 1, "CUM");break;
case 6:TM_HD44780_Puts(13, 1, "CTS");break;
case 7:TM_HD44780_Puts(13, 1, "PZR");break;

}//switch (Date[2])

}//if (i==250)

if (i==125)
{
TM_HD44780_Puts(13, 1, "   ");
}//if (i==125)

Delayms(2);
         

if (HAL_GPIO_ReadPin(GPIOE,Set))
{
while (HAL_GPIO_ReadPin(GPIOE,Set)) ;
Delayms(50);
s = 0;

sTime.Hours = saat; // ayarlanan değerleri kaydediyoruz
sTime.Minutes = dakika;
sDate.Month = ay;
sDate.Date = gun;
sDate.Year = yil;
sDate.WeekDay=hafgun;

HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
}//if (HAL_GPIO_ReadPin(GPIOE,Set))
}//for (uint8_t i=0;i<253;i++)
}//if (s == 6)
}//while (s !=  0)
 }//void ayar()
/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_RTC_Init();

  /* USER CODE BEGIN 2 */
TM_HD44780_Init(16, 2);
   
    /* Save custom character on location 0 in LCD */
       
    /* Put string to LCD */
   
TM_HD44780_Clear();
   
    /* Show cursor */
    TM_HD44780_CursorOn();
   
    /* Write new text */
    TM_HD44780_Puts(1, 2, "ACILIYOR");
   
    /* Wait a little */
    Delayms(2000);
   
    /* Enable cursor blinking */
    TM_HD44780_BlinkOn();
TM_HD44780_BlinkOff();
TM_HD44780_CursorOff();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

HAL_RTC_GetTime (&hrtc, &stimestractuteget, RTC_FORMAT_BIN);

HAL_RTC_GetDate (&hrtc, &sdatestractureget, RTC_FORMAT_BIN);


Time[0]=stimestractuteget.Hours;
Time[1]=stimestractuteget.Minutes;
Time[2]=stimestractuteget.Seconds;

Date[2]=sdatestractureget.WeekDay;
Date[1]=sdatestractureget.Month;
Date[0]=sdatestractureget.Year;
Date[3]=sdatestractureget.Date;

TM_HD44780_Clear();TM_HD44780_BlinkOff();
sprintf (Zaman,"%02d:%02d:%02d",Time[0],Time[1],Time[2]);
sprintf (Tarih,"%02d/%02d/%02d",Date[3],Date[1],Date[0]+2000);
TM_HD44780_Puts(4, 0, Zaman);
TM_HD44780_Puts(1, 1, Tarih);

switch (Date[2])
{
case 1:TM_HD44780_Puts(13, 1, "PZT");break;
case 2:TM_HD44780_Puts(13, 1, "SAL");break;
case 3:TM_HD44780_Puts(13, 1, "CAR");break;
case 4:TM_HD44780_Puts(13, 1, "PER");break;
case 5:TM_HD44780_Puts(13, 1, "CUM");break;
case 6:TM_HD44780_Puts(13, 1, "CTS");break;
case 7:TM_HD44780_Puts(13, 1, "PZR");break;
}//switch (Date[2])

Delayms(50); // lcd ekran için bekliyoruz

if (HAL_GPIO_ReadPin(GPIOE,Set))
{
while (HAL_GPIO_ReadPin(GPIOE,Set)) ;
ayar();
}//if (HAL_GPIO_ReadPin(GPIOE,Set))

}//while (1)

  /* USER CODE END 3 */

}//int main(void)

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;

    /**Configure the main internal regulator output voltage
    */
  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 5;
  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_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }

  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

    /**Configure the Systick interrupt time
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* RTC init function */
static void MX_RTC_Init(void)
{

 

    /**Initialize RTC Only
    */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 255;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

    /**Initialize RTC and set the Time and Date
    */
  if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
  sTime.Hours = saat;
  sTime.Minutes = dakika;
  sTime.Seconds = 0x0;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }

  sDate.WeekDay = RTC_WEEKDAY_SATURDAY;
  sDate.Month = RTC_MONTH_NOVEMBER;
  sDate.Date = gun;
  sDate.Year = yil;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }

    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
  }

}

/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
*/
static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pins : PE2 PE3 PE4 */
  GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

  /*Configure GPIO pin : PA0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler */
  /* User can add his own implementation to report the HAL error return state */
  while(1)
  {
  }
  /* USER CODE END Error_Handler */
}

#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