[pwm]PWM的输入捕捉模式

2023-02-18,,,

对于stm32来说,输入捕捉模式两种

普通输入捕捉模式:经常用来测量脉冲宽度和频率,例如测量脉冲宽度,TIM5_CH1来捕获高电平脉宽,首先先设置输入捕获为上升沿触发,然后记录下发生上升沿时TIM5_CNT值。再然后,设置捕获信号为下降沿,在下降沿到来的时候,记录下此时的TIM5_CNT值。这样一来,两次TIM5_CNT值只差即为脉冲宽度。只设置上升沿触发则可以捕获信号周期。
PWM输入捕捉模式:pwm输入捕获模式是普通输入模式一种特殊应用,是将TIMx输入映射了两个ICx信号(输入捕获装置IC1和IC2),其中一个捕获上升沿,另一个捕获下降沿。这样可以在中断中去读上升沿和下降沿对应寄存器中的计数,从而得出周期和占空比。其中一个捕获通道计算两次都是上升沿的时间,即周期T;而另一个通道则计算一次下降沿和之前上升沿之差,这样得到高电平时长,从而可以求得周期T和占空比。

stm32输入捕获模式简介:http://www.cnblogs.com/wangh0802PositiveANDupward/archive/2013/01/03/2843058.html

stm32 pwm输入捕获模式简介:http://www.51hei.com/bbs/dpj-41774-1.html

-----------------------------------------------------------------------------------------------------------------------------------------

PWM常用来做电机控制、LED背光亮度调节、开关电源等。

Linux pwm driver with  sysfs

TI linux pwm user guide: http://processors.wiki.ti.com/index.php/Linux_Core_PWM_User%27s_Guide#eHRPWM

Freescale: https://support.bluetechnix.at/wiki/Linux_Software_User_Manual_(i.MX6)#PWM

Gateworks: http://trac.gateworks.com/wiki/linux/pwm

对于TI的pwm来说

首先配置内核支持pwm模块,其中eHRPWM:Enhanced High Resolution PWM, eCAP:Enhanced Capture.

Procedure to build eHRPWM driver
Device Drivers --->
<*> Pulse Width Modulation(PWM) Support --->
<*> eHRPWM PWM support
Procedure to build eCAP driver
Device Drivers --->
<*> Pulse Width Modulation(PWM) Support --->
<*> eCAP PWM support

其次,申请channel

Request the Device:
target$ echo > /sys/class/pwm/pwmchip0/export
free the device:
target$ echo > /sys/class/pwm/pwmchip0/unexport

使能/失能通道,注意:在使能前需要先设置下面参数,如周期和占空比

Enable the PWM
target$ echo > /sys/class/pwm/pwmchip0/pwm0/enable
Disable the PWM
target$ echo > /sys/class/pwm/pwmchip0/pwm0/enable

设置周期/占空比/极性

i.Setting the Period
Following attributes set the period of the PWM waveform.
period Attribute
Enter the period in nano seconds value. Example
if the period is sec , enter target$ echo > /sys /class/pwm/pwmchip0/pwm0/period
ii.Setting the Duty
Following attributes set the duty of the PWM waveform. duty_cycle Attribute
Enter the Duty cycle value in nanoseconds. target$ echo val > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
iii.Setting the Polarity
Polarity Attribute.
Setup Signal Polarity Example
To set the polarity to Active High, Enter target$ echo > /sys /class/pwm/pwmchip0/pwm0/polarity

这样就完成了pwm的设置。

那sysfs和kernel中的驱动文件是怎么匹配起来的?

主要有3个文件:在drivers/pwm下得sysfs.c/core.c/pwm-imx.c

举个period的例子

-->static DEVICE_ATTR(period, 0644, pwm_period_show, pwm_period_store)   //sysfs.c

-->pwm_period_store(...)  //sysfs.c

  -->pwm_config(...)    //core.c

  -->pwm->chip->ops->config(...)  //core.c

    -->imx_pwm_config(...)    //pwm-imx.c

      -->imx_pwm_config_v1(imx1) or imx_pwm_config_v2(imx27在dts中Imx6使用)

这样就和驱动文件关联起来了。

[pwm]PWM的输入捕捉模式的相关教程结束。

《[pwm]PWM的输入捕捉模式.doc》

下载本文的Word格式文档,以方便收藏与打印。