BASIC4MCU | 질문게시판 | STM32F429ZIT6보드와 lcm1602 이용한 알람시계 질문 (사용자 지정 알람 값에 알람 인터럽트 반응)
페이지 정보
작성자 aaahhhj 작성일2024-07-24 00:51 조회129회 댓글0건본문
lcd에 있는 버튼을 활용하여 set time모드, set date 모드, set alarm모드, (기본)시계모드 를 lcd에 출력하는 알람시계 코드를 짜고 있습니다.
현재 상황
rtc alarm 설정 창에서 기본 시간값 13:50:00인데, run 돌리면 이 시간부터 lcd에 1초씩 증가하는 시계가 출력됩니다.
rtc alarm 설정 창에서 설정한 기본 알람값 13:50:10에 맞춰서 알람 인터럽트가 반응합니다.
B1 버튼을 눌러 SET TIME 모드로 진입해서 시간을 설정할수있고, 그 시간부터 시간이 카운트됩니다.
B2 버튼을 2번 눌러 SET date 모드 들어가면 년 월 일 요일을 바꿀 수 있고 바꾼 연 월 일 요일이 기본 시계모드에 반영됩니다.
B2 버튼을 3번 눌러 SET ALARM 모드 들어가면 알람을 설정할 수 있습니다.
<문제사항>
문제는 제가 버튼을 눌러 설정한 알람에 맞춰서 알람 인터럽트가 발생하지 않는 것입니다.
chat gpt 에 계속 물어봐도 몇시간째 답을 찾지 못해 여기에 질문드려봅니다.
제 코드에 있는 디버깅 코드 에서
제가 설정한 알람 시간 대로 rtc alarm set to (시간) 은 uart로 출력이 되나,
제가 설정한 알람 시간이 되면 alarm trigger 문구가 uart로 출력되어야 하는데 그게 출력이 안됩니다.
<main.c>
#include "main.h"
#include "adc.h"
#include "dac.h"
#include "dma.h"
#include "i2c.h"
#include "rtc.h"
#include "tim.h"
#include "usart.h"
#include "usb_otg.h"
#include "gpio.h"
#include "stdio.h"
#include "string.h"
#include <stdbool.h>
#define LCD_ADDR (0x27 << 1)
volatile bool alarm_triggered = 0;
volatile bool update_lcd_flag = 0;
volatile bool buzzer_flag = 0;
volatile uint8_t buzzer_index = 0;
RTC_DateTypeDef sDate;
RTC_TimeTypeDef sTime;
RTC_AlarmTypeDef sAlarm;
uint32_t adc_val=0;
volatile int timer_count=0;
char now_time[20], alm_time[20];
unsigned int hour_now=0, min_now=0, sec_now=0;
unsigned int hour_alr=0, min_alr=0, sec_alr=0;
unsigned int year_now = 24, month_now = 6, date_now = 14, weekday_now = 1;
unsigned int i =0;
int mode_count=0;
char music_cnt[5][20] = {"1.Hi GoldenBoy ", "2.LOVE ME TENDER", "3.Tender ", "4.Sad movie ", "5.Bye GoldenBoy "};
int posit =1;
unsigned int mode=0;
unsigned int set_step = 0;
uint32_t last_adc_val = 0;
uint32_t last_button_press_time = 0;
uint32_t uart_buf[100];
unsigned int alarm_hour = 0;
unsigned int alarm_minute = 0;
unsigned int alarm_second = 0;
void SystemClock_Config(void);//기본값
static void MX_NVIC_Init(void); //기본값
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
alarm_triggered = 1;
char debug_msg[50];
sprintf(debug_msg, "RTC Alarm Triggered\n");
HAL_UART_Transmit(&huart3, (uint8_t *)debug_msg, strlen(debug_msg), 100);
// 현재 시간 출력
HAL_RTC_GetTime(hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(hrtc, &sDate, RTC_FORMAT_BIN);
char current_time_msg[50];
sprintf(current_time_msg, "Alarm Triggered at %02d:%02d:%02d\n", sTime.Hours, sTime.Minutes, sTime.Seconds);
HAL_UART_Transmit(&huart3, (uint8_t *)current_time_msg, strlen(current_time_msg), 100);
}
void lcd_alarm()
{
LCD_Clear(LCD_ADDR);
LCD_SendCommand(LCD_ADDR, 0b10000000);
LCD_SendString(LCD_ADDR, "Alarm interrupt!");
LCD_SendCommand(LCD_ADDR, 0b11000000);
LCD_SendString(LCD_ADDR, "Alarm interrupt!");
}
void ADC_MODULE()
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 10);
adc_val = HAL_ADC_GetValue(&hadc1);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM3)
{
if ((timer_count % 10) == 0)
{
if (mode == 0)
{
sec_now++;
if (sec_now > 59)
{
sec_now = 0;
min_now++;
}
if (min_now > 59)
{
min_now = 0;
hour_now++;
}
if (hour_now > 23) hour_now = 0;
update_lcd_flag = 1;
}
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
char current_time_msg[50];
sprintf(current_time_msg, "Current Time: %02d:%02d:%02d\n", sTime.Hours, sTime.Minutes, sTime.Seconds);
HAL_UART_Transmit(&huart3, (uint8_t *)current_time_msg, strlen(current_time_msg), 100);
}
timer_count++;
}
}
void handle_buttons()
{
ADC_MODULE();
if (adc_val != last_adc_val && (HAL_GetTick() - last_button_press_time) > 200)
{ last_button_press_time = HAL_GetTick();
last_adc_val = adc_val;
if (adc_val >= 0 && adc_val < 10)
{ // UP 버튼
if (mode == 1)
{
if (set_step == 0)
{
hour_now++;
if (hour_now > 23)
hour_now = 0;
}
else if (set_step == 1)
{
min_now++;
if (min_now > 59)
min_now = 0;
}
else if (set_step == 2)
{
sec_now++;
if (sec_now > 59)
sec_now = 0;
}
update_lcd_set_time();
}
else if (mode == 2)
{
if (set_step == 0)
{
year_now++;
if (year_now > 99)
year_now = 0;
}
else if (set_step == 1)
{
month_now++;
if (month_now > 12)
month_now = 1;
}
else if (set_step == 2)
{
date_now++;
if (date_now > 31)
date_now = 1;
}
else if (set_step == 3)
{
weekday_now++;
if (weekday_now > 7)
weekday_now = 1;
}
update_lcd_set_date();
}
else if (mode == 3)
{
if (set_step == 0)
{
alarm_hour++;
if (alarm_hour > 23)
alarm_hour = 0;
}
else if (set_step == 1)
{
alarm_minute++;
if (alarm_minute > 59)
alarm_minute = 0;
}
else if (set_step == 2)
{
alarm_second++;
if (alarm_second > 59)
alarm_second = 0;
}
update_lcd_set_alarm();
char debug_msg[50];
int debug_msg_len = sprintf(debug_msg, "Alarm Set to %02d:%02d:%02d\n", alarm_hour, alarm_minute, alarm_second);
HAL_UART_Transmit(&huart3, (uint8_t *)debug_msg, debug_msg_len, 100);
set_rtc_alarm();
}
}
else if (adc_val >= 800 && adc_val < 900)
{ // DOWN 버튼
if (mode == 1)
{
if (set_step == 0)
{
if (hour_now == 0)
hour_now = 23;
else
hour_now--;
}
else if (set_step == 1)
{
if (min_now == 0)
min_now = 59;
else
min_now--;
}
else if (set_step == 2)
{
if (sec_now == 0)
sec_now = 59;
else
sec_now--;
}
update_lcd_set_time();
}
else if (mode == 2)
{
if (set_step == 0)
{
if (year_now == 0)
year_now = 99;
else
year_now--;
}
else if (set_step == 1)
{
if (month_now == 1)
month_now = 12;
else
month_now--;
}
else if (set_step == 2)
{
if (date_now == 1)
date_now = 31;
else
date_now--;
}
else if (set_step == 3)
{
if (weekday_now == 1)
weekday_now = 7;
else
weekday_now--;
}
update_lcd_set_date();
}
else if (mode == 3)
{
if (set_step == 0)
{
if (alarm_hour == 0)
alarm_hour = 23;
else
alarm_hour--;
}
else if (set_step == 1)
{
if (alarm_minute == 0)
alarm_minute = 59;
else
alarm_minute--;
}
else if (set_step == 2)
{
if (alarm_second == 0)
alarm_second = 59;
else
alarm_second--;
}
update_lcd_set_alarm();
char debug_msg[50];
int debug_msg_len = sprintf(debug_msg, "Alarm Set to %02d:%02d:%02d\n", alarm_hour, alarm_minute, alarm_second);
HAL_UART_Transmit(&huart3, (uint8_t *)debug_msg, debug_msg_len, 100);
set_rtc_alarm();
}
}
else if (adc_val >= 2600 && adc_val < 3000)
{ // RIGHT 버튼
if (mode == 1)
{
set_step++;
if (set_step > 2)
{
set_step = 0;
mode = 0;
sTime.Hours = hour_now;
sTime.Minutes = min_now;
sTime.Seconds = sec_now;
HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
char buffer[50];
int buffer_len = sprintf(buffer, "Mode: 0, Time Set to %02d:%02d:%02d\n", hour_now, min_now, sec_now);
HAL_UART_Transmit(&huart3, (uint8_t *)buffer, buffer_len, 100);
}
else
{
char buffer[50];
int buffer_len = sprintf(buffer, "Set Step: %d\n", set_step);
HAL_UART_Transmit(&huart3, (uint8_t *)buffer, buffer_len, 100);
}
update_lcd_set_time();
}
else if (mode == 2)
{
set_step++;
if (set_step > 3)
{
set_step = 0;
mode = 0;
sDate.Year = year_now;
sDate.Month = month_now;
sDate.Date = date_now;
sDate.WeekDay = weekday_now;
HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
char buffer[50];
int buffer_len = sprintf(buffer, "Mode: 0, Date Set to %02d-%02d-%02d\n", year_now, month_now, date_now);
HAL_UART_Transmit(&huart3, (uint8_t *)buffer, buffer_len, 100);
}
else
{
char buffer[50];
int buffer_len = sprintf(buffer, "Set Step: %d\n", set_step);
HAL_UART_Transmit(&huart3, (uint8_t *)buffer, buffer_len, 100);
}
update_lcd_set_date();
}
else if (mode == 3)
{
set_step++;
if (set_step > 2)
{
set_step = 0;
mode = 0;
set_rtc_alarm();
}
else
{
char buffer[50];
int buffer_len = sprintf(buffer, "Set Step: %d\n", set_step);
HAL_UART_Transmit(&huart3, (uint8_t *)buffer, buffer_len, 100);
}
update_lcd_set_alarm();
}
}
}
}
void set_rtc_alarm(void)
{
RTC_AlarmTypeDef sAlarm = {0};
sAlarm.AlarmTime.Hours = alarm_hour;
sAlarm.AlarmTime.Minutes = alarm_minute;
sAlarm.AlarmTime.Seconds = alarm_second;
sAlarm.Alarm = RTC_ALARM_A;
sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 0x1;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
{
char error_msg[50];
sprintf(error_msg, "Error: Alarm not set\n");
HAL_UART_Transmit(&huart3, (uint8_t *)error_msg, strlen(error_msg), 100);
Error_Handler();
}
else
{
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
char buffer[100];
int buffer_len = sprintf(buffer, "RTC Alarm Set to %02d:%02d:%02d\nCurrent Time: %02d:%02d:%02d\n",
alarm_hour, alarm_minute, alarm_second, sTime.Hours, sTime.Minutes, sTime.Seconds);
HAL_UART_Transmit(&huart3, (uint8_t *)buffer, buffer_len, 100);
// 알람 설정 확인
RTC_AlarmTypeDef readBackAlarm;
HAL_RTC_GetAlarm(&hrtc, &readBackAlarm, RTC_ALARM_A, RTC_FORMAT_BIN);
char readback_msg[100];
int readback_len = sprintf(readback_msg, "Alarm Readback: %02d:%02d:%02d\n",
readBackAlarm.AlarmTime.Hours, readBackAlarm.AlarmTime.Minutes, readBackAlarm.AlarmTime.Seconds);
HAL_UART_Transmit(&huart3, (uint8_t *)readback_msg, readback_len, 100);
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_USB_OTG_FS_PCD_Init();
MX_USART3_UART_Init();
MX_ADC1_Init();
MX_DAC_Init();
MX_TIM3_Init();
MX_I2C1_Init();
MX_RTC_Init();
MX_TIM2_Init();
MX_NVIC_Init(); //기본 초기화 코드
HAL_TIM_Base_Start_IT(&htim2);
HAL_TIM_Base_Init(&htim3);
HAL_TIM_Base_Start_IT(&htim3);
time_LCM1602_init();
HAL_ADC_Start_IT(&hadc1);
sprintf(uart_buf, "System Initialized\n");
HAL_UART_Transmit(&huart3, (uint8_t *)&uart_buf, sizeof(uart_buf), 100);
while (1)
{
if (mode == 0)
{
if (update_lcd_flag)
{
update_lcd_flag = 0;
update_lcd_time();
}
}
else if (mode == 1 || mode == 2 || mode == 3)
{
handle_buttons();
}
if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_SET)
{
mode++;
if (mode > 3)
{
mode = 0;
sTime.Hours = hour_now;
sTime.Minutes = min_now;
sTime.Seconds = sec_now;
HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
sDate.Year = year_now;
sDate.Month = month_now;
sDate.Date = date_now;
sDate.WeekDay = weekday_now;
HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
}
if (mode == 1)
{
set_step = 0;
update_lcd_set_time();
}
else if (mode == 2)
{
set_step = 0;
update_lcd_set_date();
}
else if (mode == 3)
{
set_step = 0;
update_lcd_set_alarm();
}
HAL_Delay(200);
}
if (alarm_triggered)
{
lcd_alarm();
alarm_triggered = 0;
}
}
}
<lcd.c>
/* Includes ------------------------------------------------------------------*/
//#include "lcm1602.h"
#include "main.h"
#include "stm32f4xx_hal.h"
#include <stdio.h>
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
#include <stdbool.h>
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
extern I2C_HandleTypeDef hi2c1; //extern: 변수 공유
extern UART_HandleTypeDef huart3; //
extern RTC_DateTypeDef sDate;
extern RTC_TimeTypeDef sTime;
extern RTC_AlarmTypeDef sAlarm;
extern RTC_HandleTypeDef hrtc;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
#include <string.h>
#define LCD_ADDR (0x27 << 1)
#define PIN_RS (1 << 0)
#define PIN_EN (1 << 2)
#define BACKLIGHT (1 << 3)
#define LCD_DELAY_MS 5
extern HAL_StatusTypeDef LCD_SendInternal(uint8_t lcd_addr, uint8_t data, uint8_t flags);
extern void LCD_SendCommand(uint8_t lcd_addr, uint8_t cmd);
extern void LCD_SendData(uint8_t lcd_addr, uint8_t data);
extern void LCD_Init(uint8_t lcd_addr);
extern void LCD_SendString(uint8_t lcd_addr, char *str);
extern void LCM1602_init();
extern void loop();
extern unsigned int hour_now, min_now, sec_now;
extern unsigned int hour_alr, min_alr, sec_alr;
extern unsigned int year_now, month_now, date_now, weekday_now;
extern unsigned int alarm_hour, alarm_minute, alarm_second;
void I2C_Scan()
{
char info[] = "Scanning I2C bus...\r\n";
HAL_UART_Transmit(&huart3, (uint8_t*)info, strlen(info), HAL_MAX_DELAY);
HAL_StatusTypeDef res;
for(uint16_t i = 0; i < 128; i++) {
res = HAL_I2C_IsDeviceReady(&hi2c1, i << 1, 1, 10);
if(res == HAL_OK) {
char msg[64];
snprintf(msg, sizeof(msg), "0x%02X", i);
HAL_UART_Transmit(&huart3, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
} else {
HAL_UART_Transmit(&huart3, (uint8_t*)".", 1, HAL_MAX_DELAY);
}
}
HAL_UART_Transmit(&huart3, (uint8_t*)"\r\n", 2, HAL_MAX_DELAY);
}
HAL_StatusTypeDef LCD_SendInternal(uint8_t lcd_addr, uint8_t data, uint8_t flags)
{
HAL_StatusTypeDef res;
for(;;) {
res = HAL_I2C_IsDeviceReady(&hi2c1, lcd_addr, 1, HAL_MAX_DELAY);
if(res == HAL_OK)
break;
}
uint8_t up = data & 0xF0;
uint8_t lo = (data << 4) & 0xF0;
uint8_t data_arr[4];
data_arr[0] = up|flags|BACKLIGHT|PIN_EN;
data_arr[1] = up|flags|BACKLIGHT;
data_arr[2] = lo|flags|BACKLIGHT|PIN_EN;
data_arr[3] = lo|flags|BACKLIGHT;
res = HAL_I2C_Master_Transmit(&hi2c1, lcd_addr, data_arr, sizeof(data_arr), HAL_MAX_DELAY);
HAL_Delay(LCD_DELAY_MS);
return res;
}
void LCD_SendCommand(uint8_t lcd_addr, uint8_t cmd) {
LCD_SendInternal(lcd_addr, cmd, 0);
}
void LCD_SendData(uint8_t lcd_addr, uint8_t data) {
LCD_SendInternal(lcd_addr, data, PIN_RS);
}
void LCD_Init(uint8_t lcd_addr) {
// 4-bit mode, 2 lines, 5x7 format
LCD_SendCommand(lcd_addr, 0b00110000);
// display & cursor home (keep this!)
LCD_SendCommand(lcd_addr, 0b00000010);
// display on, right shift, underline off, blink off
LCD_SendCommand(lcd_addr, 0b00001100);
// clear display (optional here)
LCD_SendCommand(lcd_addr, 0b00000001);
}
void LCD_SendString(uint8_t lcd_addr, char *str) {
while(*str) {
LCD_SendData(lcd_addr, (uint8_t)(*str));
str++;
}
}
void LCM1602_init() {
I2C_Scan();
LCD_Init(LCD_ADDR);
//LCD_Clear(LCD_ADDR);
// set address to 0x00
LCD_SendCommand(LCD_ADDR, 0b10000000);
LCD_SendString(LCD_ADDR, " Using 1602 LCD");
// set address to 0x40
LCD_SendCommand(LCD_ADDR, 0b11000000);
LCD_SendString(LCD_ADDR, " over I2C bus");
}
const char* getWeekdayString(uint8_t weekday)
{ const char* WeekdayStrings[] =
{
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
return WeekdayStrings[weekday % 7];
}
void time_LCM1602_init()
{
char upline[100];
char downline[100];
sAlarm.AlarmTime.Hours = 20;
sAlarm.AlarmTime.Minutes = 40;
sAlarm.AlarmTime.Seconds = 10;
sAlarm.Alarm = RTC_ALARM_A;
LCD_Init(LCD_ADDR);
}
void update_lcd_time()
{
char upline[100];
char downline[100];
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
sprintf(upline, "20%02d-%02d-%02d %s ",
sDate.Year, sDate.Month, sDate.Date, getWeekdayString(sDate.WeekDay));
sprintf(downline, "%02d : %02d : %02d ",
sTime.Hours, sTime.Minutes, sTime.Seconds);
LCD_SendCommand(LCD_ADDR, 0b10000000);
LCD_SendString(LCD_ADDR, upline);
LCD_SendCommand(LCD_ADDR, 0b11000000);
LCD_SendString(LCD_ADDR, downline);
}
void update_lcd_set_time(void)
{
char upline[100];
char downline[100];
char blank[100];
sprintf(blank, " ");
LCD_SendCommand(LCD_ADDR, 0b10000000);
LCD_SendString(LCD_ADDR, blank);
sprintf(upline, "Set Time:");
sprintf(downline, "%02d : %02d : %02d ", hour_now, min_now, sec_now);
LCD_SendCommand(LCD_ADDR, 0b10000000);
LCD_SendString(LCD_ADDR, upline);
LCD_SendCommand(LCD_ADDR, 0b11000000);
LCD_SendString(LCD_ADDR, downline);
}
void update_lcd_set_date(void)
{
char upline[100];
char downline[100];
char blank[100];
sprintf(blank, " ");
LCD_SendCommand(LCD_ADDR, 0b10000000);
LCD_SendString(LCD_ADDR, blank);
sprintf(upline, "Set Date:");
sprintf(downline, "20%02d-%02d-%02d %s", year_now, month_now, date_now, getWeekdayString(weekday_now));
LCD_SendCommand(LCD_ADDR, 0b10000000);
LCD_SendString(LCD_ADDR, upline);
LCD_SendCommand(LCD_ADDR, 0b11000000);
LCD_SendString(LCD_ADDR, downline);
}
void update_lcd_set_alarm(void)
{
char upline[100];
char downline[100];
char blank[100];
sprintf(blank, " ");
LCD_SendCommand(LCD_ADDR, 0b10000000);
LCD_SendString(LCD_ADDR, blank);
LCD_SendCommand(LCD_ADDR, 0b11000000);
LCD_SendString(LCD_ADDR, blank);
sprintf(upline, "Set Alarm:");
sprintf(downline, "%02d : %02d : %02d ", alarm_hour, alarm_minute, alarm_second);
LCD_SendCommand(LCD_ADDR, 0b10000000);
LCD_SendString(LCD_ADDR, upline);
LCD_SendCommand(LCD_ADDR, 0b11000000);
LCD_SendString(LCD_ADDR, downline);
}
void LCD_Clear(uint8_t lcd_addr)
{
LCD_SendCommand(lcd_addr, 0b00000001); // Clear display command
HAL_Delay(2);
}
<rtc.c> // 알람 초기값인 13:50:10에 알람 안울리게 하고 싶어서 그부분 지원놓고, chat gpt에서 추가하라는 코드를 추가함
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
//RTC_AlarmTypeDef sAlarm = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** 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();
}
/* USER CODE BEGIN Check_RTC_BKUP */
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x13;
sTime.Minutes = 0x50;
sTime.Seconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JUNE;
sDate.Date = 0x14;
sDate.Year = 0x24;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/** Enable the Alarm A
*/
/*
sAlarm.AlarmTime.Hours = 0x13;
sAlarm.AlarmTime.Minutes = 0x50;
sAlarm.AlarmTime.Seconds = 0x10;
sAlarm.AlarmTime.SubSeconds = 0x0;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 0x1;
sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
*/
/* USER CODE BEGIN RTC_Init 2 */
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0); //add-이 부분 추가 한 것
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); //add-이 부분 추가한 것
/* USER CODE END RTC_Init 2 */
}
댓글 0
조회수 129등록된 댓글이 없습니다.