STM32CUBE使用中的小问题

目的

(1)串口使用

1: 在函数HAL_UART_MspInit中添加,开启接收中断

__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);	//开启接收中断	

2 : 为支持printf函数功能,在USART.C中添加如下部分

typedef struct __FILE FILE;

#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
 
  return ch;
}

(2)RTC时钟的使用问题

RTC使用过程中,需修改初始化部分,否则会每次开机复位。修改代码如下

//  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
//  {
//    Error_Handler();
//  }
//  sDate.WeekDay = RTC_WEEKDAY_THURSDAY;
//  sDate.Month = RTC_MONTH_AUGUST;
//  sDate.Date = 0x15;
//  sDate.Year = 0x19;

//  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
//  {
//    Error_Handler();
//  }
	//判断是否为第一次开机
	if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR0)!=0x5050)
	{
		//设置RTC日期时间
		if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
		{
			Error_Handler();
		}
		sDate.WeekDay = RTC_WEEKDAY_THURSDAY;
		sDate.Month = RTC_MONTH_AUGUST;
		sDate.Date = 0x15;
		sDate.Year = 0x19;
		//设置时钟时间数据
		if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
		{
			Error_Handler();
		}	
		//写入标志,用于第一次开机检查
		HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0X5050);
	}

此外RTC时钟读取时一定是先读取RTC Time 后读取RTC Date。

/**
  * @brief  Gets RTC current time.
  * @param  hrtc pointer to a RTC_HandleTypeDef structure that contains
  *                the configuration information for RTC.
  * @param  sTime Pointer to Time structure
  * @param  Format Specifies the format of the entered parameters.
  *          This parameter can be one of the following values:
  *            @arg RTC_FORMAT_BIN: Binary data format
  *            @arg RTC_FORMAT_BCD: BCD data format
  * @note  You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
  *        value in second fraction ratio with time unit following generic formula:
  *        Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
  *        This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
  *
  * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  *        in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  *        Reading RTC current time locks the values in calendar shadow registers until current date is read. 
  * 
  * @retval HAL status
  */

在官方函数中明确指出必须先读取 time后在读取Date,否则会出现未知错误。
@note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values in the higher-order calendar shadow registers to ensure consistency between the time and date values.
Reading RTC current time locks the values in calendar shadow registers until current date is read.

( 3 ) RTC的定时WakeUP问题

STM32内置RTC具备有两个闹钟与一个定时WakeUP功能,使用WakeUp中注意当正确使能中断后,仍未能进入中断
STM32CUBE使用中的小问题_第1张图片
在STM32cube生成的代码中可以看到,生成代码使用了HAL_RTCEx_SetWakeUpTimer函数,但在函数说明中仅仅是Sets wake up timer。该函数只是设置了WakeUp一些参数未开启中断

  /** Enable the WakeUp 
  */
	//触发时间 (2048-1)*(16/32768)=1S *60*60 =1h
  if (HAL_RTCEx_SetWakeUpTimer(&hrtc,2047, RTC_WAKEUPCLOCK_RTCCLK_DIV16) != HAL_OK)
  {
    Error_Handler();
  }

将上述函数修改为HAL_RTCEx_SetWakeUpTimer_IT即可

  /** Enable the WakeUp 
  */
	//触发时间 (2048-1)*(16/32768)=1S *60*60 =1h
  if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc,2047, RTC_WAKEUPCLOCK_RTCCLK_DIV16) != HAL_OK)
  {
    Error_Handler();
  }

( 4)CAN通讯的使用

STM32cube生成代码中无ID滤波设置,需自行添加,以及一些启动CAN的操作

void SetCAN1_Filter(void)
{
	CAN_FilterTypeDef  sFilterConfig;   
	/*##-2- Configure the CAN1 Filter ###########################################*/
	sFilterConfig.FilterBank=0;								             //指定要初始化的筛选器组。
	sFilterConfig.FilterIdHigh=0X0000;                     //32位ID
	sFilterConfig.FilterIdLow=0X0000;   
	sFilterConfig.FilterMaskIdHigh=0X0000;                 //32位MASK
	sFilterConfig.FilterMaskIdLow=0X0000;     
	sFilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0;   //过滤器0关联到FIFO0
	sFilterConfig.FilterMode=CAN_FILTERMODE_IDMASK;     
	sFilterConfig.FilterScale=CAN_FILTERSCALE_32BIT;   
	sFilterConfig.FilterActivation=ENABLE;    //激活滤波器0
  sFilterConfig.SlaveStartFilterBank = 14;   
	//初始化	CAN滤波器
	if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
	{
		/* Filter configuration Error */
		Error_Handler();
	}
	//启动CAN_IT_RX_FIFO0_MSG_PENDING中断
	if(HAL_CAN_ActivateNotification(&hcan1,CAN_IT_RX_FIFO0_MSG_PENDING)!=HAL_OK)
	{
		Error_Handler();
	}
	//启动CAN
	if(HAL_CAN_Start(&hcan1)!=HAL_OK)
	{
		Error_Handler();
	}

}

你可能感兴趣的:(学习)