16 bit PWM using CCP1


Closed Thread
Results 1 to 40 of 61

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel,

    Pretty cool stuff. Have you tested this on other 18F parts? I don't have a 4520 to test
    with, but I couldn't get this to work without clearing CCP1CON before moving 9 to it on
    a 452 or 4431.

    Had to do this;
    Code:
    ASM
    MyInt                    
        clrf CCP1CON         ; 18F452 & 18F4431 require this before it changes CCP1 output
        movlw 9
        movwf CCP1CON
        bcf PIR1,TMR1IF      ; clear TMR1 int flag
        retfie FAST          ; use RETFIE FAST to restore WREG, STATUS & BSR
    ENDASM
    On an 18F452 or 18F4431, the above works fine, but only if I clear CCP1CON first.

    This works on a 452 & 4431 with CCP1 & TMR1 ints enabled on any output pin;
    Code:
    ASM
    MyInt                    
        btfss PIR1,TMR1IF    ; TMR1 interrupt?
        bra CCP              ; no - service CCP interrupt
        bcf PIR1,TMR1IF      ; yes - clear TMR1 int flag
        bsf LATB,0           ; set RB0 on TMR1 interrupt
        retfie FAST          ; return
    CCP
        bcf PIR1,CCP1IF      ; clear CCP1 int flag
        bcf LATB,0           ; clear RB0 on compare interrupt
        retfie FAST          ; use RETFIE FAST to restore WREG, STATUS & BSR
    ENDASM
    I was just wondering if this might be a fluke with the 4520 part - since none of the other
    18F parts I've tested will toggle CCP1 with just the movlw 9, movwf CCP1CON?
    Last edited by Bruce; - 14th December 2009 at 02:34. Reason: 2nd version uses CCP1CON = %00001010
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

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


    Did you find this post helpful? Yes | No

    Default

    Hmmm, very interesting.

    I had not tried it on any other chips, and with the 4520 you definitely don't have to clear it first.

    But I just tried an 18F452, and you are absolutely correct.
    It doesn't work unless you clear the CCPCON register first.

    Very interesting indeed,
    Thanks Bruce.
    <br>
    DT

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Thanks. I was just curious. I had fought with this one before on a servo driver, and just
    couldn't get it to work without both ints enabled, or clearing CCP1CON first.

    I'm thinking it may be due to the huge number of problems Mchip had (see erratas') on
    ECCP & CCP with this one?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    I'm thinking it may be due to the huge number of problems Mchip had (see erratas') on ECCP & CCP with this one?
    No doubt!

    BTW, I like the any Output Pin variation too.
    <br>
    DT

  5. #5
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default

    Works good!
    Thanks Darrel

    Would it be correct to say the maximum PWM frequency for a 40 MHz PIC is 152 Hz?
    To slow for music.
    Know of any > 10 bit PWM PIC's?

    Norm

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


    Did you find this post helpful? Yes | No

    Default

    Yup, that's what mister-e's calculator says with 40mhz, 1:1, and 16-bit. 152 hz.

    The PIC18F2331/2431/4331/4431 series has a bunch of 14-bit PWM modules. But they are completely different from the CCP module.

    Bruce has done a few examples, but I don't think any of them were for music.

    best regards,
    DT

  7. #7
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default

    PIC18F2331/2431/4331/4431:

    FOSC 40 MHz
    MIPS 10
    PWM Resolution 14 bits
    PWM Frequency 2.4 kHz

    14 bit to slow for CD quality music.
    10 bit sounds good though.

    A dsPIC would be ideal.

    On my first post my code included saving the state of the critical registers.
    Would this be required in a longer program?

    Norm
    Last edited by Normnet; - 14th December 2009 at 05:57.

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    just tried your "red" code in the isr on a pic18f45k20

    you are right

    bsf INTCON,6 ;PEIE = 1 Enable peripheral interrupts
    bsf PIE1,0 ; TMR1IE= 1 Enable TMR1

    these two lines lock it up. can't see why yet

    it looked harmless but its not ,its much worse than useless

  9. #9
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    The point of coming here is to learn and I am now comparing the architecture of the 18F452 and the 16F1827. They are completely different, not surprise there.

    I have found the sfrs are in bank 5 on the 16F1827 but I have no idea how to use this information.

  10. #10
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    CALLS TO ASM always set bank 0

    pbp has a macro you can use "chk?rp sfr" it will ckeck that the appropriate bank is set (see status or bank sfr)
    the mpasm assembler can also do a " BANKSEL SFR"

    BEFORE YOU RETURN T0 PBP YOU MUST SET BANK BACK TO BANK 0
    Last edited by richard; - 26th August 2014 at 11:23. Reason: still can't spell

  11. #11
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    bsf INTCON,7 ;GIE = 1
    bsf INTCON,6 ;PEIE = 1 Enable peripheral interrupts
    bsf PIE1,0 ; TMR1IE= 1 Enable TMR1

    intcon bits 6 and 7 should already be set , setting them again should do no harm
    pie1.0 is also set , setting it again can't hurt

    it just wastes 12 cpu cycles

    the problem lies elsewhere, PWM_VAL must be > 30 ,values from 0-29 wont work could that have happened

  12. #12
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    intcon bits 6 and 7 should already be set , setting them again should do no harm
    pie1.0 is also set , setting it again can't hurt
    That is exactly what I thought and could not find anything to indicate otherwise in the datasheet.

    the problem lies elsewhere, PWM_VAL must be > 30 ,values from 0-29 wont work could that have happened
    No I started PWM_VAL for zero and incremented up in steps of 1 and it worked all the way rolling over and starting from 0. If 0-29 does not work I could not detect it with my voltmeter.

  13. #13
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    N
    o I started PWM_VAL for zero and incremented up in steps of 1 and it worked all the way rolling over and starting from 0. If 0-29 does not work I could not detect it with my voltmeter
    I'm using a cro those <30ish pwm's cause the output pin to be permanently high when you expect a very small pw , in real life that could be nasty
    try a loop range yourself of 0 to say 28

  14. #14
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    Quote Originally Posted by richard View Post
    N
    I'm using a cro those <30ish pwm's cause the output pin to be permanently high when you expect a very small pw , in real life that could be nasty
    try a loop range yourself of 0 to say 28
    Just tried it no problem 0 gave 0 volts 9 gave 3 mV.

  15. #15
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    not on a cro

    using a main loop like this
    MAIN:

    PWM_VAL = PWM_VAL+1
    PAUSE 500
    if pwm_val > 29 then pwm_val=0
    toggle portd.1
    Goto MAIN

  16. #16
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    Quote Originally Posted by richard View Post
    not on a cro
    What is a cro?

  17. #17
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    Quote Originally Posted by richard View Post
    N
    I'm using a cro those <30ish pwm's cause the output pin to be permanently high when you expect a very small pw , in real life that could be nasty
    try a loop range yourself of 0 to say 28
    I will take your word for it as I have no instrument that will replicate your results, and as I have found in the past just because I can not see it does not mean it is not there.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Half-bridge PWM with a 16F684 ?
    By Byte_Butcher in forum General
    Replies: 7
    Last Post: - 17th January 2010, 22:18
  3. PICBasic newbie problem
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 12th February 2008, 00:55
  4. How to tell which PICs can handle 16 bit variables?
    By MikeTamu in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 31st August 2005, 08:44
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

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