Misbehaving HPWM on 16F1827


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2005
    Location
    Pinckney, Michigan
    Posts
    91

    Default Misbehaving HPWM on 16F1827

    The following code generates the proper frequency provided I specify 980 or higher.

    If I specify 976 the PWM frequency suddenly divides by 4 to 244Hz.

    If I specify 400, the output frequency again divides by 4 and generates 100Hz.

    What gives?


    Code:
    DEFINE  OSC 4 
    HPWM 1,127,977      '980 = 980Hz, 977 = 980Hz,  976 = 244Hz,, 400 = 100Hz

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


    Did you find this post helpful? Yes | No

    Default Re: Misbehaving HPWM on 16F1827

    Download the latest patch.
    http://melabs.com/support/patches.htm

    The 16F1's have an extra prescaler on Timer2 that wasn't accounted for.
    It was fixed in the "2.60B" patch.
    Current patch is "C".
    DT

  3. #3
    Join Date
    Oct 2005
    Location
    Pinckney, Michigan
    Posts
    91


    Did you find this post helpful? Yes | No

    Default Re: Misbehaving HPWM on 16F1827

    Thanks Darrel.

    I shouldv'e thought of that!

    Do the other PWM channels now work with the PBP direct commands at differing frequencies, or do I still need to "poke" the registers for the other PWM channels? (can't try it until I get home from work, 8 long hours from now).
    Last edited by J. Mark Wolf; - 21st March 2011 at 12:58.

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


    Did you find this post helpful? Yes | No

    Default Re: Misbehaving HPWM on 16F1827

    The HPWM command now works with up to 10 CCP modules, on those chips that have that many.

    It also recognizes what timer has been assigned to each CCP and uses it to adjust the frequency for that and any other CCP module that is also assigned to it.

    The timer assignments must be done manually though, as every family seems to do it differently.
    DT

  5. #5
    Join Date
    Oct 2005
    Location
    Pinckney, Michigan
    Posts
    91


    Did you find this post helpful? Yes | No

    Default Re: Misbehaving HPWM on 16F1827

    Sure enough Darrel!

    I set up the CCPTMRS register to assign the timers, assigned the desired pins (code below) and away it went!

    Now I've got two differing PWM frequencies and the DAC working great!

    Thanks.

    Somebody pinch me!


    Code:
    CCPTMRS = %00100100  ' CCP1=TMR2,CCP3=TMR6 in PWM Mode
    Main:
    '
    DEFINE CCP1_REG PORTB
    DEFINE CCP1_BIT 3           'pin 9
    'DEFINE CCP2_REG PORTA 
    'DEFINE CCP2_BIT 7          'pin 16 
    DEFINE CCP3_REG PORTA       '
    DEFINE CCP3_BIT 3          'pin 2 
    'DEFINE CCP4_REG PORTA
    'DEFINE CCP4_BIT 4          'pin 3 
    '
    HPWM 1,127,400              'apparently uses timer2 by default
    'HPWM 2,100,100
    HPWM 3,127,1000             'assigned to tmr6
    'HPWM 4,50,1000
    '
    goto dac_test

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default Re: Misbehaving HPWM on 16F1827

    I am trying to use the PWM module directly and have 10 bit resolution, but the output is LOW with this:

    Code:
    DEFINE CCP1_REG PORTB
    DEFINE CCP1_BIT 3    
    ccptmrs=0
    pr2=249
    ccp1con=$0C
    trisb.3=0
    ...
    main_loop:
    ...
    ccpr1l=duty.lowbyte
    ...
    goto main_loop
    If I use the PBP command

    Code:
    HPWM 1,duty,2000
    then output follows the duty value.

    Any ideas?

    Ioannis
    Last edited by Ioannis; - 22nd March 2011 at 22:46.

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default Re: Misbehaving HPWM on 16F1827

    OK, I got it.

    Forgot to add the T2CON register.

    Ioannis

  8. #8
    Join Date
    Oct 2005
    Location
    Pinckney, Michigan
    Posts
    91


    Did you find this post helpful? Yes | No

    Default Re: Misbehaving HPWM on 16F1827

    16F1827 code below will allow only 2 of the available 4 PWM channels to run simultaneously. Need 3 Simultaneous PWM channels, all at different frequencies.

    Channel 1 & 3 will work provided channel 4 is not enabled.
    Channel 2 won't run ever.
    Channel 3 always works.
    Channel 3 & 4 will work but breaks channel 1.

    Compiler is PBPX 3.0.1.4

    Sorry couldn't figure out how to wrap "code tags" around the code block.

    Can anyone spot what I'm doing wrong?


    '************************************************* ******************************
    '
    DEFINE OSC 4 'inform compiler of "intended" oscillator freq
    '
    OSCCON = %01101010 '4Mhz internal (%01110000 for 8Mhz)
    DACCON0 = %11100000 'DAC config reg
    DACCON1 = %00000000 '
    ANSELA = %00000000 'All digital. A/D disabled
    ANSELB = %00000000
    APFCON1 = %00000001 'Alternate pin function config
    FVRCON = %00000000
    ADCON0 = %00000001
    ADCON1 = %10010000
    INTCON = %00000000
    '
    APFCON0 = %00001000 'CCP1=RB3,CCP2=RA7 (CCP3=RA3, CCP4=RA4)
    CCPTMRS = %00100100 'CCP1=TMR2,CCP2=TMR4,CCP3=TMR6 in PWM Mode
    '
    pr2 = 77 '400Hz
    t2con = 32
    pr4 = 68 '900Hz
    t4con = 16
    pr6 = 77 '1600Hz
    t6con = 8
    '
    TRISA = %00100001 '
    TRISB = %11000010
    '
    '************************************************* ******************************
    '
    ' HPWM configs
    '
    '************************************************* ******************************
    '
    DEFINE CCP1_REG PORTB
    DEFINE CCP1_BIT 3 'pin 9
    DEFINE CCP2_REG PORTA '
    DEFINE CCP2_BIT 7 'pin 16
    DEFINE CCP3_REG PORTA '
    DEFINE CCP3_BIT 3 'pin 2
    DEFINE CCP4_REG PORTA '
    DEFINE CCP4_BIT 4 'pin 3
    '
    HPWM 1,127,400 'works if CCP4 not enabled
    HPWM 2,127,1600 'CCP2 doesn't work at all
    HPWM 3,127,900 'works always
    'HPWM 4,127,1000 'CCP4 works but breaks CCP1
    STOP
    '
    end

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