Hello,
I am using the Mod 5213 to control a voltage based servo motor. As such, I need to use pulse width modulation in order to control the position of the servo motor. I stumbled upon a PWM example for the Netburner and it has been working with limited success. The code is as follows:
Pins[24].function(PIN24_PWM0); // Enable PWM functionality for pin 24
sim.pwm.pwme = 0; // Disable all PWM channels before making any settings
sim.pwm.pwmpol &= ~0x01; // Set to have an initial low signal, then set high on duty cycle output compare
sim.pwm.pwmclk |= 0x01; // Set to use clock SA (Scale A)
sim.pwm.pwmprclk |= 0xF; // Set to use clock A prescale value of 2
sim.pwm.pwmcae &= ~0x07; // Set to operate channel 0 in left-aligned output mode
sim.pwm.pwmctl = 0; // All channels are 8-bit channels, doze and debug mode disabled
sim.pwm.pwmscla = 0x04; // Use scale divisor value of 2 to generate clock SA from clock A
sim.pwm.pwmcnt[0] = 1; // Write any value to this register to reset the counter and start off clean
sim.pwm.pwmper[0] = 100; // Set PWM channel period register to a value
sim.pwm.pwmdty[0] = 30; // Set PWM channel duty register to a value of 3
sim.pwm.pwme |= 0x01; // Enable PWM output for PWM channel 0
This code does work (I have checked on an oscilloscope) but I am having difficulty in a couple of areas.
1) I can only get this code to function for pin 24. I have tried everything that I can think of, but can't seem to adjust the code to work on another PWM enabled pin.
2) I am trying to get this code to operate in 16-bit mode (using 2 concatenated 8 bits) by altering pwm.pwmctl according to the application notes. However, I cannot get anything to work, let alone compile, as every syntax I have tried, fails.
If I could get some help with this, I would greatly appreciate it!
PWM Channels and 16 bit operation
-
- Posts: 513
- Joined: Sat Apr 26, 2008 7:14 am
Re: PWM Channels and 16 bit operation
1) Try looking at the app note:http://www.netburner.com/downloads/mod5 ... 13-PWM.pdf
2) Without looking at your code in too much detail, I'm assuming you've initialized the proper Pins[xx].function for each desired output pin, and that in addition, throughout your code you've edited the [0] offsets:
sim.pwm.pwmcnt[0] = 1; // Write any value to this register to reset the counter and start off clean
sim.pwm.pwmper[0] = 100; // Set PWM channel period register to a value
sim.pwm.pwmdty[0] = 30; // Set PWM channel duty register to a value of 3
to [1], [2], [3], etc for each channel you are setting up.
3) Also, be sure to set the proper enable bits in sim.pwm.pwme for each channel
4) You might also find downloading this utility handy to get an initial take on what the code should look like:
http://www.microapl.co.uk/CFInit/cfinit_main.html
The code it generates is for CodeWarrior, but it is usually very easy to modify to NB. It's a fast way to configure the registers for a 1st cut on an unfamiliar hardware module...
2) Without looking at your code in too much detail, I'm assuming you've initialized the proper Pins[xx].function for each desired output pin, and that in addition, throughout your code you've edited the [0] offsets:
sim.pwm.pwmcnt[0] = 1; // Write any value to this register to reset the counter and start off clean
sim.pwm.pwmper[0] = 100; // Set PWM channel period register to a value
sim.pwm.pwmdty[0] = 30; // Set PWM channel duty register to a value of 3
to [1], [2], [3], etc for each channel you are setting up.
3) Also, be sure to set the proper enable bits in sim.pwm.pwme for each channel
4) You might also find downloading this utility handy to get an initial take on what the code should look like:
http://www.microapl.co.uk/CFInit/cfinit_main.html
The code it generates is for CodeWarrior, but it is usually very easy to modify to NB. It's a fast way to configure the registers for a 1st cut on an unfamiliar hardware module...
Re: PWM Channels and 16 bit operation
Hey,
How do you set the proper enable bits in sim.pwm.pwme for a channel other than 0? Do you have to enable each channel separately?
Also, what is the code using sim.pwm.pwmctl to set up channel 0 and 1 in concatenation?
-Thanks
How do you set the proper enable bits in sim.pwm.pwme for a channel other than 0? Do you have to enable each channel separately?
Also, what is the code using sim.pwm.pwmctl to set up channel 0 and 1 in concatenation?
-Thanks
-
- Posts: 513
- Joined: Sat Apr 26, 2008 7:14 am
Re: PWM Channels and 16 bit operation
You should probably read the Freescale MCF5213 reference manual: C:\Nburn\docs\FreescaleManuals\MCF5213RM.pdf.
See section 24.2.1 for the PMME register. Section 24.3.2.7 describes the PWM 16-bit functions and the concatenation bits in the PWMCTL register. In the version I have, these sections ar on pages 24-3, and 24-19.
See section 24.2.1 for the PMME register. Section 24.3.2.7 describes the PWM 16-bit functions and the concatenation bits in the PWMCTL register. In the version I have, these sections ar on pages 24-3, and 24-19.