N0.5 PWM波形输出测试
时间:10-02
整理:3721RD
点击:
经过前一段时间的学习现在对HAL库的感觉可是说是更好了,这个库用起来的确是很方便,很多功能在库里已经写好了。
直接调用就可以了,但是必须说一点你的英语水平一定要好点,不然 呵呵~(我一直都用翻译工具看的)
好了话不多说,今天抓经时间测试了一下PWM波形的输出功能:
官网例子代码,
使用TIM3输出4路PWM波形,
硬件接口:PB0 - CH3 PB1 - CH4 PB4 - CH1 PB5 - CH2
占空比:
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR + 1)* 100 = 50%
TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR + 1)* 100 = 37.5%
TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR + 1)* 100 = 25%
TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR + 1)* 100 = 12.5%
下面面是代码简单介绍:
定时器初始化;
TimHandle.Instance = TIMx;
TimHandle.Init.Prescaler = uhPrescalerValue;
TimHandle.Init.Period = PERIOD_VALUE;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the PWM channels #########################################*/
/* Common configuration for all channels */
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
设置4个通道的占空比值:
sConfig.Pulse = 300; (此处测试时修改了值)
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 2 */
sConfig.Pulse = PULSE2_VALUE;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 3 */
sConfig.Pulse = PULSE3_VALUE;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 4 */
sConfig.Pulse = PULSE4_VALUE;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
启动4路PWM输出波形:
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 2 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 3 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* PWM generation Error */
Error_Handler();
}
/* Start channel 4 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK)
{
/* PWM generation Error */
Error_Handler();
}
下面看一下硬件连接:
下面是用示波器测出的PWM波形:
通道 1 .4
通道 2 3
我的示波器是双通道所以要分两次测量。
以上请各位高手大神多多指教。
直接调用就可以了,但是必须说一点你的英语水平一定要好点,不然 呵呵~(我一直都用翻译工具看的)
好了话不多说,今天抓经时间测试了一下PWM波形的输出功能:
官网例子代码,
使用TIM3输出4路PWM波形,
硬件接口:PB0 - CH3 PB1 - CH4 PB4 - CH1 PB5 - CH2
占空比:
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR + 1)* 100 = 50%
TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR + 1)* 100 = 37.5%
TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR + 1)* 100 = 25%
TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR + 1)* 100 = 12.5%
下面面是代码简单介绍:
定时器初始化;
TimHandle.Instance = TIMx;
TimHandle.Init.Prescaler = uhPrescalerValue;
TimHandle.Init.Period = PERIOD_VALUE;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the PWM channels #########################################*/
/* Common configuration for all channels */
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
设置4个通道的占空比值:
sConfig.Pulse = 300; (此处测试时修改了值)
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 2 */
sConfig.Pulse = PULSE2_VALUE;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 3 */
sConfig.Pulse = PULSE3_VALUE;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Set the pulse value for channel 4 */
sConfig.Pulse = PULSE4_VALUE;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
启动4路PWM输出波形:
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 2 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
/* Start channel 3 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* PWM generation Error */
Error_Handler();
}
/* Start channel 4 */
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK)
{
/* PWM generation Error */
Error_Handler();
}
下面看一下硬件连接:
下面是用示波器测出的PWM波形:
通道 1 .4
通道 2 3
我的示波器是双通道所以要分两次测量。
以上请各位高手大神多多指教。
嘿嘿,居然是虚拟示波器。看上去好高级的样子啊
呵呵~是的!虚拟示波器使用方便,便宜~