Low freq PWM problem


Results 1 to 6 of 6

Threaded View

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


    Did you find this post helpful? Yes | No

    Default

    You might also consider using the PWM module along with a small interrupt driven "helper" to link together several smaller PWM period "frames" into the much longer 120-Hz PWM period. This method produces a relatively low overhead high resolution jitter free PWM output.

    I don't have PBP but perhaps you and other Forum members can muddle through this pseudo C code example.

    This example uses a 12-MHz clock (Tcy = 0.3334-usecs) with PWM module settings that produce a PWM period of 333.3334-usecs (250 x 1.3334-usec 'steps'). The interrupt "helper" links together 25 of these short 333.3334-usec PWM period "frames" to produce a precise 120-Hz (8333.3334-usec) period with an output pulse width of 0 to 6250 x 1.3334-usec "steps".

    Regards, Mike

    Code:
    ;******************************************************************
    ;
    ;  // setup PWM for a 333.3 usec period (12-MHz xtal, Timer 2
    ;  // prescale 4:1, PR2 = 249) and use 25 of these 333.3 usec
    ;  // PWM "frames" for a precise 120-Hz (8.3334 msec) period.
    ;
    ;  char frame = 1;              // initialize "frame" var'
    ;  int width = 100;             // 0..6250 (1.333 usec steps)
    ;  int pulse;                   // isr work variable, 0..6250
    ;
    ;  void interrupt()             // 120-Hz example
    ;  { pir1.TMR2IF = 0;           // clear timer 2 interrupt flag
    ;    frame--;                   // decrement 'frame' number
    ;    if(frame == 0)             // if end-of-period
    ;    { frame = 25;              // reset for new period
    ;      pulse = width;           // refresh work variable
    ;    }
    ;    if(pulse > 250)            //
    ;    { ccpr1l = 250;            // do a 100% duty cycle frame
    ;      pulse -= 250;            // update 'pulse'
    ;    }
    ;    else                       //
    ;    { ccpr1l = pulse;          // do variable duty cycle
    ;      pulse = 0;               // force remaining frames to 0%
    ;    }
    ;
    ;
    ;
    ;  void Main()                  //
    ;  { cmcon0 = 7;                // comparators off
    ;    ansel = 0;                 // a2d convertor off
    ;    pir1 = 0;                  // clear all interrupt flags
    ;    pie1.TMR2IE = 1;           // enable TMR2 interrupts
    ;    ccpr1l = 0;                // set 0% duty cycle (output off)
    ;    t2con = 0b00000101;        // '0-------' unimplemented bit
    ;                               // '-0000---' TOUTPS<3:0>, postscale 1
    ;                               // '-----1--' TMR2ON, turn Timer 2 on
    ;                               // '------01' T2CKPS<1:0>, prescale 4
    ;    pr2 = 250-1;               // 250 x 1333-nsec 'ticks' = 333 usecs
    ;    intcon = 0b11000000;       // '1-------' GIE, enable global ints
    ;                               // '-1------' PEIE, enable peripheral ints
    ;                               // '--0-----' T0IE, TMR0 ints disabled
    ;                               // '----0---' GPIE, IOC disabled
    ;                               // '-----000' T0IF/INTF/GPIF flags
    ;    while(1)                   //
    ;    {                          //
    ;    }                          //
    ;  }
    ;
    Last edited by Mike, K8LH; - 7th February 2010 at 13:53.

Similar Threads

  1. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  2. confused problem with interrupt in a working program
    By illuminator4 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 14th November 2008, 17:01
  3. problem with PWM
    By mimmmis in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 12th September 2008, 13:36
  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. 4-line LCD Help - using PortA instead of B
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 28
    Last Post: - 31st March 2005, 03:14

Members who have read this thread : 1

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