Difference between revisions of "EBC Exercise 13 Pulse Width Modulation"

From eLinux.org
Jump to: navigation, search
m
(Pulse Width Modulation)
 
(2 intermediate revisions by one other user not shown)
Line 8: Line 8:
 
In this exercise you will learn how to use the Beagle's pulse width modulation (pwm) hardware using the sysfs interface.
 
In this exercise you will learn how to use the Beagle's pulse width modulation (pwm) hardware using the sysfs interface.
  
== PWM on the Bone ==
+
== Pulse Width Modulation ==
  
 
(Note: The pwm interface seems to changing. Some of this may not apply in the future.)
 
(Note: The pwm interface seems to changing. Some of this may not apply in the future.)
  
The Bone has a PWM interface at <code>/sys/class/pwm/</code>. You can see what's there by:
+
We have to run a couple of commands to make the PWM interface appear.  Try
 +
bone$ '''SLOTS=/sys/devices/bone_capemgr.*/slots'''
 +
bone$ '''echo am33xx_pwm > $SLOTS'''
 +
bone$ '''echo bone_pwm_P9_21 > $SLOTS'''
 +
bone$ '''cd /sys/devices/ocp.2/pwm_test_P9_21.14'''
 +
bone$ '''ls'''
 +
driver  duty  modalias  period  polarity  power  run  subsystem  uevent
  
beagle$ '''cd /sys/class/pwm'''
+
The units are in ns.
beagle$ '''ls -F'''
+
Try a 1Hz frequency with a 25% duty cycle
export  unexport
+
  bone$ '''echo 1000000000 > period'''
Hmmm, there isn't much there.  We have to run a command to make something appear.  Try
+
  bone$ '''echo  250000000 > duty'''
  beagle$ '''SLOTS=/sys/devices/bone_capemgr.*/slots'''
+
  bone$ '''echo 1 > run'''
  beagle$ '''PINS=/sys/kernel/debug/pinctrl/44e10800.pinmux/pins'''
+
 
  beagle$ '''echo am33xx_pwm > $SLOTS'''
+
Connect the LED from and watch it flash. Try changing the frequency and duty cycle. You may have to set the duty cycle to 0 to change the frequencyCan you guess why?
beagle$ '''ls -F'''
+
 
  export pwmchip0@  pwmchip2@  pwmchip3@  pwmchip5@  pwmchip7@  unexport
+
=== Challenge ===
Now we need to run another command to say which pwm pin we want to useI'm using P9_21.
+
Combine the analog in and the PWM by having the pot control the frequency or the duty cycle of the LED.
beagle$ '''echo bone_pwm_P9_21 > $SLOTS
+
 
Now you can export a pwm much list you export a gpio port
+
If you have an oscilloscope try probing pin P9_21. I'm getting a nice clean 1Hz signal, with no variation.  Let's try a higher frequency, like 10 MHz.
beagle$ '''echo 1 > export'''
+
beagle$ '''cd pwm1'''
+
beagle$ '''ls -F'''
+
device@  duty_ns  period_ns  polarity  power/  run  subsystem@  uevent
+
Try a 1kHz frequency with a 25% duty cycle
+
beagle$ '''echo 1000000 > period_ns'''
+
beagle$ '''echo  250000 > duty_ns'''
+
beagle$ '''echo 1 > run'''
+
If you have an oscilloscope try probing pin P9_21. I'm getting a nice clean 1kHz signal, with no variation.  Let's try a higher frequency, like 10 MHz.
+
 
  beagle$ '''echo  50 > duty_ns'''
 
  beagle$ '''echo  50 > duty_ns'''
 
  beagle$ '''echo 100 > period_ns'''
 
  beagle$ '''echo 100 > period_ns'''
 
I'm getting a 9 MHz signal that has lots of ringing. The timer in the bone must be off a bit.
 
I'm getting a 9 MHz signal that has lots of ringing. The timer in the bone must be off a bit.
 
  
 
The [http://processors.wiki.ti.com/index.php/AM335x_PWM_Driver's_Guide AM335x PWM Driver's Guide] details what eCAP and eHRPWM are and gives some examples.   
 
The [http://processors.wiki.ti.com/index.php/AM335x_PWM_Driver's_Guide AM335x PWM Driver's Guide] details what eCAP and eHRPWM are and gives some examples.   
 
Connect the LED from and watch it flash.  Try changing the frequency and duty cycle.  You may have to set the duty cycle to 0 to change the frequency.  Can you guess why?
 
 
Stick a scope on the pin and see if the frequency and duty cycle are right.  What's the highest frequency you can get? What's the lowest?
 
  
 
{{YoderFoot}}
 
{{YoderFoot}}

Latest revision as of 22:59, 19 September 2013

thumb‎ Embedded Linux Class by Mark A. Yoder


3.8 Kernel

This is for the 3.8 kernel. EBC Exercise 13 Pulse Width Modulation 3.2 is for the 3.2 kernel.

In a previous exercise (EBC Exercise 11 gpio Polling and Interrupts) you saw how to use the gpio to produce a square wave out using a C program and sysfs. I was able to get a 1.5kHz square wave out; however we can do much better using some built in hardware on the Beagle.

In this exercise you will learn how to use the Beagle's pulse width modulation (pwm) hardware using the sysfs interface.

Pulse Width Modulation

(Note: The pwm interface seems to changing. Some of this may not apply in the future.)

We have to run a couple of commands to make the PWM interface appear. Try

bone$ SLOTS=/sys/devices/bone_capemgr.*/slots
bone$ echo am33xx_pwm > $SLOTS
bone$ echo bone_pwm_P9_21 > $SLOTS
bone$ cd /sys/devices/ocp.2/pwm_test_P9_21.14
bone$ ls
driver  duty  modalias  period  polarity  power  run  subsystem  uevent

The units are in ns. Try a 1Hz frequency with a 25% duty cycle

bone$ echo 1000000000 > period
bone$ echo  250000000 > duty
bone$ echo 1 > run

Connect the LED from and watch it flash. Try changing the frequency and duty cycle. You may have to set the duty cycle to 0 to change the frequency. Can you guess why?

Challenge

Combine the analog in and the PWM by having the pot control the frequency or the duty cycle of the LED.

If you have an oscilloscope try probing pin P9_21. I'm getting a nice clean 1Hz signal, with no variation. Let's try a higher frequency, like 10 MHz.

beagle$ echo  50 > duty_ns
beagle$ echo 100 > period_ns

I'm getting a 9 MHz signal that has lots of ringing. The timer in the bone must be off a bit.

The AM335x PWM Driver's Guide details what eCAP and eHRPWM are and gives some examples.




thumb‎ Embedded Linux Class by Mark A. Yoder