(OLED)SCI IIC & RTC
本帖最后由 RA_kjnsc 于 2023-1-5 15:24 编辑
RTC配置:点击Stacks->New Stack->timers->Realtime Clock(r_rtc)。
IIC配置:点击Stacks->New Stack->Connectivity->I2C Master (r_sci_i2c)。
显示汉字需要先进行取模,生成字模前先进行设置。
将取得的编码放入oledfont.h的Hzk数组中。
- #include "hal_data.h"
- #include "oled.h"
- FSP_CPP_HEADER
- void R_BSP_WARMStart(bsp_warm_start_event_t event);
- FSP_CPP_FOOTER
- uint8_t rtc_second= 56; //秒
- uint8_t rtc_minute =34; //分
- uint8_t rtc_hour =12; //时
- uint8_t rtc_day =2; //日
- uint8_t rtc_month =1; //月
- uint16_t rtc_year =123; //年
- rtc_time_t get_time;
- i2c_master_event_t i2c_event = I2C_MASTER_EVENT_ABORTED;
- void sci_i2c_master_callback(i2c_master_callback_args_t *p_args)
- {
- i2c_event = I2C_MASTER_EVENT_ABORTED;
- if (NULL != p_args)
- {
- /* capture callback event for validating the i2c transfer event*/
- i2c_event = p_args->event;
- }
- }
- fsp_err_t err = FSP_SUCCESS;
- int timeout_ms = 100;
- /* rtc_time_t is an alias for the C Standard time.h struct 'tm' */
- rtc_time_t set_time =
- {
- .tm_sec = 56, /* 秒,范围从 0 到 59 */
- .tm_min = 34, /* 分,范围从 0 到 59 */
- .tm_hour = 12, /* 小时,范围从 0 到 23*/
- .tm_mday = 2, /* 一月中的第几天,范围从 1 到 31*/
- .tm_mon = 1, /* 月份,范围从 0 到 11*/
- .tm_year = 123, /* 自 1900 起的年数,2021为121*/
- };
- volatile bool rtc_flag = 0;//RTC延时1s标志位
- void rtc_callback(rtc_callback_args_t *p_args)
- {
- /* TODO: add your own code here */
- if(p_args->event == RTC_EVENT_PERIODIC_IRQ)
- rtc_flag=1;
- }
- void hal_entry(void)
- {
- /* TODO: add your own code here */
- err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
- assert(FSP_SUCCESS == err);
- R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
- R_RTC_PeriodicIrqRateSet(&g_rtc0_ctrl, RTC_PERIODIC_IRQ_SELECT_1_SECOND);
- /* IIC初始化*/
- err = R_SCI_I2C_Open(&g_i2c0_ctrl, &g_i2c0_cfg);
- assert(FSP_SUCCESS == err);
- WriteCmd();//OLED初始化
- OLED_Clear();//清屏
- OLED_ShowCHinese(0,1,3); //野
- OLED_ShowCHinese(16,1,4); //火
- OLED_ShowString(36,1,"&",16);
- OLED_ShowCHinese(48,1,5); //电
- OLED_ShowCHinese(64,1,6); //子
- OLED_ShowCHinese(80,1,7); //发
- OLED_ShowCHinese(96,1,8); //烧
- OLED_ShowCHinese(112,1,9); //友
- OLED_ShowCHinese(40,5,0); //年
- OLED_ShowCHinese(72,5,1); //月
- OLED_ShowCHinese(104,5,2); //日
- OLED_ShowString(32,3,"12:34:56",16);
- while(1)
- {
- if(rtc_flag)
- {
- R_RTC_CalendarTimeGet(&g_rtc0_ctrl, &get_time);//获取RTC计数时间
- rtc_flag=0;
- rtc_second=get_time.tm_sec; //获取秒
- rtc_minute=get_time.tm_min; //获取分
- rtc_hour=get_time.tm_hour; //获取时
- rtc_day=get_time.tm_mday; //获取日
- rtc_month=get_time.tm_mon; //获取月
- rtc_year=get_time.tm_year; //获取年
- }
- OLED_ShowNum(8,5,rtc_year+1900,4,16); //显示年
- OLED_ShowNum(56,5,rtc_month,2,16); //显示月
- OLED_ShowNum(88,5,rtc_day,2,16); //显示日
- OLED_ShowNum(32,3,rtc_hour,2,16); //显示时
- OLED_ShowNum(56,3,rtc_minute,2,16); //显示分
- OLED_ShowNum(80,3,rtc_second,2,16); //显示秒
- }
- #if BSP_TZ_SECURE_BUILD
- /* Enter non-secure code */
- R_BSP_NonSecureEnter();
- #endif
- }
- /*******************************************************************************************************************//**
- * This function is called at various points during the startup process. This implementation uses the event that is
- * called right before main() to set up the pins.
- *
- * @param[in] event Where at in the start up process the code is currently at
- **********************************************************************************************************************/
- void R_BSP_WarmStart(bsp_warm_start_event_t event)
- {
- if (BSP_WARM_START_RESET == event)
- {
- #if BSP_FEATURE_FLASH_LP_VERSION != 0
- /* Enable reading from data flash. */
- R_FACI_LP->DFLCTL = 1U;
- /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
- * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
- #endif
- }
- if (BSP_WARM_START_POST_C == event)
- {
- /* C runtime environment and system clocks are setup. */
- /* Configure pins. */
- R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
- }
- }
- #if BSP_TZ_SECURE_BUILD
- BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();
- /* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
- BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
- {
- }
- #endif
复制代码
|