1、配置图
2、程序注意
想要读出正确的时间,先要使用HAL_RTC_GetTime()函数,再使用HAL_RTC_GetDate()函数,否则读出的时间不正确!!!!!
HAL_RTC_GetTime(&hrtc, &Nowtime, RTC_FORMAT_BIN);//必须先获取时间
HAL_RTC_GetDate(&hrtc, &Nowdate, RTC_FORMAT_BIN);//再获取日期,否者读不出实时时间
3、手动设置起始时间
//设置初始化时分秒
Nowtime.Hours = 10;
Nowtime.Minutes = 10;
Nowtime.Seconds = 50;
HAL_RTC_SetTime(&hrtc, &Nowtime, RTC_FORMAT_BIN);
//设置初始化年月日
Nowdate.Year = 20;
Nowdate.Month = 1;
Nowdate.Date = 6;
HAL_RTC_SetDate(&hrtc, &Nowdate, RTC_FORMAT_BIN);
4、主函数
int main(void)
{
/* USER CODE BEGIN 1 */
uint8_t ver = 0;
uint32_t adc_value;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();
MX_RTC_Init();
MX_ADC_Init();
/* USER CODE BEGIN 2 */
//设置初始化时分秒
Nowtime.Hours = 10;
Nowtime.Minutes = 10;
Nowtime.Seconds = 50;
HAL_RTC_SetTime(&hrtc, &Nowtime, RTC_FORMAT_BIN);
//设置初始化年月日
Nowdate.Year = 20;
Nowdate.Month = 1;
Nowdate.Date = 6;
HAL_RTC_SetDate(&hrtc, &Nowdate, RTC_FORMAT_BIN);
tRadioDriver *radio;
radio = RadioDriverInit();//初始化Lora驱动
radio -> Init();
radio -> StartRx();//开始发送
HAL_ADC_Start_DMA(&hadc, &adc_value, 8);//开始转换
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
SX1276Read(REG_LR_VERSION,&ver);
if(ver == 0x12)
{
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
HAL_ADC_Start_DMA(&hadc, &adc_value, 1000);
printf("ADC_VALUE:%d \r\n",adc_value);
HAL_RTC_GetTime(&hrtc, &Nowtime, RTC_FORMAT_BIN);//必须先获取时间
HAL_RTC_GetDate(&hrtc, &Nowdate, RTC_FORMAT_BIN);//再获取日期,否者读不出实时时间
printf("Now Time :%d-%d-%d-%d-%d-%d \r\n",Nowdate.Year,Nowdate.Month,Nowdate.Date,Nowtime.Hours,Nowtime.Minutes,Nowtime.Seconds);
HAL_Delay(1000);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}