PWM problem CCP2 port


Closed Thread
Results 1 to 11 of 11
  1. #1

    Default PWM problem CCP2 port

    I'm Using a PIC16F737 and want to use the three pwm ports on it:

    But i get onlu CCP1 and 3 Working, does anybody have a idea?

    I have the following code:

    '************************************************* ***************
    '* Name : UNTITLED.BAS *
    '* Author : [select VIEW...EDITOR OPTIONS] *
    '* Notice : Copyright (c) 2006 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 9/18/2006 *
    '* Version : 1.0 *
    '* Notes : *
    '* : *
    '************************************************* ***************
    @ DEVICE HS_OSC

    Include "modedefs.bas"

    DEFINE OSC 20 ' Gebruikte kristal is 20MHz

    DEFINE CCP1_REG PORTC 'Hpwm 1 pin port
    DEFINE CCP1_BIT 2 'Hpwm 1 pin bit
    DEFINE CCP2_REG PORTB 'Hpwm 2 pin port
    DEFINE CCP2_BIT 3 'Hpwm 2 pin bit
    DEFINE CCP3_REG PORTB 'Hpwm 3 pin port
    DEFINE CCP3_BIT 5 'Hpwm 3 pin bit

    Duty1 VAR WORD ' Channel #1
    Duty2 VAR WORD ' #2
    Duty3 VAR WORD ' #3

    Rval CON 256
    Gval CON 128
    Bval CON 64


    'Rval var word
    'Gval var word
    'Bval var word

    ' Set CCPx pins to outputs
    TRISC.2=0 ' CCP1 output
    TRISB.3=0 ' CCP2 output (could also be assigned to RB3)
    TRISB.5=0 ' CCP3 output
    trisa=%00000111

    ' Set CCP modules to PWM mode
    CCP1CON = %00001100 ' Mode select = PWM
    CCP2CON = %00001100 ' Mode select = PWM
    CCP3CON = %00001100 ' Mode select = PWM

    ' Set period up for 1.22kHz PWM freq
    PR2 = $7F

    ' Set TMR2 up for 1:16 prescale & turn it on
    T2CON = %00000110 ' TMR2 ON 1:16 prescale

    mainloop:


    Duty1 = Bval *2
    CCP1CON.4 = Duty1.0 ' Setup 10-bit duty cycle as
    CCP1CON.5 = Duty1.1 ' a 10-bit word
    CCPR1L = Duty1 >> 2

    Duty2 = Rval *2
    CCP2CON.4 = Duty2.0
    CCP2CON.5 = Duty2.1
    CCPR2L = Duty2 >> 2

    Duty3 = Gval *2
    CCP3CON.4 = Duty3.0
    CCP3CON.5 = Duty3.1
    CCPR3L = Duty3 >> 2

    Goto mainloop
    End

  2. #2


    Did you find this post helpful? Yes | No

    Default

    Hi. I'm using a 16F73 with 2 HPWM's and have had no problems. I didn't use any DEFINEs. I just set the 2 PWM ports as outputs and use:

    HPWM 1,LEFT,15000 'SEND 15KHZ WITH DUTY CYCLE VALUE IN VARIABLE LEFT
    HPWM 2,RIGHT,15000 'SEND 15KHZ WITH DUTY CYCLE IN VARIABLE RIGHT
    DO THIS AND THAT
    LET CCP1CON = 0 'TURN OFF LEFT PWM
    LET CCP2CON = 0 'TURN OFF RIGHT PWM
    Low PORTC.1 'CCP1 OUTPUT LOW
    Low PORTC.2 'CCP2 OUTPUT LOW

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


    Did you find this post helpful? Yes | No

    Default

    NL2TTL,

    Seems everyone is forgetting the analog ports these days.

    ADCON1 = $0F

    Added: It will probably be better to have subroutines that set the dutycycle of the CCP modules. Then call them only when needed.
    Continually setting the registers in the main loop may cause some problems, and isn't needed since the hardware will continue putting out the PWM while the main program is doing other things.

    Also, by using these statements Duty3 = Gval *2, it effectively reduces the PWM resolution to 9-bit since bit0 will always be 0.

    Added2: The RB3/CCP2 pin is the alternate CCP2 output. You'll need to change the CCPMX configuration fuse for it to work there.
    <br>
    Last edited by Darrel Taylor; - 18th September 2006 at 18:48. Reason: Coffee first, answers later :)
    DT

  4. #4


    Did you find this post helpful? Yes | No

    Default

    @Darrel Taylor

    Thank you for you replay, about adcon and the Rb3 i understand, but what exactly do you mean about:

    Also, by using these statements Duty3 = Gval *2, it effectively reduces the PWM resolution to 9-bit since bit0 will always be 0

    How can i fix that?

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


    Did you find this post helpful? Yes | No

    Default

    In the binary world, multiplying by 2 is the same as shift left 1 (<< 1).

    This will always leave a 0 it the bit0 location. So if you had a dutycycle of say 257 (100000001), * 2 would be 514 (1000000010).

    The easiest way to fix it is to just use bigger numbers. 10-bit numbers range from 0 to 1023. <br>
    DT

  6. #6


    Did you find this post helpful? Yes | No

    Default

    I've added:

    ADCON1 = $0F

    But still ccp2 doesn;t work (also on portc.2 it gives problems)

    The pin is always high.

  7. #7


    Did you find this post helpful? Yes | No

    Default

    And correct me if i'm wrong but when i don't make a loop in the program i get no response on any pwm port

    So i need the Goto mainloop

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


    Did you find this post helpful? Yes | No

    Default

    Did you also change the CCPMX configuration fuse for RB3/CCP2 alternate?

    portc.2 is CCP1, are you saying that that one stopped working ?

    And yes, you need to have a mainloop. Otherwise it goes to the END statement and enters Sleep mode. But changing the CCP registers doesn't need to be in the mainloop.
    <br>
    DT

  9. #9


    Did you find this post helpful? Yes | No

    Default

    it's working thank you very much!

  10. #10


    Did you find this post helpful? Yes | No

    Default

    Oke when change the value to 1023 what to do with the >> 2 value?

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


    Did you find this post helpful? Yes | No

    Default

    It'll stay the same as you had it before. As will the
    CCP?CON.4 = Duty?.0
    CCP?CON.5 = Duty?.1


    Unless I'm missing the point of the question.
    DT

Similar Threads

  1. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  2. PWM Problem
    By cihhan in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 14th June 2008, 18:43
  3. PWM setting PIC16F887
    By Gevo in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th June 2008, 07:24
  4. Problem with A port on 18F2550
    By NL2TTL in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 9th May 2007, 22:59
  5. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27

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