[pwm]PWM的輸入捕捉模式


對於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 0 > /sys/class/pwm/pwmchip0/export
free the device:
target$ echo 0 > /sys/class/pwm/pwmchip0/unexport
  • 使能/失能通道,注意:在使能前需要先設置下面參數,如周期和占空比
Enable the PWM
target$ echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable
Disable the PWM
target$ echo 0 > /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 1 sec , enter

target$ echo 1000000000 > /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 1 > /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使用)

這樣就和驅動文件關聯起來了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM