Matt, Sounds cool project you worked on. good job... using pic16,18 name CCP i understand =A0capture, compare or =A0pwm=A0 now pic24,32 pwm they call it output compare. are you suggesting =A0to use = pwm or compare? thanks AA ________________________________ From: Matt Pobursky To: Microcontroller discussion list - Public. =20 Sent: Tuesday, December 20, 2011 11:05 AM Subject: Re: [PIC]: PIC32 pwm calculation =20 You might also considering generating your stepper motor pulse with an Output Compare function. The bulk of the work is done in hardware with a little software overhead to setup the next pulse toggle time. I just did this with a PIC18F part in a GPS based speedometer that had to generate a speed pulse that simulated a ground speed radar output. The 40 MHz PIC will generate a speed pulse from sub-Hz to 3KHz with 800 nS accuracy, no jitter (since the pulse output is toggle by the OC module). The basic algorithm is as follows: A routine is called with the desired frequency of output. Calculate the time of half the frequency period. Save this pulse toggle timer value.=20 Add this value to current timer value and set OC register to next toggle ti= me. Turn OC mode on to toggle output (or set or clear output). Enable OC interrupts. In OC interrupt service routine: Add timer offset for next toggle to OC register. Set OC register to next toggle time. Set mode to Set or Clear on Compare Match (if not using Toggle mode). My interrupt service routine is 4 lines of code... one C statement to set the new timer output compare count, one C code line to determine whether next compare is toggle HIGH or LOW and and two assembler lines -- one to set the OC mode to SET on compare and one to set the OC mode to CLEAR on compare. Very, very low overhead. With this method you can easily do 20H to 2Khz with a CCP clock of about 1MHz and 16 bit compare registers. With a little more code you can extend the algorithm to hold off output toggles to greater than 16 bits and get sub-Hz very accurate outputs. You could easily add a counter to the above algorithm to only output as many pulse as the stepper requires. In my GPS speedometer application, this speed pulse is being generated concurrently with the CPU processing 115,200 bps messages from the GPS receiver, parsing the NMEA messages and outputting NMEA messages to the outside world. Matt Pobursky Maximum Performance Systems On Tue, 20 Dec 2011 09:20:06 -0800 (PST), Andre Abelian wrote: > Hi all, > > I am driving stepper motor that requires H to L transition to do > stepping. I used pic 32 core timer and it works > fine now my co worker wants me to change it to hardware PWM instead of > using timer. well sounds ok until > when I started changing the frequency on PWM the result I got all > unknown. like frequency is too off or duty cycle > is moved after changing the frequency. the duty cycle is half the period. > basically the user is going to change the frequency > on stepper motor from 20hz to 2khz and I noticed that hardware pwm is its > limits. according to my co worker hardware pwm > is flexible and based on my test it is not going to give 20hz to 2k > output. some frequencys are not even close. > using time I am able to get accurate frequency but it is using interrupt > and they worry about it. my question is > how do you guys calculate hardware pwm to get accurate result that always > is going to output 50% duty cycle and > close frequency. I am using pic32mx=A0 80mhz > > > any idea? > > thanks > > AA --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .