820 views|11 replies

6062

Posts

4

Resources
The OP
 

[ST NUCLEO-U5A5ZJ-Q development board review] 8 serial port printing and clock [Copy link]

 
This post was last edited by damiaa on 2024-3-13 23:59
[ST NUCLEO-U5A5ZJ-Q development board review] 8 serial port printing and clock
The clock is always an important peripheral in embedded devices. Many devices that interact with people need to use clocks, such as alarm clocks, watches, mobile phones, etc. Therefore, it is very necessary to be familiar with the peripheral processing of the clock.
Now let’s start the experiment in this area.
1. Open stm32cubeide and configure the clock
RCC configuration HSE and LSE external crystal
RTC selects Activate Clock Source and Activate Calendar
Then check the clock configuration (Clock Config) and select LSE
There are clocks and calendars, which can be configured here or set at runtime
If you add an alarm, some parameters of the alarm will appear below. You can configure them here or set them at runtime.
And there are alarm interruptions to choose from:
The 32.768 external clock is now connected to the RTC and the clock calendar is also configured.
2. Introduction to Generated Code
Select and save to generate the code.
In main.c, there is
Definition of Rtc structure handle
There is RTC initialization procedure
stm32u5xx_it.c has the addition of alarm interrupt
The RTC initialization program contains the clock calendar setting and alarm setting code, which can be copied and used directly.
3. Add code
Because alarm interrupt A is selected, if you want to handle this interrupt, you must add this callback function to the main program (if other interrupts are selected, you must add callback functions for other interrupts).
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
}
If alarm B or other interrupts are selected, the processing is similar.
Regarding the callback function, ST also provides a HAL_RTC_RegisterCallback to register your own callback function, but you also need to configure USE_HAL_RTC_REGISTER_CALLBACKS to 1. This is troublesome. It is better to use the callback function with the same name. In fact, other interrupts also have this operation, such as serial ports.
As for the time and calendar settings, they are already in the initialization and can be copied here.
How to read the time and calendar, we can find their prototypes in stm32u5xx_hal_rtc.c .
Finally, add code to the main program
  1. The time, date, alarm, etc. are set during initialization, so we won’t worry about that here.
  2. Do a serial port read command analysis:
    getDT +0x0d (Enter)
  3. Then read out the date, time and week
  4. Add a read command parsing program to the callback function, and make a mark if there is a read command.
  5. When the main program finds a read flag, it reads the time, date, and week and sends them to the serial port.
  6. At the same time, turn on a green light to remind you
Add parsing command to void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
if((rx1_b[0] == 'g')&&(rx1_b[1] == 'e')&&(rx1_b[2] == 't')&&(rx1_b[3] == 'D')&&(rx1_b[4] == 'T')&&(rx1_b_index >=6))
{
    setRTCflag=0x11;
}

Processing in the main program:

switch(setRTCflag)
	   {

       	   case 0x03:
             //setT 12 30 09
    	     //setD 2 2 11 2024
    	     //setA 23 32 00 00
    	     if (HAL_RTC_SetTime(&hrtc, &sTime2, RTC_FORMAT_BCD) != HAL_OK)
    	     {
    	       Error_Handler();
    	     }
    	     break;
       	  case 0x05:
    	     if (HAL_RTC_SetDate(&hrtc, &sDate2, RTC_FORMAT_BCD) != HAL_OK)
    	     {
    	       Error_Handler();
    	     }
    	     break;
    	     /** Enable the Alarm A*/
       	  case 0x09:
    	     if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm2, RTC_FORMAT_BCD) != HAL_OK)
    	     {
    	       Error_Handler();
    	     }
    	     break;
       	  case 0x11:
    	     if(HAL_RTC_GetTime(&hrtc, &sTime2, RTC_FORMAT_BIN) == HAL_OK)
    	     {
    	    	     char uart_buf[100]={0};
    	    	     HAL_RTC_GetDate(&hrtc, &sDate2, RTC_FORMAT_BIN);
    	    	     char str[25]={0};
    	    	     switch(sDate2.WeekDay)
    	    	     {
    	    	       	  case 1:
    	    	       		   sprintf(str,"WeekDay is MONDAY\n");
    	    	       		   break;
    	    	          case 2:
    	    	         	   sprintf(str,"WeekDay is TUESDAY\n");
    	    	         	   break;
    	    	          case 3:
    	    	         	   sprintf(str,"WeekDay is WEDNESDAY\n");
    	    	         	   break;
    	    	         case 4:
    	    	         	   sprintf(str,"WeekDay is THURSDAY\n");
    	    	         	   break;
    	    	         case 5:
    	    	         	   sprintf(str,"WeekDay is FRIDAY\n");
    	    	         	   break;
    	    	         case 6:
    	    	         	   sprintf(str,"WeekDay is SATURDAY\n");
    	    	         	   break;
    	    	         case 7:
    	    	         	   sprintf(str,"WeekDay is SUNDAY\n");
    	    	         	   break;

    	    	    }
    	    	    sprintf(uart_buf,"Date is 20%2d-%02d-%02d  \n    Time is %02d : %02d : %02d   \n  %s\n ",sDate2.Year,sDate2.Month,sDate2.Date,sTime2.Hours,sTime2.Minutes,sTime2.Seconds,str);
    	     		HAL_UART_Transmit_IT(&huart1,(const uint8_t*)&uart_buf[0], 100);
    	     		
    	     		HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET);
    	     		HAL_Delay(100);
    	     		HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET);
    	     }
    	     break;
       	  default:break;
	   }
       setRTCflag =0;

Compile, download, debug, and verify experimentally:
1

The time setting part is in the initialization code, so I won't introduce it here. I'll report here first. Thank you
This post is from stm32/stm8

Latest reply

Thank you for your selfless sharing. The technical content is illustrated and very detailed. I look forward to your subsequent exciting content.   Details Published on 2024-3-16 19:10
 

5998

Posts

6

Resources
2
 

Is this a development board or a self-designed one? The external 32.768 crystal oscillator clock is not much different.

This post is from stm32/stm8

Comments

Thanks for the support. Indeed, the clock of ST is still good. Just pay attention to matching the crystal oscillator and the capacitors on the side. If you grab a crystal oscillator randomly on the market, it is easy to fail to start oscillation. However, the method recommended by the ST board is basically fine.  Details Published on 2024-3-14 09:21
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 

6818

Posts

11

Resources
3
 

The moderator put a lot of thought into the design of this trial, thank you for your hard work!

This post is from stm32/stm8

Comments

Thanks for the support!  Details Published on 2024-3-14 09:19
 
 

6062

Posts

4

Resources
4
 
This post was last edited by damiaa on 2024-3-14 09:37
lugl4313820 posted on 2024-3-14 09:08 The moderator put a lot of thought into the design of this trial, thank you for your hard work!

Thanks for the support! This experiment took me three whole nights to write the article.

I also set the clock, but didn't debug it, so I didn't talk about it. I don't know if there are any errors, but the code is shown in the video.

This post is from stm32/stm8

Comments

Testing is a learning process, which takes a lot of time. Almost every article, from design to research, code verification, article writing, and video recording, takes at least a few days to complete.  Details Published on 2024-3-14 10:31
Testing is a learning process, which takes a lot of time. Almost every article, from design to research, code verification, article writing, and video recording, takes at least a few days to complete.  Details Published on 2024-3-14 09:52
 
 
 

6062

Posts

4

Resources
5
 
Qintianqintian0303 posted on 2024-3-14 08:36 Is this a development board or a self-designed one? The external 32.768 crystal oscillator clock is not much different

Thanks for the support. Indeed, ST's clock is still well made.

Just pay attention to matching the crystal oscillator and the capacitors on the side. If you just grab a crystal oscillator from the market, it is easy to fail to oscillate.

However, there is basically no problem with the method recommended by the ST board.

This post is from stm32/stm8
 
 
 

6818

Posts

11

Resources
6
 
damiaa posted on 2024-3-14 09:19 Thanks for the support! This experiment took three nights to write the article. I also set the clock, but didn't debug it, so I didn't talk about it. I don't know if there is any...

Testing is a learning process, which takes a lot of time. Almost every article, from design to research, code verification, article writing, and video recording, takes at least a few days to complete.

This post is from stm32/stm8
 
 
 

5998

Posts

6

Resources
7
 
damiaa posted on 2024-3-14 09:19 Thanks for the support! This experiment took three nights to write the article. I also set the clock, but didn't debug it, so I didn't talk about it. I don't know if there is any...

Persist, persist and persist again. What we have achieved is what we have accumulated. We hope it will be useful to others. First of all, we will definitely gain something.

This post is from stm32/stm8

Comments

That makes sense. Let's study and study. It's a good opportunity for us to study together.  Details Published on 2024-3-14 10:41
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

6062

Posts

4

Resources
8
 
Qintianqintian0303 posted on 2024-3-14 10:31 Persist, persist and persist again. What we have made is what we have accumulated. We hope it will be useful to others. First of all, we will definitely gain something.

That makes sense. Let's study and study. It's a good opportunity for us to study together.

This post is from stm32/stm8
 
 
 

7422

Posts

2

Resources
9
 

Thanks for sharing, looking forward to the follow-up!

This post is from stm32/stm8

Comments

Thanks for your support!  Details Published on 2024-3-15 16:04
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

6062

Posts

4

Resources
10
 
freebsder posted on 2024-3-15 14:52 Thank you for sharing, looking forward to the follow-up!

Thanks for the support!

This post is from stm32/stm8
 
 
 

718

Posts

4

Resources
11
 

Thank you for your selfless sharing. The technical content is illustrated and very detailed. I look forward to your subsequent exciting content.

This post is from stm32/stm8

Comments

Thank you for your support!  Details Published on 2024-3-16 22:57
 
 
 

6062

Posts

4

Resources
12
 
chejm posted on 2024-3-16 19:10 Thank you for your selfless sharing, the technical content is illustrated and very detailed, and I look forward to the subsequent exciting content from the OP

Thank you for your support!

This post is from stm32/stm8
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list