阅读量:0
ADC模块用的是RP1不用多说了,主要是脉冲模块,这个模块没考过
这个脉冲模块放出脉冲,主要能用TIM捕获到这个脉冲的高电平持续时间即可
CubMx配置:
脉冲模块的引脚与PB0相连,所以用PB0读取上升沿记的数和下降沿记的数,后前相减就是高电平记的数
CubeMx配置:
internal clock 内部时钟
input capture direct mode 输入捕获测量脉宽
channel3 通道3
类似GPIO INPUT读取外部电平
中断enable
预分配设置3200,1s记录1000000 / 100个数,那么记录一个数花费0.1ms
自动重装enable
kil5:
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) // 定时器捕获中断回调函数
在触发上升沿或者下降沿的时候会进入这个中断函数,设置上升沿下降沿在polarity selection中
HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_3); //读取计数器的值
显示计数器的值
__HAL_TIM_SET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_3,TIM_ICPOLARITY_FALLING); // 设置触发条件
设置下次中断为下降沿
代码:
#include "Function.h" #include "main.h" #include "oled.h" #include "i2c.h" #include "tim.h" #include <stdio.h> uint16_t ENDNUMBER = 0; uint16_t BEGINNUMBER = 0; uint16_t TIMSTATE = 0; char ARRAY1[20]; void OLED_Write(unsigned char type, unsigned char data){ unsigned char WriteData[2]; WriteData[0] = type; WriteData[1] = data; HAL_I2C_Master_Transmit(&hi2c3, 0x78, WriteData, 2, 0xff); } void Function_OledInit(unsigned char ms){ HAL_GPIO_WritePin(OLED_Power_GPIO_Port, OLED_Power_Pin, GPIO_PIN_RESET); HAL_Delay(ms); OLED_Init(); } void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim){ if(htim == &htim3){ if(TIMSTATE == 0){ TIMSTATE ++; BEGINNUMBER = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_3); __HAL_TIM_SET_CAPTUREPOLARITY(&htim3, TIM_CHANNEL_3, TIM_ICPOLARITY_FALLING); }else if(TIMSTATE == 1){ TIMSTATE ++; __HAL_TIM_SET_CAPTUREPOLARITY(&htim3, TIM_CHANNEL_3, TIM_ICPOLARITY_RISING); }else if(TIMSTATE == 2){ TIMSTATE ++; ENDNUMBER = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_3); } } } void Function_PuseCheck(){ if(TIMSTATE == 3){ sprintf(ARRAY1, "%.1f", (float) (ENDNUMBER - BEGINNUMBER) / 10); OLED_ShowString(0, (unsigned char* )ARRAY1); TIMSTATE = 0; } HAL_Delay(500); } void Function_MyMain(){ Function_PuseCheck(); }
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_3);
主函数一定要使能要不然进不了中断函数