本部分完整的代码如下,由于使用FreeRTOS,此处仅展示产生随机数这个进程的代码:
#include <stdio.h>
#include <stdlib.h>
#include "rtc.h"
#include "tim.h"
/* USER CODE BEGIN Header_Algorithm_Function */
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Function implementing the Algorithm_Task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Algorithm_Function */
void Algorithm_Function(void *argument)
{
/* USER CODE BEGIN Algorithm_Function */
RTC_DateTypeDef date_info;
RTC_TimeTypeDef time_info;
printf("勇敢牛牛,不怕困难\r\n");
/* Infinite loop */
for(;;)
{
HAL_RTC_GetTime(&hrtc, &time_info, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &date_info, RTC_FORMAT_BIN);
printf("当前时间:%2d:%2d:%2d", time_info.Hours, time_info.Minutes, time_info.Seconds);
HAL_TIM_Base_Start(&htim3);
srand(time_info.Hours+time_info.Minutes+time_info.Seconds+__HAL_TIM_GET_COUNTER(&htim3));//随机数种子设置
#define RANDOM_MAX 65535 //随机数最大值
#define RANDOM_MIN 0 //随机数最小值
uint32_t random_value;
random_value = rand() % (RANDOM_MAX + 1 - RANDOM_MIN) + RANDOM_MIN;//随机数生成
printf(" 当前随机数为:%d\r\n", random_value);
osDelay(77);
}
/* USER CODE END Algorithm_Function */
}
|