HPWM ....use Timer0?


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default HPWM ....use Timer0?

    Hello folks

    I'm dabbling 12LF1822 which only has three timers available TMR0, TMR1 & TR2.

    I need a 16 bit timer fpor capactive touch so that'll be TMR1 (to count the output from the CPS module) & timer2 is needed for the capacitive touch 'timebase'.

    therefore that only leaves only Timer0 for HPWM.

    I've used the following define...

    DEFINE HPWM1_TMR 0

    ....but not sure what the limitations are if associating Timer0 with HPWM?

    I'm ideally looking for HPWM in the 40-50khz range ....is this gonna be possible here? (the manual says no higher than 32767Hz is do-able .....are there any workarounds possible...if not, under what conditions is that 32,757 reachable...I'm running the OSC at 8MHZ internal)

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: HPWM ....use Timer0?

    Hi,
    I don't think the 12LF1822 allows you to "switch" timers for the CCP module, some do, this one seems not. As far as I can see it requires the use of TMR2 in PWM mode.

    It looks to me like you should be able to use TMR0 for the timebase and TMR1 to do the "counting". Or why not use TMR1 to "count the pulses" and use your PWM timebase (TMR2) as the timebase for capacitive module as well? I have no idea what kind of periods we're talking about here but if the PWM period is too short to use as a timebase for the capacitive module you can use the TMR2 postscaler to divide that down further, tripping an interrupt every 1:2 - 1:16 cycle, meaning that with a PWM frequency of 20kHz you can get a timebase for the capsense as low 20k/16=1250Hz, is that too fast?

    EDIT: Sorry, forgot that last question. Not sure what the actual limitations on HPWM are but you probably want to set up the CPP module "manually" instead of using HPWM. With 8MHz oscillator you can easily get 50kHz, the datasheet shows you all about how to calculate the values and I believe I've written a post or two about how it works (but how do I find that now...). I usuallt cheat though and use PICMultiCalc which, in this case tells me that with 8Mhz oscillator you'll get 50kHz by setting PR2 to 39. The dutycycle register then ranges from 0-160 for 0-100% dutycycle.

    And again, at 50kHz PWM frequency and TMR2 postscaler of 1:16 you'll get an interrupt rate of ~3kHz. If that's too fast you can always do a little software postscaler as well. Or simply use TMR0 if you don't need it for anything else.

    /Henrik.
    Last edited by HenrikOlsson; - 18th June 2011 at 07:36.

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: HPWM ....use Timer0?

    Hi Henrik,

    Many thanks....after I posted that and went to bed ...I was lying with this still in my mind and then the penny dropped (pretty sad that I think about timers lying in bed with a foxy wife next to me!)....yes, you're quite right I can just use Timer0 as the timebase which frees up Timer2 for HPWM (which I've just done a program that uses manual HPWM @50khz that I can lift chunks from).

    ..once more unto the breach!

    Many thanks!

    PS as ever, you're right on the money with your HPWM calculations, just dug out that associated code ...

    Code:
    'HPWM SETTINGS (MANUAL SETTING OF THE REGISTERS VS THE PICBASIC HPWM COMMAND) uses timer 2
    CCP1CON  = %00001100     'Turn HPWM on on CCP1
    CCPR1L.6 = 0             'I'm only using 8 bit PWM so clear the top two bits
    CCPR1L.7 = 0             'I'm only using 8 bit PWM so clear the top two bits
    CCPTMRS0 = 0            'CCP 'Timer select' register - these bits select 'CCP2 USING TIMER2'
    PR2 =41                     'a maximum of  167 steps available at a PWM FREQUENCY OF 48kHZ.
    T2CON = %00000100        'TIMER2 ON 1:1 PRESCALER 1:1 POSTSCALER
    Last edited by HankMcSpank; - 18th June 2011 at 09:21.

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