You are on page 1of 12

5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13

https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 1/12
SparkFun Electronics
Sharing Ingenuity
Skip to content
Search Search
Advanced search
Board index PIC Microchip PIC Microcontrollers - Software and Hardware
Change font size
Print view
FAQ
Register
Login
PWM using dsPIC30f3012/13
Moderator: phalanx
Post a reply
Search this topic Search
14 posts Page 1 of 1
PWM using dsPIC30f3012/13
by sterling2 Thu Mar 25, 2010 2:54 pm
Hi,
I am trying to control two motors using PWM with the dspic30f3012 (& will change to the dspic30f3013 soon -
ordered them). I got the actual PWM working for one motor, but i was curious about something and i couldn't
find the answer in the datasheet:
Is it possible to control both motors with the same timer? The micro has two OC channels/pins, and I am using
timer2 with OC1 right now, and was wondering if the duty cycle and clock were the same, is it possible to
control OC2 with timer2 as well, or is that not done? I have timer3 which i can use for OC2, so that's not a
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 2/12
problem, i was just curious if using the same timer (timer2 in this case) for 2 OC pins was possible.
sterling2

Posts: 19
Joined: Wed Mar 17, 2010 7:44 pm
Top
Re: PWM using dsPIC30f3012/13
by waltr Thu Mar 25, 2010 6:15 pm
I haven't used the dsPIC's but have done two PMW outputs for motor control with PIC16F873. That chip has
two CCP outputs that use timer 2 for the period of both PWMs but has independent registers for the duty cycle
(CCPR1 & CCPR2).
The data sheet wasn't clear that these were independent and the dsPIC data sheet and PWM modules may be
similar.
Hope this is helpful.
waltr

Posts: 2656
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA
Top
Re: PWM using dsPIC30f3012/13
by sterling2 Fri Mar 26, 2010 11:26 am
Hi waltr,
Thanks for replying - i was just curious. I'll go ahead and use 2 timers - i think the duty cycle will need to be
different for the two motors at some point, so i might as well. On another note, I'll start work on the XBee part
of my project again very soon & will probably have some more questions to ask about that. I appreciate all your
help so far.
Just another quick question - I don't suppose you can use the prinft command in the PICs or dsPICs to print a
register content to the computer screen unless you have a serial cable hooked to the computer and PIC or
something? I wrote some printf code to test the UART (TX connected to Rx) to see if i would get back the same
thing i sent but then realized that printf would probably not work without the serial cable. If that is so, I was
wondering under what conditions the printf command works, and if there was any other way to view/find out the
contents of registers in the microcontroller? Basically, if i don't do prinft with a serial cable (or something with a
serial cable), is there no other way to view the micro register contents? the answer is probably no, but being a
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 3/12
newbie, i wanted to be sure. (I guess i could use the XBee.)
sterling2

Posts: 19
Joined: Wed Mar 17, 2010 7:44 pm
Top
Re: PWM using dsPIC30f3012/13
by waltr Fri Mar 26, 2010 11:56 am
I'll go ahead and use 2 timers
Do you mean TIMER2 instead?
Both PWM outputs should use the same timer (TIMER2) to set the PWM period. Then there are two
independent registers to set the duty cycle for each PWM output. This is how the speed of two motors can be
independently controlled.
Well, you do need some sort of serial communication cable to send data from the PIC to a PC. I use good old
RS232 either into a PC Comm port or through an RS232 to USB adapter. On the PIC you will need an RS232
level translator like the MAX232 or MAX3232 to convert the PIC's logic level serial to RS232 levels and back.
By the C standard the printf function sends to the console which I think can be directed to the UART but I've
never used the printf function in a PIC. I just write my own SendtoUART function to send data back to the PC. I
also convert any 8-bit binary register value into two ASCII characters for easy display in a terminal program on
the PC. This is a very handy method to get live values from the PIC like the motor speed sensor and PWM
motor values.
Are using MPLAB and the Microchip dsPIC compiler? What programmer/debugger are you using?
The PICKit2's utility program can use the PICKit2 as a serial interface to the PC. Handy as you don't need to
wire a 232 chip.
waltr

Posts: 2656
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA
Top
Re: PWM using dsPIC30f3012/13
by phalanx Fri Mar 26, 2010 11:56 am
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 4/12
The dsPIC30F3012 has two compare modules that can run off of either timer 2 or timer 3. You can run both
channels off of a single timer but you will be forced to have the same pwm period (frequency) on both channels
since the period register is part of the timer and not the compare module. There are separate duty cycle registers
for each channel so you would still be able to have independent control of both motors.
The printf statement in C30 by default wants to transmit over UART1 which will require you to have a serial
cable and a computer with some flavor of hyperterminal. You can modify the libraries to make printf output to
other peripherals if you choose.
A better way to see that status of registers inside the PIC is to use a debugger. The PICKIT-3 and the ICD-3
are both capable of doing so and integrate seamlessly with MPLAB. The PICKIT-3 is a fair amount cheaper but
isn't as powerful as the ICD-3. They both will let you set breakpoints in your application as well as view and
update any registers (including program memory) inside the PIC. I would normally recommend an ICD-2 but
Microchip will stop supporting it after September of this year.
-Bill
phalanx

Posts: 1547
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH
Top
Re: PWM using dsPIC30f3012/13
by rrpilot Fri Mar 26, 2010 3:21 pm
EDIT: Sorry I missed the last post, I didn't realize the C30 mapped printf by default to UART1.
I did some work recently with the dsPIC and printf and I think it may be very useful to you. Using printf on the
PIC18 series seems to be documented a lot cleaner but here is what I found for the dsPIC (which I found to
work).
First include the stdio.h from the Microchip dspic c30 libraries
Code: Select all
#include <stdio.h>
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 5/12
Then you need to implement the write() function
Code: Select all
int write(int handle, void *buffer, unsigned int len) {
int i;
volatile UxMODEBITS *umode = &U2MODEbits;
volatile UxSTABITS *ustatus = &U2STAbits;
volatile unsigned int *txreg = &U2TXREG;
volatile unsigned int *brg = &U2BRG;
switch (handle)
{
case 0:
case 1:
case 2:
/*if ((__C30_UART != 1) && (&U2BRG)) {
umode = &U2MODEbits;
ustatus = &U2STAbits;
txreg = &U2TXREG;
brg = &U2BRG;
}*/
if ((umode->UARTEN) == 0)
{
*brg = 0;
umode->UARTEN = 1;
}
if ((ustatus->UTXEN) == 0)
{
ustatus->UTXEN = 1;
}
for (i = len; i; --i)
{
while ((ustatus->TRMT) ==0);
*txreg = *(char*)buffer++;
}
break;
default:
break;
}

return len;
}
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 6/12
That will map printf("blah blah blah") to UART2 on the dsPIC. You can just as easily change it to the UART1 by
changing "U2" to "U1" found in the code. My UART1 for some reason wasn't responding.
I will leave UART initialization up to you.
rrpilot

Posts: 270
Joined: Thu Dec 24, 2009 7:45 pm
Top
Re: PWM using dsPIC30f3012/13
by sterling2 Sat Mar 27, 2010 3:45 pm
waltr, phalanx, and rrpilot - thanks for your advice. I know a few more things about printf and PWM now
because of it.
Phalanx - you mentioned using the debugger to get register values off the dspic. I am using a ccs c compiler PCD
version 4.106 (it says PCDHW as a heading on the program window); i think the programmer is from melabs.
When i go to the debug window of the compiler, under the "enable debugger" tab,it says ICD USB, ICD Serial,
MPLAB-ICD2, MPLAB-ICD3, MPLAB-real ice as choices. Can i use one of these to look at the register
values somehow for the dspic? And to do that, would i still need to connect the UART of the dspic to the serial
port or usb port? right now, I have a usb cable connected to the programmer module which then connects to the
dspic which is on a breadboard. the uart of the dspic is not connected to anything yet, but will be connected to
an xbee once i get the logic level converter i ordered.
If you could give me some advice as to how to setup the debugger so that i can look at the register values of the
dspic (with or without a serial cable?) I would be very grateful.
sterling2

Posts: 19
Joined: Wed Mar 17, 2010 7:44 pm
Top
Re: PWM using dsPIC30f3012/13
by sterling2 Sun Mar 28, 2010 10:51 am
Hi - just wanted to verify something about PWM :
The no. of counts at which the PWM timer overflows and resets is the same as the no. of counts for one PWM
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 7/12
period, isn't it? Meaning if the period register has a value of 128 for example for every PWM period, then the
timer will reset itself after every 128 counts? I want an interrupt on every PWM period and wanted to verify that
the timer overflow interrupt would do the job?
sterling2

Posts: 19
Joined: Wed Mar 17, 2010 7:44 pm
Top
Re: PWM using dsPIC30f3012/13
by waltr Sun Mar 28, 2010 11:17 am
Yes, the timer overflow will create an interrupt if the post-scaler is set to 1.
waltr

Posts: 2656
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA
Top
Re: PWM using dsPIC30f3012/13
by sterling2 Sun Mar 28, 2010 1:45 pm
Hi again - thanks waltr for clearing that up.
I wrote up the code for PWM with interrupt on every PWM period and went to test it in the school lab where
the MCU is setup - only the lab seems locked - so decided i would ask a question here about the code setup
order and when exactly the ISR executes because I am a bit confused about that (the code compiles with no
errors):
#if defined(__PCD__) //choose the compiler
#include <30F3012.h> //select the device MCU
#fuses NOWDT, NOPROTECT, NOBROWNOUT //set configuration bits, clock bits set automatically
#use delay(internal=512KHZ) //choose internal clock
#INT_TIMER2 //Specify this function is for timer2 interrupt
void timer2_isr() {
output_high(PIN_B6); //Turn on pin connected to LED
delay_ms(1000); //Keep on for 1s
output_low(PIN_B6); //Turn off pin
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 8/12
clear_interrupt(INT_TIMER2); } //Clear interrupt flag for timer2 & return
void main() {
setup_compare(1, COMPARE_PWM | COMPARE_TIMER2); //pin OC1,no fault,timebase
// PWM period = (1/clock)*4*(TMR DIV)*(PR+1)// PWM period below = 2 s so i can see LED blink
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8, 32000); //No. @ end=PR (period reg) val or timer
overflow
set_pwm_duty(1,16000); // Value (16000) =duty% * (PR+1); here = set duty on pin OC1, 50% duty
enable_interrupts(INT_TIMER2); //Enable timer2 overflow/reset interrupt
enable_interrupts(INTR_GLOBAL); //Enable interrupts of all priority levels
while(1) {
}
}
The OC1 (PWM pin) is connected to an LED; the idea here is to make the LED blink once with PWM, then
after the period ends, go to the ISR, make the other LED blink once (so that i know i can do the interrupt
correctly) then come back to main, and get stuck in the while loop for no repetition of the program. I am just
wondering if my code to setup the interrupt and the timer is in the correct order and when exactly in the code do
i go to the ISR to run that? I have everything happening just once to keep it simple.
I will eventually put the PWM and the interrupt request in a while loop to make it continuous, so that after every
PWM period i can get an interrupt and run an ISR, which is one reason why i wanted to know when the
Interrupt request happens so i know what part of the code should go into the while loop.
sterling2

Posts: 19
Joined: Wed Mar 17, 2010 7:44 pm
Top
Re: PWM using dsPIC30f3012/13
by waltr Sun Mar 28, 2010 1:55 pm
Well first I don't see an ISR handler in the code you posted. Look in the compiler manual for the syntax of
declaring an ISR.
Also read the data sheet for the PIC you are using for how the interrupts work.
Then run the code in the MPLAB simulator with a break point in the ISR. I test and debug 96% of my PIC code
with the MPLAB simulator checking the timing of ISR's and code. This is much easier to figure out what is going
on in the simulator than when an LED doesn't blink.
waltr
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 9/12

Posts: 2656
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA
Top
Re: PWM using dsPIC30f3012/13
by phalanx Sun Mar 28, 2010 5:57 pm
Here is what a timer2 ISR looks like in C30:
Code: Select all
void __attribute__((__interrupt__)) _T2Interrupt(void) //Timer2 interrupt
handler
{
ISR CODE GOES HERE
}
On a 3-axis stepper controller, I used Timer 2 to control the speed of my 2nd axis. The motor steps were only
generated in the ISR so I also scanned the limit switches inside the ISR so I would know immediately if it had
moved too far. Absolute position feedback of the stage was used and processed in the main loop of the
program. The ISR looked like this:
Code: Select all
void __attribute__((__interrupt__)) _T2Interrupt(void) //Timer2 interrupt
handler
{
if(direction2 == cw && limithi2 == 0) //Terminate move if
limit switch is hit
halt();
if(direction2 == ccw && limitlo2 == 0) //Terminate move if
limit switch is hit
halt();
if(go2 == 1 && loop2 != 0) //Generate a step if loop
counter is greater than zero and go = 1
{
step2 = !step2;
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 10/12
Delay(1);
step2 = !step2;
loop2--;
}
if(loop2 == 0) //If loop counter is zero,
uncheck the wait flag and set go to zero
{
wait2 = 0;
go2 = 0;
}
IFS0bits.T2IF = 0;
}
-Bill
phalanx

Posts: 1547
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH
Top
Re: PWM using dsPIC30f3012/13
by sterling2 Fri Apr 02, 2010 12:25 pm
Waltr and phalanx - i appreciate all your replies so far.
I got the interrupts to work for the dsPIC; i am actually using the ccs c compiler PCD version 4.106 (it says
PCDHW as a heading on the program window) and not MPLAb. it seems that the "#int_timer2" heading
specifies the isr function. I was able to do PWM with timer2 and timer3 on both OC channels and light up the
LED using the interrupt routine after every PWM period as i had wanted to. I still haven't tried changing the duty
cycle through the isr yet, but hopefully will be able to do that.
When i go to the debug window of the ccs compiler, under the "enable debugger" tab,it says ICD USB, ICD
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 11/12
Serial, MPLAB-ICD2, MPLAB-ICD3, MPLAB-real ice as choices. Is there a particular one i should use that
you recommend from these choices? And do you know if i can use one of these debuggers to look at the register
values somehow for the dspic?
thanks for all the help.
sterling2

Posts: 19
Joined: Wed Mar 17, 2010 7:44 pm
Top
Re: PWM using dsPIC30f3012/13
by waltr Fri Apr 02, 2010 12:36 pm
I believe that that differences in those Debuggers is speed verse cost. Check them out on the Microchip web
site, the manuals should be available to download.
All of them should be able to return register values to the debug screen.
waltr

Posts: 2656
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA
Top
Display posts from previous: All posts Sort by Post time Ascending Go
Display posts by user: All users Go
Post a reply
14 posts Page 1 of 1
Return to PIC Microcontrollers - Software and Hardware
Jump to: PIC Microcontrollers - Software and Hardware Go
Who is online
Users browsing this forum: No registered users and 2 guests
5/8/2014 SparkFun Electronics View topic - PWM using dsPIC30f3012/13
https://forum.sparkfun.com/viewtopic.php?f=4&t=20893 12/12
Board index
The team Delete all board cookies All times are UTC - 7 hours
Powered by phpBB Forum Software phpBB Group

You might also like