DATE_GET_WEEK 和 WEEK_GET_FIRST_DAY

本文介绍两个函数模块:获取指定日期所在周及该周的第一天。这些功能已整合进HR_GBSSP_GET_WEEK_DATES函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function date_get_week.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"       IMPORTING
*"             VALUE(DATE) LIKE  SCAL-DATE
*"       EXPORTING
*"             VALUE(WEEK) LIKE  SCAL-WEEK
*"       EXCEPTIONS
*"              DATE_INVALID
*"----------------------------------------------------------------------

函数模块作用:
获得某个日期所在的周。
=============================================
function week_get_first_day.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(WEEK) LIKE  SCAL-WEEK
*"  EXPORTING
*"     VALUE(DATE) LIKE  SCAL-DATE
*"  EXCEPTIONS
*"      WEEK_INVALID
*"----------------------------------------------------------------------

函数模块作用:
获得某周的第一天。
============================================
上述两个函数的功能已经包含在 HR_GBSSP_GET_WEEK_DATES 中了。

转载于:https://www.cnblogs.com/qiangsheng/archive/2008/05/21/1203760.html

WITH un_hatchu_taisho_item_data_json( get_information ) AS ( VALUES ( #{unHatchuTaishoItemDataJson} ::JSON ) ), un_hatchu_taisho_item_list AS ( SELECT json_array_elements(get_information ::json) ->> 'item_code' ::VARCHAR AS item_code ,json_array_elements(get_information ::json) ->> 'shipment' ::VARCHAR AS shipment ,json_array_elements(get_information ::json) ->> 'first_delivery_date' ::VARCHAR AS first_delivery_date ,json_array_elements(get_information ::json) ->> 'delivery_date' ::VARCHAR AS delivery_date ,json_array_elements(get_information ::json) ->> 'pattern_type' ::VARCHAR AS pattern_type ,json_array_elements(get_information ::json) ->> 'pattern_code' ::VARCHAR AS pattern_code ,json_array_elements(get_information ::json) ->> 'delivery_schedule_code' ::VARCHAR AS delivery_schedule_code ,json_array_elements(get_information ::json) ->> 'host_cycle_code' ::VARCHAR AS host_cycle_code ,json_array_elements(get_information ::json) ->> 'store_cycle_code' ::VARCHAR AS store_cycle_code ,json_array_elements(get_information ::json) ->> 'lead_time' ::VARCHAR AS lead_time ,json_array_elements(get_information ::json) ->> 'new_week_type' ::VARCHAR AS new_week_type FROM un_hatchu_taisho_item_data_json ) </if> <if test='unHatchuTaishoItemDataJson == "{}"' > un_hatchu_taisho_item_list AS ( SELECT NULL AS item_code ,NULL AS shipment ,NULL AS first_delivery_date ,NULL AS delivery_date ,NULL AS pattern_type ,NULL AS pattern_code ,NULL AS delivery_schedule_code ,NULL AS host_cycle_code ,NULL AS store_cycle_code ,NULL AS lead_time ,NULL AS new_week_type ) </if> , next_tentative_order_dateWK AS ( SELECT hhas.item_code AS item_code , hhas.shipment AS shipment , hhas.first_delivery_date :: DATE AS first_delivery_date , hhas.delivery_date :: DATE AS delivery_date , MIN(hsm.order_date) :: DATE AS cost_unit_item_next_plan_order_date , MIN(hsm2.order_date) :: DATE AS forward_tilt_item_next_plan_order_date , MAX(hhas.pattern_type) AS pattern_type , MAX(hhas.pattern_code) AS pattern_code FROM un_hatchu_taisho_item_list hhas INNER JOIN m_order_schedule hsm ON hsm.pattern_type = hhas.pattern_type AND hsm.pattern_code = hhas.pattern_code AND hsm.delivery_schedule_code = hhas.delivery_schedule_code AND hsm.order_date > #{specifyOrderDate} :: DATE AND hsm.host_cycle_code = hhas.host_cycle_code AND hsm.store_cycle_code = hhas.store_cycle_code AND hsm.lead_time = hhas.lead_time AND hsm.version = #{orderScheduleVersion} LEFT OUTER JOIN m_order_schedule hsm2 ON hsm2.pattern_type = hhas.pattern_type AND hsm2.pattern_code = hhas.pattern_code AND hsm2.delivery_schedule_code = hhas.delivery_schedule_code AND hsm2.order_date > #{specifyOrderDate} :: DATE AND hsm2.host_cycle_code = hhas.host_cycle_code AND hsm2.store_cycle_code = hhas.store_cycle_code AND hsm2.lead_time = hhas.lead_time AND hsm2.version = #{orderScheduleVersion} AND hhas.first_delivery_date IS NOT NULL AND hsm2.delivery_date > hhas.first_delivery_date :: DATE WHERE hhas.new_week_type <> '9' GROUP BY hhas.item_code , hhas.shipment , hhas.first_delivery_date , hhas.delivery_date ) ,next_order_dateWK AS ( SELECT jph.item_code AS item_code , jph.shipment AS shipment , jph.first_delivery_date AS first_delivery_date , jph.delivery_date AS delivery_date , CASE WHEN jph.cost_unit_item_next_plan_order_date >= #{nextWeekMonday}:: DATE THEN MIN(hsm3.order_date) ELSE jph.cost_unit_item_next_plan_order_date END AS cost_unit_item_next_order_date , CASE WHEN jph.forward_tilt_item_next_plan_order_date >= #{nextWeekMonday}:: DATE THEN MIN(hsm4.order_date) ELSE jph.forward_tilt_item_next_plan_order_date END AS forward_tilt_item_next_order_date , MAX(jph.pattern_type) AS pattern_type , MAX(jph.pattern_code) AS pattern_code FROM next_tentative_order_dateWK jph INNER JOIN m_item sm ON sm.item_code = jph.item_code AND sm.shipment = jph.shipment AND sm.pattern_type = jph.pattern_type AND sm.pattern_code = jph.pattern_code AND sm.apply_start_date <= jph.cost_unit_item_next_plan_order_date AND sm.version = #{itemVersion} AND sm.apply_end_date >= jph.cost_unit_item_next_plan_order_date AND sm.order_start_date <= jph.cost_unit_item_next_plan_order_date AND sm.order_end_date >= jph.cost_unit_item_next_plan_order_date INNER JOIN m_pattern pm ON pm.original_store_code = #{originalStoreCode} AND pm.pattern_type = sm.pattern_type AND pm.apply_start_date <= jph.cost_unit_item_next_plan_order_date AND pm.pattern_code = sm.pattern_code AND pm.version = #{patternVersion} AND pm.apply_end_date >= jph.cost_unit_item_next_plan_order_date LEFT OUTER JOIN m_order_schedule hsm3 ON hsm3.pattern_type = sm.pattern_type AND hsm3.pattern_code = sm.pattern_code AND ( hsm3.delivery_schedule_code = '0' OR hsm3.delivery_schedule_code IN ( SELECT nkm.delivery_schedule_code FROM m_delivery_type nkm WHERE nkm.original_store_code = #{originalStoreCode} AND nkm.host_cycle_code = #{hostCycleCode} AND nkm.apply_start_date <= jph.cost_unit_item_next_plan_order_date AND nkm.version = #{deliveryTypeVersion} AND nkm.apply_end_date >= jph.cost_unit_item_next_plan_order_date ) ) AND hsm3.order_date >= #{nextWeekMonday} :: DATE AND hsm3.host_cycle_code = sm.host_cycle_code AND hsm3.store_cycle_code = sm.store_cycle_code AND hsm3.lead_time = sm.lead_time AND hsm3.version = #{orderScheduleVersion} LEFT OUTER JOIN m_item sm2 ON sm2.item_code = jph.item_code AND sm2.shipment = jph.shipment AND sm2.pattern_type = jph.pattern_type AND sm2.pattern_code = jph.pattern_code AND sm2.apply_start_date <= jph.forward_tilt_item_next_plan_order_date AND sm2.version = #{itemVersion} AND sm2.apply_end_date >= jph.forward_tilt_item_next_plan_order_date AND sm2.order_start_date <= jph.forward_tilt_item_next_plan_order_date AND sm2.order_end_date >= jph.forward_tilt_item_next_plan_order_date LEFT OUTER JOIN m_pattern pm2 ON pm2.original_store_code = #{originalStoreCode} AND pm2.pattern_type = sm2.pattern_type AND pm2.apply_start_date <= jph.forward_tilt_item_next_plan_order_date AND pm2.pattern_code = sm2.pattern_code AND pm2.version = #{patternVersion} AND pm2.apply_end_date >= jph.forward_tilt_item_next_plan_order_date LEFT OUTER JOIN m_order_schedule hsm4 ON hsm4.pattern_type = sm.pattern_type AND hsm4.pattern_code = sm.pattern_code AND ( hsm4.delivery_schedule_code = '0' OR hsm4.delivery_schedule_code IN ( SELECT nkm2.delivery_schedule_code FROM m_delivery_type nkm2 WHERE nkm2.original_store_code = #{originalStoreCode} AND nkm2.host_cycle_code = #{hostCycleCode} AND nkm2.apply_start_date <= jph.forward_tilt_item_next_plan_order_date AND nkm2.version = #{deliveryTypeVersion} AND nkm2.apply_end_date >= jph.forward_tilt_item_next_plan_order_date ) ) AND hsm4.order_date >= #{nextWeekMonday} :: DATE AND hsm4.delivery_date > jph.first_delivery_date AND hsm4.host_cycle_code = sm2.host_cycle_code AND hsm4.store_cycle_code = sm2.store_cycle_code AND hsm4.lead_time = sm2.lead_time AND hsm4.version = #{orderScheduleVersion} GROUP BY jph.item_code , jph.shipment , jph.first_delivery_date , jph.delivery_date , jph.cost_unit_item_next_plan_order_date , jph.forward_tilt_item_next_plan_order_date ) SELECT jh.item_code AS item_code , jh.shipment AS shipment , hsm5.delivery_date AS cost_unit_item_next_delivery_date , hsm6.delivery_date AS forward_tilt_item_next_delivery_date , CASE WHEN hsm5.delivery_date IS NOT NULL THEN hsm5.delivery_date ::date - INTERVAL '1 day' ELSE jh.delivery_date END AS sum_end_date FROM next_order_dateWK jh INNER JOIN m_item sm3 ON sm3.item_code = jh.item_code AND sm3.shipment = jh.shipment AND sm3.pattern_type = jh.pattern_type AND sm3.pattern_code = jh.pattern_code AND sm3.apply_start_date <= jh.cost_unit_item_next_order_date AND sm3.version = #{itemVersion} AND sm3.apply_end_date >= jh.cost_unit_item_next_order_date AND sm3.order_start_date <= jh.cost_unit_item_next_order_date AND sm3.order_end_date >= jh.cost_unit_item_next_order_date INNER JOIN m_pattern pm3 ON pm3.original_store_code = #{originalStoreCode} AND pm3.pattern_type = sm3.pattern_type AND pm3.apply_start_date <= jh.cost_unit_item_next_order_date AND pm3.version = #{patternVersion} AND pm3.pattern_code = sm3.pattern_code AND pm3.apply_end_date >= jh.cost_unit_item_next_order_date LEFT OUTER JOIN m_order_schedule hsm5 ON hsm5.pattern_type = sm3.pattern_type AND hsm5.pattern_code = sm3.pattern_code AND ( hsm5.delivery_schedule_code = '0' OR hsm5.delivery_schedule_code IN ( SELECT nkm7.delivery_schedule_code FROM m_delivery_type nkm7 WHERE nkm7.original_store_code = #{originalStoreCode} AND nkm7.host_cycle_code = #{hostCycleCode} AND nkm7.apply_start_date <= jh.cost_unit_item_next_order_date AND nkm7.version = #{deliveryTypeVersion} AND nkm7.apply_end_date >= jh.cost_unit_item_next_order_date ) ) AND hsm5.order_date = jh.cost_unit_item_next_order_date AND hsm5.host_cycle_code = sm3.host_cycle_code AND hsm5.store_cycle_code = sm3.store_cycle_code AND hsm5.lead_time = sm3.lead_time AND hsm5.version = #{orderScheduleVersion} LEFT OUTER JOIN m_item sm4 ON sm4.item_code = jh.item_code AND sm4.shipment = jh.shipment AND sm4.pattern_type = jh.pattern_type AND sm4.pattern_code = jh.pattern_code AND sm4.apply_start_date <= jh.forward_tilt_item_next_order_date AND sm4.version = #{itemVersion} AND sm4.apply_end_date >= jh.forward_tilt_item_next_order_date AND sm4.order_start_date <= jh.forward_tilt_item_next_order_date AND sm4.order_end_date >= jh.forward_tilt_item_next_order_date LEFT OUTER JOIN m_pattern pm4 ON pm4.original_store_code = #{originalStoreCode} AND pm4.pattern_type = sm4.pattern_type AND pm4.apply_start_date <= jh.forward_tilt_item_next_order_date AND pm4.version = #{patternVersion} AND pm4.pattern_code = sm4.pattern_code AND pm4.apply_end_date >= jh.forward_tilt_item_next_order_date LEFT OUTER JOIN m_order_schedule hsm6 ON hsm6.pattern_type = sm4.pattern_type AND hsm6.pattern_code = sm4.pattern_code AND ( hsm6.delivery_schedule_code = '0' OR hsm6.delivery_schedule_code IN ( SELECT nkm4.delivery_schedule_code FROM m_delivery_type nkm4 WHERE nkm4.original_store_code = #{originalStoreCode} AND nkm4.host_cycle_code = #{hostCycleCode} AND nkm4.apply_start_date <= jh.forward_tilt_item_next_order_date AND nkm4.version = #{deliveryTypeVersion} AND nkm4.apply_end_date >= jh.forward_tilt_item_next_order_date ) ) AND hsm6.order_date = jh.forward_tilt_item_next_order_date AND hsm6.delivery_date > jh.first_delivery_date AND hsm6.host_cycle_code = sm4.host_cycle_code AND hsm6.store_cycle_code = sm4.store_cycle_code AND hsm6.lead_time = sm4.lead_time AND hsm6.version = #{orderScheduleVersion} GROUP BY jh.item_code ,jh.shipment ,hsm5.delivery_date ,hsm6.delivery_date ,jh.delivery_date 判断一下 LEFT OUTER JOIN m_order_schedule hsm2 最后用到了什么地方
08-05
程序1: #include "RTC.h" #define RTC_CLOCK_SOURCE_LXTAL // ÅäÖÃRTCʱÖÓÔ´ #define BKP_VALUE 0x32F0 rtc_parameter_struct rtc_initpara; rtc_alarm_struct rtc_alarm; __IO uint32_t prescaler_a = 0, prescaler_s = 0; uint32_t RTCSRC_FLAG = 0; /*! \brief main function \param[in] none \param[out] none \retval none */ void RTC_Init(void) { printf("\n\r ****************** RTC calendar demo ******************\n\r"); /* enable PMU clock */ rcu_periph_clock_enable(RCU_PMU); /* enable the access of the RTC registers */ pmu_backup_write_enable(); rtc_pre_config(); /* get RTC clock entry selection */ RTCSRC_FLAG = GET_BITS(RCU_BDCTL, 8, 9); /* check if RTC has aready been configured */ if((BKP_VALUE != RTC_BKP0) || (0x00 == RTCSRC_FLAG)){ /* backup data register value is not correct or not yet programmed or RTC clock source is not configured (when the first time the program is executed or data in RCU_BDCTL is lost due to Vbat feeding) */ rtc_setup(); }else{ /* detect the reset source */ if (RESET != rcu_flag_get(RCU_FLAG_PORRST)){ printf("power on reset occurred....\n\r"); }else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)){ printf("external reset occurred....\n\r"); } printf("no need to configure RTC....\n\r"); rtc_show_time(); } rcu_all_reset_flag_clear(); } /*! \brief RTC configuration function \param[in] none \param[out] none \retval none */ void rtc_pre_config(void) { #if defined (RTC_CLOCK_SOURCE_IRC32K) rcu_osci_on(RCU_IRC32K); rcu_osci_stab_wait(RCU_IRC32K); rcu_rtc_clock_config(RCU_RTCSRC_IRC32K); prescaler_s = 0x13F; prescaler_a = 0x63; #elif defined (RTC_CLOCK_SOURCE_LXTAL) rcu_osci_on(RCU_LXTAL); rcu_osci_stab_wait(RCU_LXTAL); rcu_rtc_clock_config(RCU_RTCSRC_LXTAL); prescaler_s = 0xFF; prescaler_a = 0x7F; #else #error RTC clock source should be defined. #endif /* RTC_CLOCK_SOURCE_IRC32K */ rcu_periph_clock_enable(RCU_RTC); rtc_register_sync_wait(); } /*! \brief use hyperterminal to setup RTC time and alarm \param[in] none \param[out] none \retval none */ void rtc_setup(void) { /* setup RTC time value */ uint32_t tmp_year = 0xFF, tmp_month = 0xFF, tmp_day = 0xFF; uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF; rtc_initpara.factor_asyn = prescaler_a; rtc_initpara.factor_syn = prescaler_s; rtc_initpara.year = 0x16; rtc_initpara.day_of_week = RTC_SATURDAY; rtc_initpara.month = RTC_APR; rtc_initpara.date = 0x30; rtc_initpara.display_format = RTC_24HOUR; rtc_initpara.am_pm = RTC_AM; /* current time input */ printf("=======Configure RTC Time========\n\r"); printf(" please set the last two digits of current year:\n\r"); while(tmp_year == 0xFF) { tmp_year = usart_input_threshold(99); rtc_initpara.year = tmp_year; } printf(" 20%0.2x\n\r", tmp_year); printf(" please input month:\n\r"); while(tmp_month == 0xFF) { tmp_month = usart_input_threshold(12); rtc_initpara.month = tmp_month; } printf(" %0.2x\n\r", tmp_month); printf(" please input day:\n\r"); while(tmp_day == 0xFF) { tmp_day = usart_input_threshold(31); rtc_initpara.date = tmp_day; } printf(" %0.2x\n\r", tmp_day); printf(" please input hour:\n\r"); while (0xFF == tmp_hh){ tmp_hh = usart_input_threshold(23); rtc_initpara.hour = tmp_hh; } printf(" %0.2x\n\r", tmp_hh); printf(" please input minute:\n\r"); while (0xFF == tmp_mm){ tmp_mm = usart_input_threshold(59); rtc_initpara.minute = tmp_mm; } printf(" %0.2x\n\r", tmp_mm); printf(" please input second:\n\r"); while (0xFF == tmp_ss){ tmp_ss = usart_input_threshold(59); rtc_initpara.second = tmp_ss; } printf(" %0.2x\n\r", tmp_ss); /* RTC current time configuration */ if(ERROR == rtc_init(&rtc_initpara)){ printf("\n\r** RTC time configuration failed! **\n\r"); }else{ printf("\n\r** RTC time configuration success! **\n\r"); rtc_show_time(); RTC_BKP0 = BKP_VALUE; } // /* setup RTC alarm */ // tmp_hh = 0xFF; // tmp_mm = 0xFF; // tmp_ss = 0xFF; // rtc_alarm_disable(RTC_ALARM0); // printf("=======Input Alarm Value=======\n\r"); // rtc_alarm.alarm_mask = RTC_ALARM_DATE_MASK|RTC_ALARM_HOUR_MASK|RTC_ALARM_MINUTE_MASK; // rtc_alarm.weekday_or_date = RTC_ALARM_DATE_SELECTED; // rtc_alarm.alarm_day = 0x31; // rtc_alarm.am_pm = RTC_AM; // /* RTC alarm input */ // printf(" please input Alarm Hour:\n\r"); // while (0xFF == tmp_hh){ // tmp_hh = usart_input_threshold(23); // rtc_alarm.alarm_hour = tmp_hh; // } // printf(" %0.2x\n\r", tmp_hh); // printf(" Please Input Alarm Minute:\n\r"); // while (0xFF == tmp_mm){ // tmp_mm = usart_input_threshold(59); // rtc_alarm.alarm_minute = tmp_mm; // } // printf(" %0.2x\n\r", tmp_mm); // printf(" Please Input Alarm Second:\n\r"); // while (0xFF == tmp_ss){ // tmp_ss = usart_input_threshold(59); // rtc_alarm.alarm_second = tmp_ss; // } // printf(" %0.2x", tmp_ss); // /* RTC alarm configuration */ // rtc_alarm_config(RTC_ALARM0,&rtc_alarm); // printf("\n\r** RTC Set Alarm Success! **\n\r"); // rtc_show_alarm(); // rtc_interrupt_enable(RTC_INT_ALARM0); // rtc_alarm_enable(RTC_ALARM0); } /*! \brief display the current time \param[in] none \param[out] none \retval none */ void rtc_show_time(void) { // uint32_t time_subsecond = 0; // uint8_t subsecond_ss = 0,subsecond_ts = 0,subsecond_hs = 0; rtc_current_time_get(&rtc_initpara); /* get the subsecond value of current time, and convert it into fractional format */ // time_subsecond = rtc_subsecond_get(); // subsecond_ss=(1000-(time_subsecond*1000+1000)/400)/100; // subsecond_ts=(1000-(time_subsecond*1000+1000)/400)%100/10; // subsecond_hs=(1000-(time_subsecond*1000+1000)/400)%10; printf("\r\nCurrent time: 20%0.2x-%0.2x-%0.2x", \ rtc_initpara.year, rtc_initpara.month, rtc_initpara.date); printf(" : %0.2x:%0.2x:%0.2x \r\n", \ rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); } /*! \brief display the alram value \param[in] none \param[out] none \retval none */ void rtc_show_alarm(void) { rtc_alarm_get(RTC_ALARM0,&rtc_alarm); printf("The alarm: %0.2x:%0.2x:%0.2x \n\r", rtc_alarm.alarm_hour, rtc_alarm.alarm_minute,\ rtc_alarm.alarm_second); } /*! \brief get the input character string and check if it is valid \param[in] none \param[out] none \retval input value in BCD mode */ uint8_t usart_input_threshold(uint32_t value) { uint32_t index = 0; uint32_t tmp[2] = {0, 0}; while (index < 2){ while (RESET == usart_flag_get(USART0, USART_FLAG_RBNE)); tmp[index++] = usart_data_receive(USART0); if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){ printf("\n\r please input a valid number between 0 and 9 \n\r"); index--; } } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10); if (index > value){ printf("\n\r please input a valid number between 0 and %d \n\r", value); return 0xFF; } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) <<4); return index; } 程序2: /************************************************************ * °æÈ¨£º2025CIMC Copyright¡£ * Îļþ£ºRTC.c * ×÷Õß: Qiao Qin @ GigaDevice * ƽ̨: 2025CIMC IHD-V04 * °æ±¾: Qiao Qin 2025/4/20 V0.01 original ************************************************************/ #include "RTC.h" #define RTC_CLOCK_SOURCE_LXTAL // ÅäÖÃRTCʱÖÓÔ´ #define BKP_VALUE 0x32F0 rtc_parameter_struct rtc_initpara; rtc_alarm_struct rtc_alarm; __IO uint32_t prescaler_a = 0, prescaler_s = 0; uint32_t RTCSRC_FLAG = 0; /*! \brief main function \param[in] none \param[out] none \retval none */ void RTC_Init(void) { printf("\n\r ****************** RTC calendar demo ******************\n\r"); /* enable PMU clock */ rcu_periph_clock_enable(RCU_PMU); /* enable the access of the RTC registers */ pmu_backup_write_enable(); rtc_pre_config(); printf("Configuring RTC on every initialization...\n\r"); char cmd_buffer[32] = "2000-01-01 00:02:51"; rtc_settime((const char*)cmd_buffer); // ????RTC?? if (RESET != rcu_flag_get(RCU_FLAG_PORRST)) { printf("power on reset occurred....\n\r"); } else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)) { printf("external reset occurred....\n\r"); } rtc_show_time(); // ?????? rcu_all_reset_flag_clear(); } /*! \brief RTC configuration function \param[in] none \param[out] none \retval none */ void rtc_pre_config(void) { #if defined (RTC_CLOCK_SOURCE_IRC32K) rcu_osci_on(RCU_IRC32K); rcu_osci_stab_wait(RCU_IRC32K); rcu_rtc_clock_config(RCU_RTCSRC_IRC32K); prescaler_s = 0x13F; prescaler_a = 0x63; #elif defined (RTC_CLOCK_SOURCE_LXTAL) rcu_osci_on(RCU_LXTAL); rcu_osci_stab_wait(RCU_LXTAL); rcu_rtc_clock_config(RCU_RTCSRC_LXTAL); prescaler_s = 0xFF; prescaler_a = 0x7F; #else #error RTC clock source should be defined. #endif /* RTC_CLOCK_SOURCE_IRC32K */ rcu_periph_clock_enable(RCU_RTC); rtc_register_sync_wait(); } /*! \brief use hyperterminal to setup RTC time and alarm \param[in] none \param[out] none \retval none */ void rtc_settime(const char *datetime) { /* setup RTC time value */ uint32_t tmp_year = 0, tmp_month = 0, tmp_day = 0; uint32_t tmp_hh = 0, tmp_mm = 0, tmp_ss = 0; if (sscanf(datetime, "%4u-%2u-%2u %2u:%2u:%2u", &tmp_year, &tmp_month, &tmp_day, &tmp_hh, &tmp_mm, &tmp_ss) != 6) { printf(" ERROR: Invalid time format!\n\r"); return; } rtc_initpara.factor_asyn = prescaler_a; rtc_initpara.factor_syn = prescaler_s; rtc_initpara.year = 0x16; rtc_initpara.day_of_week = RTC_SATURDAY; rtc_initpara.month = RTC_APR; rtc_initpara.date = 0x30; rtc_initpara.display_format = RTC_24HOUR; rtc_initpara.am_pm = RTC_AM; // Äê·Ý(2000-2099) if (tmp_year < 2000 || tmp_year > 2099) { printf(" ERROR: Year out of range [2000-2099]!\n\r"); return; } rtc_initpara.year = tmp_year % 100; // ???? // ÔÂ·Ý (1-12) if (tmp_month < 1 || tmp_month > 12) { printf(" ERROR: Month out of range [1-12]!\n\r"); return; } rtc_initpara.month = tmp_month; // ÈÕÆÚ (1-31) if (tmp_day < 1 || tmp_day > 31) { printf(" ERROR: Day out of range [1-31]!\n\r"); return; } rtc_initpara.date = tmp_day; // Сʱ(0-23) if (tmp_hh > 23) { printf(" ERROR: Hour out of range [0-23]!\n\r"); return; } rtc_initpara.hour = tmp_hh; // ·ÖÖÓ(0-59) if (tmp_mm > 59) { printf(" ERROR: Minute out of range [0-59]!\n\r"); return; } rtc_initpara.minute = tmp_mm; // mÃëÖÓ(0-59) if (tmp_ss > 59) { printf(" ERROR: Second out of range [0-59]!\n\r"); return; } rtc_initpara.second = tmp_ss; printf("\n\rRTC Config success\n\r"); printf("Time: 20%02u-%02u-%02u %02u:%02u:%02u\n\r", rtc_initpara.year, rtc_initpara.month, rtc_initpara.date, rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); /*RTC³õʼ»¯*/ if (ERROR == rtc_init(&rtc_initpara)) { // printf("\n\r** RTC configuration failed! **\n\r"); } else { // printf("\n\r** RTC configuration success! **\n\r"); RTC_BKP0 = BKP_VALUE; } } /*! \brief display the current time \param[in] none \param[out] none \retval none */ void rtc_show_time(void) { rtc_current_time_get(&rtc_initpara); printf("\r\nCurrent Time: 20%02u-%02u-%02u %02u:%02u:%02u\n\r", rtc_initpara.year, rtc_initpara.month, rtc_initpara.date, rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); } /*! \brief display the alram value \param[in] none \param[out] none \retval none */ void rtc_show_alarm(void) { rtc_alarm_get(RTC_ALARM0,&rtc_alarm); printf("The alarm: %0.2x:%0.2x:%0.2x \n\r", rtc_alarm.alarm_hour, rtc_alarm.alarm_minute,\ rtc_alarm.alarm_second); } /*! \brief get the input character string and check if it is valid \param[in] none \param[out] none \retval input value in BCD mode */ uint8_t usart_input_threshold(uint32_t value) { uint32_t index = 0; uint32_t tmp[2] = {0, 0}; while (index < 2){ while (RESET == usart_flag_get(USART0, USART_FLAG_RBNE)); tmp[index++] = usart_data_receive(USART0); if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){ printf("\n\r please input a valid number between 0 and 9 \n\r"); index--; } } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10); if (index > value){ printf("\n\r please input a valid number between 0 and %d \n\r", value); return 0xFF; } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) <<4); return index; } 程序1可以正常记时,程序二时间会有跳变,请改错 程序1: #include "RTC.h" #define RTC_CLOCK_SOURCE_LXTAL // ÅäÖÃRTCʱÖÓÔ´ #define BKP_VALUE 0x32F0 rtc_parameter_struct rtc_initpara; rtc_alarm_struct rtc_alarm; __IO uint32_t prescaler_a = 0, prescaler_s = 0; uint32_t RTCSRC_FLAG = 0; /*! \brief main function \param[in] none \param[out] none \retval none */ void RTC_Init(void) { printf("\n\r ****************** RTC calendar demo ******************\n\r"); /* enable PMU clock */ rcu_periph_clock_enable(RCU_PMU); /* enable the access of the RTC registers */ pmu_backup_write_enable(); rtc_pre_config(); /* get RTC clock entry selection */ RTCSRC_FLAG = GET_BITS(RCU_BDCTL, 8, 9); /* check if RTC has aready been configured */ if((BKP_VALUE != RTC_BKP0) || (0x00 == RTCSRC_FLAG)){ /* backup data register value is not correct or not yet programmed or RTC clock source is not configured (when the first time the program is executed or data in RCU_BDCTL is lost due to Vbat feeding) */ rtc_setup(); }else{ /* detect the reset source */ if (RESET != rcu_flag_get(RCU_FLAG_PORRST)){ printf("power on reset occurred....\n\r"); }else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)){ printf("external reset occurred....\n\r"); } printf("no need to configure RTC....\n\r"); rtc_show_time(); } rcu_all_reset_flag_clear(); } /*! \brief RTC configuration function \param[in] none \param[out] none \retval none */ void rtc_pre_config(void) { #if defined (RTC_CLOCK_SOURCE_IRC32K) rcu_osci_on(RCU_IRC32K); rcu_osci_stab_wait(RCU_IRC32K); rcu_rtc_clock_config(RCU_RTCSRC_IRC32K); prescaler_s = 0x13F; prescaler_a = 0x63; #elif defined (RTC_CLOCK_SOURCE_LXTAL) rcu_osci_on(RCU_LXTAL); rcu_osci_stab_wait(RCU_LXTAL); rcu_rtc_clock_config(RCU_RTCSRC_LXTAL); prescaler_s = 0xFF; prescaler_a = 0x7F; #else #error RTC clock source should be defined. #endif /* RTC_CLOCK_SOURCE_IRC32K */ rcu_periph_clock_enable(RCU_RTC); rtc_register_sync_wait(); } /*! \brief use hyperterminal to setup RTC time and alarm \param[in] none \param[out] none \retval none */ void rtc_setup(void) { /* setup RTC time value */ uint32_t tmp_year = 0xFF, tmp_month = 0xFF, tmp_day = 0xFF; uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF; rtc_initpara.factor_asyn = prescaler_a; rtc_initpara.factor_syn = prescaler_s; rtc_initpara.year = 0x16; rtc_initpara.day_of_week = RTC_SATURDAY; rtc_initpara.month = RTC_APR; rtc_initpara.date = 0x30; rtc_initpara.display_format = RTC_24HOUR; rtc_initpara.am_pm = RTC_AM; /* current time input */ printf("=======Configure RTC Time========\n\r"); printf(" please set the last two digits of current year:\n\r"); while(tmp_year == 0xFF) { tmp_year = usart_input_threshold(99); rtc_initpara.year = tmp_year; } printf(" 20%0.2x\n\r", tmp_year); printf(" please input month:\n\r"); while(tmp_month == 0xFF) { tmp_month = usart_input_threshold(12); rtc_initpara.month = tmp_month; } printf(" %0.2x\n\r", tmp_month); printf(" please input day:\n\r"); while(tmp_day == 0xFF) { tmp_day = usart_input_threshold(31); rtc_initpara.date = tmp_day; } printf(" %0.2x\n\r", tmp_day); printf(" please input hour:\n\r"); while (0xFF == tmp_hh){ tmp_hh = usart_input_threshold(23); rtc_initpara.hour = tmp_hh; } printf(" %0.2x\n\r", tmp_hh); printf(" please input minute:\n\r"); while (0xFF == tmp_mm){ tmp_mm = usart_input_threshold(59); rtc_initpara.minute = tmp_mm; } printf(" %0.2x\n\r", tmp_mm); printf(" please input second:\n\r"); while (0xFF == tmp_ss){ tmp_ss = usart_input_threshold(59); rtc_initpara.second = tmp_ss; } printf(" %0.2x\n\r", tmp_ss); /* RTC current time configuration */ if(ERROR == rtc_init(&rtc_initpara)){ printf("\n\r** RTC time configuration failed! **\n\r"); }else{ printf("\n\r** RTC time configuration success! **\n\r"); rtc_show_time(); RTC_BKP0 = BKP_VALUE; } // /* setup RTC alarm */ // tmp_hh = 0xFF; // tmp_mm = 0xFF; // tmp_ss = 0xFF; // rtc_alarm_disable(RTC_ALARM0); // printf("=======Input Alarm Value=======\n\r"); // rtc_alarm.alarm_mask = RTC_ALARM_DATE_MASK|RTC_ALARM_HOUR_MASK|RTC_ALARM_MINUTE_MASK; // rtc_alarm.weekday_or_date = RTC_ALARM_DATE_SELECTED; // rtc_alarm.alarm_day = 0x31; // rtc_alarm.am_pm = RTC_AM; // /* RTC alarm input */ // printf(" please input Alarm Hour:\n\r"); // while (0xFF == tmp_hh){ // tmp_hh = usart_input_threshold(23); // rtc_alarm.alarm_hour = tmp_hh; // } // printf(" %0.2x\n\r", tmp_hh); // printf(" Please Input Alarm Minute:\n\r"); // while (0xFF == tmp_mm){ // tmp_mm = usart_input_threshold(59); // rtc_alarm.alarm_minute = tmp_mm; // } // printf(" %0.2x\n\r", tmp_mm); // printf(" Please Input Alarm Second:\n\r"); // while (0xFF == tmp_ss){ // tmp_ss = usart_input_threshold(59); // rtc_alarm.alarm_second = tmp_ss; // } // printf(" %0.2x", tmp_ss); // /* RTC alarm configuration */ // rtc_alarm_config(RTC_ALARM0,&rtc_alarm); // printf("\n\r** RTC Set Alarm Success! **\n\r"); // rtc_show_alarm(); // rtc_interrupt_enable(RTC_INT_ALARM0); // rtc_alarm_enable(RTC_ALARM0); } /*! \brief display the current time \param[in] none \param[out] none \retval none */ void rtc_show_time(void) { // uint32_t time_subsecond = 0; // uint8_t subsecond_ss = 0,subsecond_ts = 0,subsecond_hs = 0; rtc_current_time_get(&rtc_initpara); /* get the subsecond value of current time, and convert it into fractional format */ // time_subsecond = rtc_subsecond_get(); // subsecond_ss=(1000-(time_subsecond*1000+1000)/400)/100; // subsecond_ts=(1000-(time_subsecond*1000+1000)/400)%100/10; // subsecond_hs=(1000-(time_subsecond*1000+1000)/400)%10; printf("\r\nCurrent time: 20%0.2x-%0.2x-%0.2x", \ rtc_initpara.year, rtc_initpara.month, rtc_initpara.date); printf(" : %0.2x:%0.2x:%0.2x \r\n", \ rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); } /*! \brief display the alram value \param[in] none \param[out] none \retval none */ void rtc_show_alarm(void) { rtc_alarm_get(RTC_ALARM0,&rtc_alarm); printf("The alarm: %0.2x:%0.2x:%0.2x \n\r", rtc_alarm.alarm_hour, rtc_alarm.alarm_minute,\ rtc_alarm.alarm_second); } /*! \brief get the input character string and check if it is valid \param[in] none \param[out] none \retval input value in BCD mode */ uint8_t usart_input_threshold(uint32_t value) { uint32_t index = 0; uint32_t tmp[2] = {0, 0}; while (index < 2){ while (RESET == usart_flag_get(USART0, USART_FLAG_RBNE)); tmp[index++] = usart_data_receive(USART0); if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){ printf("\n\r please input a valid number between 0 and 9 \n\r"); index--; } } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10); if (index > value){ printf("\n\r please input a valid number between 0 and %d \n\r", value); return 0xFF; } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) <<4); return index; } 程序2: /************************************************************ * °æÈ¨£º2025CIMC Copyright¡£ * Îļþ£ºRTC.c * ×÷Õß: Qiao Qin @ GigaDevice * ƽ̨: 2025CIMC IHD-V04 * °æ±¾: Qiao Qin 2025/4/20 V0.01 original ************************************************************/ #include "RTC.h" #define RTC_CLOCK_SOURCE_LXTAL // ÅäÖÃRTCʱÖÓÔ´ #define BKP_VALUE 0x32F0 rtc_parameter_struct rtc_initpara; rtc_alarm_struct rtc_alarm; __IO uint32_t prescaler_a = 0, prescaler_s = 0; uint32_t RTCSRC_FLAG = 0; /*! \brief main function \param[in] none \param[out] none \retval none */ void RTC_Init(void) { printf("\n\r ****************** RTC calendar demo ******************\n\r"); /* enable PMU clock */ rcu_periph_clock_enable(RCU_PMU); /* enable the access of the RTC registers */ pmu_backup_write_enable(); rtc_pre_config(); printf("Configuring RTC on every initialization...\n\r"); char cmd_buffer[32] = "2000-01-01 00:02:51"; rtc_settime((const char*)cmd_buffer); // ????RTC?? if (RESET != rcu_flag_get(RCU_FLAG_PORRST)) { printf("power on reset occurred....\n\r"); } else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)) { printf("external reset occurred....\n\r"); } rtc_show_time(); // ?????? rcu_all_reset_flag_clear(); } /*! \brief RTC configuration function \param[in] none \param[out] none \retval none */ void rtc_pre_config(void) { #if defined (RTC_CLOCK_SOURCE_IRC32K) rcu_osci_on(RCU_IRC32K); rcu_osci_stab_wait(RCU_IRC32K); rcu_rtc_clock_config(RCU_RTCSRC_IRC32K); prescaler_s = 0x13F; prescaler_a = 0x63; #elif defined (RTC_CLOCK_SOURCE_LXTAL) rcu_osci_on(RCU_LXTAL); rcu_osci_stab_wait(RCU_LXTAL); rcu_rtc_clock_config(RCU_RTCSRC_LXTAL); prescaler_s = 0xFF; prescaler_a = 0x7F; #else #error RTC clock source should be defined. #endif /* RTC_CLOCK_SOURCE_IRC32K */ rcu_periph_clock_enable(RCU_RTC); rtc_register_sync_wait(); } /*! \brief use hyperterminal to setup RTC time and alarm \param[in] none \param[out] none \retval none */ void rtc_settime(const char *datetime) { /* setup RTC time value */ uint32_t tmp_year = 0, tmp_month = 0, tmp_day = 0; uint32_t tmp_hh = 0, tmp_mm = 0, tmp_ss = 0; if (sscanf(datetime, "%4u-%2u-%2u %2u:%2u:%2u", &tmp_year, &tmp_month, &tmp_day, &tmp_hh, &tmp_mm, &tmp_ss) != 6) { printf(" ERROR: Invalid time format!\n\r"); return; } rtc_initpara.factor_asyn = prescaler_a; rtc_initpara.factor_syn = prescaler_s; rtc_initpara.year = 0x16; rtc_initpara.day_of_week = RTC_SATURDAY; rtc_initpara.month = RTC_APR; rtc_initpara.date = 0x30; rtc_initpara.display_format = RTC_24HOUR; rtc_initpara.am_pm = RTC_AM; // Äê·Ý(2000-2099) if (tmp_year < 2000 || tmp_year > 2099) { printf(" ERROR: Year out of range [2000-2099]!\n\r"); return; } rtc_initpara.year = tmp_year % 100; // ???? // ÔÂ·Ý (1-12) if (tmp_month < 1 || tmp_month > 12) { printf(" ERROR: Month out of range [1-12]!\n\r"); return; } rtc_initpara.month = tmp_month; // ÈÕÆÚ (1-31) if (tmp_day < 1 || tmp_day > 31) { printf(" ERROR: Day out of range [1-31]!\n\r"); return; } rtc_initpara.date = tmp_day; // Сʱ(0-23) if (tmp_hh > 23) { printf(" ERROR: Hour out of range [0-23]!\n\r"); return; } rtc_initpara.hour = tmp_hh; // ·ÖÖÓ(0-59) if (tmp_mm > 59) { printf(" ERROR: Minute out of range [0-59]!\n\r"); return; } rtc_initpara.minute = tmp_mm; // mÃëÖÓ(0-59) if (tmp_ss > 59) { printf(" ERROR: Second out of range [0-59]!\n\r"); return; } rtc_initpara.second = tmp_ss; printf("\n\rRTC Config success\n\r"); printf("Time: 20%02u-%02u-%02u %02u:%02u:%02u\n\r", rtc_initpara.year, rtc_initpara.month, rtc_initpara.date, rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); /*RTC³õʼ»¯*/ if (ERROR == rtc_init(&rtc_initpara)) { // printf("\n\r** RTC configuration failed! **\n\r"); } else { // printf("\n\r** RTC configuration success! **\n\r"); RTC_BKP0 = BKP_VALUE; } } /*! \brief display the current time \param[in] none \param[out] none \retval none */ void rtc_show_time(void) { rtc_current_time_get(&rtc_initpara); printf("\r\nCurrent Time: 20%02u-%02u-%02u %02u:%02u:%02u\n\r", rtc_initpara.year, rtc_initpara.month, rtc_initpara.date, rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second); } /*! \brief display the alram value \param[in] none \param[out] none \retval none */ void rtc_show_alarm(void) { rtc_alarm_get(RTC_ALARM0,&rtc_alarm); printf("The alarm: %0.2x:%0.2x:%0.2x \n\r", rtc_alarm.alarm_hour, rtc_alarm.alarm_minute,\ rtc_alarm.alarm_second); } /*! \brief get the input character string and check if it is valid \param[in] none \param[out] none \retval input value in BCD mode */ uint8_t usart_input_threshold(uint32_t value) { uint32_t index = 0; uint32_t tmp[2] = {0, 0}; while (index < 2){ while (RESET == usart_flag_get(USART0, USART_FLAG_RBNE)); tmp[index++] = usart_data_receive(USART0); if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)){ printf("\n\r please input a valid number between 0 and 9 \n\r"); index--; } } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10); if (index > value){ printf("\n\r please input a valid number between 0 and %d \n\r", value); return 0xFF; } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) <<4); return index; } 程序1可以正常记时,不会出现跳变,程序二时间会有跳变,请改错程序2,我需要在程序2上改错,保留大部分不错的内容,仅改变错误的地方
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值