12F683 2 x PWM Outputs


Closed Thread
Results 1 to 30 of 30

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    DT's software PWM should work:

    http://darreltaylor.com/DT_INTS-14/SPWM.html
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  2. #2
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    Since the 2-KHz PWM period (500-usecs) and duty cycle interval (50-usecs) is an exact multiple of the 20-KHz PWM period (50-usecs), I don't see why you couldn't use the PWM Timer 2 interrupt to update both PWM streams. That is unless interrupt code overhead becomes a significant portion of the 100 instruction cycles available between interrupts (using an 8-MHz clock).

    Please forgive me for the pseudo C example (I don't have PBP).

    Regards, Mike

    Code:
    ;******************************************************************
    ;  // setup PWM for a 50-usec period (8-MHz INTOSC, Timer 2
    ;  // prescale 1:1, and PR2 = 99)
    ;
    ;  char duty1 = 50;             // 20-KHz duty cycle, 25 or 50
    ;  char duty2 = 1;              // 2-KHz duty cycle, 1..9 (10%..90%)
    ;  char pwm2 = 9;               // preset 2-KHz duty cycle counter
    ;
    ;  void interrupt()             // 50-usec interrupt intervals
    ;  { pir1.TMR2IF = 0;           // clear timer 2 interrupt flag
    ;    ccpr1l = duty1;            // update PWM1 (20-KHz) duty cycle
    ;    pwm2++;                    // increment PWM2 period counter
    ;    if(pwm2 == 10)             // if end of 500-usec period
    ;    { pwm2 = 0; gpio.1 = 1;    // reset counter, turn output "on"
    ;    }                          //
    ;    if(duty2 == pwm2)          // if PWM2 duty cycle match
    ;      gpio.1 = 0;              // turn PWM2 output "off"
    ;  }                            //
    ;

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Thanks for those good ideas. I'll post some code when I have it on breadboard and tested with scope.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Just resurecting this thread does Mikes idea below look feasible in PBP running at 8mhz on a 12F683? Can I use DT interrupts on timer 2 IT flag if it is being used by the HPWM module to maintain the 20khz pwm ?

    Any other brilliant ideas or code examples?


    Quote Originally Posted by Mike, K8LH View Post
    Since the 2-KHz PWM period (500-usecs) and duty cycle interval (50-usecs) is an exact multiple of the 20-KHz PWM period (50-usecs), I don't see why you couldn't use the PWM Timer 2 interrupt to update both PWM streams. That is unless interrupt code overhead becomes a significant portion of the 100 instruction cycles available between interrupts (using an 8-MHz clock).

    Please forgive me for the pseudo C example (I don't have PBP).

    Regards, Mike

    Code:
    ;******************************************************************
    ;  // setup PWM for a 50-usec period (8-MHz INTOSC, Timer 2
    ;  // prescale 1:1, and PR2 = 99)
    ;
    ;  char duty1 = 50;             // 20-KHz duty cycle, 25 or 50
    ;  char duty2 = 1;              // 2-KHz duty cycle, 1..9 (10%..90%)
    ;  char pwm2 = 9;               // preset 2-KHz duty cycle counter
    ;
    ;  void interrupt()             // 50-usec interrupt intervals
    ;  { pir1.TMR2IF = 0;           // clear timer 2 interrupt flag
    ;    ccpr1l = duty1;            // update PWM1 (20-KHz) duty cycle
    ;    pwm2++;                    // increment PWM2 period counter
    ;    if(pwm2 == 10)             // if end of 500-usec period
    ;    { pwm2 = 0; gpio.1 = 1;    // reset counter, turn output "on"
    ;    }                          //
    ;    if(duty2 == pwm2)          // if PWM2 duty cycle match
    ;      gpio.1 = 0;              // turn PWM2 output "off"
    ;  }                            //
    ;

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    If you only need 9 steps in your 2Khz PWM (10% each step), then Mikes idea will work just fine.

    If you want 80 steps between 10-90%, I think you can use SSPWM ...
    http://www.pbpgroup.com/modules/wfse...hp?articleid=6

    The article lists lower maximum frequencies because it can't get 1% resolution so close to the start and end points. But if you only need 10-90%, it should be OK.

    If you plan on using it, I will test it to make sure.
    Since this thread was from last February, I have doubts.
    DT

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Thanks for the reply. I do plan on doing something with this in due course. My chip will be limited to 8mhz so
    can it reach 2khz with the limited clock speed. 80 points resolution would be fine.

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by retepsnikrep View Post
    Thanks for the reply. I do plan on doing something with this in due course.
    Great confidence I have now ... NOT!

    Oh well, more things to do tomorrow I have ....
    Tonight, I do this ...
    Next year, you may try it.

    Name:  Retep_2PWM.JPG
Views: 12924
Size:  135.1 KB

    Code:
    DEFINE OSC 8
    
    SPWMpin  var GPIO.5     ' Output Pin for SSPWM
    INCLUDE "SSPWM.inc"     ' include the SSPWM module
    
    ;----[Constants]------------------------------------------
    HPWMfreq        CON 20000
    Freq = 2000             ' Set Frequency of SSPWM (word)
    
    ;----[Variables]------------------------------------------
    HPWMduty        VAR BYTE
    LastHPWMduty    VAR BYTE
    SSPWMduty       VAR BYTE
    
    ;----[Initialize]-----------------------------------------
    OSCCON = %01110000      ' 8Mhz
    ANSEL = %000011         ' AN0 and AN1 ANALOG
    DutyCycle = 10          ' Set Duty Cycle of SSPWM
    gosub StartSPWM         ' Start SSPWM @ Freq/DutyCycle
    HPWM 1, 127, HPWMfreq   ' Start 20Khz PWM @ 50%
    
    ;----[Main Program Loop]---------------------------------
    Main:
        ADCIN 0, HPWMduty
        ADCIN 1, SSPWMduty
    
        IF HPWMDuty != LastHPWMduty THEN
            LastHPWMduty = HPWMDuty
            HPWM 1, HPWMDuty, HPWMfreq
        ENDIF
    
        
        DutyCycle = SSPWMduty*80/256+10  ' scale SSPWM dutycycle
        gosub SetSPWM                    ' Change SSPWM DutyCycle
    GOTO Main
    Dutycycle of the 2Khz is 10% in the image.
    Both dutycycles are adjustable from the pots, and works from 10% to 90%, (0 to 100% for the HPWM).

    Don't forget to comment the wsave vars not used by the 683 in the .inc file.

    Happy Holidays.
    DT

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Incredible I was envisaging pages of code! many thanks to Darrel

    Could you post that Isis code and hex code as i have it and would like to look at the simulation.
    Last edited by retepsnikrep; - 17th December 2010 at 15:57.

Similar Threads

  1. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  2. 2 PWM in 16F690 problems
    By ciendavila in forum mel PIC BASIC
    Replies: 9
    Last Post: - 27th April 2008, 09:03
  3. 12f683 comparator and pwm
    By Automan in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 23rd May 2007, 18:05
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. Variable PWM on 2 Channels using software.
    By Tissy in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 23rd September 2006, 01:34

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts