如题,我用STM32CUBEMX生成的工程,做输入捕获的时候,高电平时间不变。如下图,高电平时间一直都是192us,但实际我用示波器测量的是300us和400us。
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 400);//高电平为400us
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 300);//高电平为300us
我只改动了上面一句,实际用示波器测量效果和代码可以对得上。
可是串口调试助手却一直都是显示192us不变,代码如下,求解。
int main(void)
{
/* USER CODE BEGIN 1 */
/* 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_TIM3_Init();
MX_TIM5_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_IC_Start_IT(&htim5,TIM_CHANNEL_1); //一定要开启TIM5通道1的捕获中断
__HAL_TIM_ENABLE_IT(&htim5,TIM_IT_UPDATE); //一定要开启TIM5的更新中断
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
printf("准备开始采集高电平...\r\n");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 400);
HAL_Delay(500);
if(TIM5CH1_CAPTURE_STA & 0X80)
{ //完成一次高电平捕获
temp = TIM5CH1_CAPTURE_STA & 0X3F;
temp *= 65536; //溢出总时间
temp += TIM5CH1_CAPTURE_STA; //总的高电平时间
printf("高电平时间:%lld us\r\n",temp);
TIM5CH1_CAPTURE_STA = 0; //准备下一次捕获
}
}
/* USER CODE END 3 */
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* 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_TIM3_Init();
MX_TIM5_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_IC_Start_IT(&htim5,TIM_CHANNEL_1); //一定要开启TIM5通道1的捕获中断
__HAL_TIM_ENABLE_IT(&htim5,TIM_IT_UPDATE); //一定要开启TIM5的更新中断
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
printf("准备开始采集高电平...\r\n");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 400);
HAL_Delay(500);
if(TIM5CH1_CAPTURE_STA & 0X80)
{ //完成一次高电平捕获
temp = TIM5CH1_CAPTURE_STA & 0X3F;
temp *= 65536; //溢出总时间
temp += TIM5CH1_CAPTURE_STA; //总的高电平时间
printf("高电平时间:%lld us\r\n",temp);
TIM5CH1_CAPTURE_STA = 0; //准备下一次捕获
}
}
/* USER CODE END 3 */
}
|