16 bit PWM using CCP1


Closed Thread
Results 1 to 40 of 61

Hybrid View

  1. #1
    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 Dave View Post
    What Acetronics2 is trying to say is, when accessing PBP variables inside of an ASM routine you need to prefix an underscore before the variable name such as "movf _PWM_VAL,W ; lb"
    As far as getting more resolution from the PIC processor, Your out of luck. You can always build your own digital pwm circuit with hardware if you are so inclined.
    But Darrel did not do this in his code, prefix with "_". Also as Talat has said he is following Darel's example for "16 bit PWM using CCP1" which obviously worked for Darrel, Bruce and Norm.

    So Talat 16 bit PWM is possible. I have read the whole thread and the datasheets for the 18F452 and the chip you are using 16F1827.

    The first possible issue Darell said his code was for 18F's only in post 3 does anyone know why not 16F's? The 16F has CCPx compare mode.
    With regard to the 16F1827 I think CCP4 has the required features needed and CCP1 does not but I need more time to re-read the datasheet but in the meantime it would be helpful if some one could confirm which CCPx should be used.

  2. #2
    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 EarlyBird2 View Post
    The first possible issue Darell said his code was for 18F's only in post 3 does anyone know why not 16F's? The 16F has CCPx compare mode.
    With regard to the 16F1827 I think CCP4 has the required features needed and CCP1 does not but I need more time to re-read the datasheet but in the meantime it would be helpful if some one could confirm which CCPx should be used.
    Having dealt with the "_", wild goose chase, which had nothing to do with the problem any ideas?

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    A pbp version
    Code:
    '****************************************************************
    '*  Name    : PWM16.BAS                                      *
    '*  Author  : RICHARD                    *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 8/23/2014                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
      
    #CONFIG
                 __config        _CONFIG1,    _FOSC_INTOSC & _CP_OFF & _WDTE_ON  &  _PWRTE_ON  &  _MCLRE_ON  & _CLKOUTEN_OFF
                  __config      _CONFIG2, _PLLEN_ON & _LVP_OFF
    #ENDCONFIG
    
    
     
      include "dt_ints-14.bas"
      
    
    asm
    INT_LIST macro
          
         
         
         
          INT_HANDLER TMR1_INT, _Tm1, asm,yes
          endm
           INT_CREATE
           
         
           
    ENDASM   
    
     
        DEFINE OSC 32 
        
    
         
    
        sSEROUT_PIN VAR PORTA.0
        sBAUD CON 84  '16416 = 19200 BAUD 
    
        yTEMP VAR Byte 
     
        wDUTY  VAR WORD       
        
    
        '***********************************************
    START:
           ' disable analog functions
       OSCCON=$70
       ANSELA=0
       ANSELC=0
    
        '********************
        '16 BIT CCP1 PWM
        T1CON   = 0   ' Turn off Timer1 with a Prescaler value of 1:8
        PIE1    = %00000001    ' enable TMR1 overflow interrupt 
        INTCON  = %11000000    ' enable global and peripheral interrupts 
         ' LOW PORTC.2 ON MATCH TM1 WITH DUTY
        ccp4con=0
        wDUTY = 32768          ' 32768 = 50% DUTY PWM
        T1CON.0 = 1            ' TMR1 ON
        CCPR4H=$80;
        CCPR4L=0;
        PAUSE 800 
        SerOut2 sSEROUT_PIN,sBAUD,["START",13] 
         trisc = %11111001
        '***********************************************
          CCP4CON = %00001001  
    MAIN:
        yTEMP = yTEMP +1 
        SerOut2 sSEROUT_PIN,sBAUD,["LOOP ",Dec yTEMP,13] 
        PAUSE 500
    Goto MAIN
    
    
      '**** ASSEMBLY INTERRUPT ROUTINE ****  
    Tm1: 
      Asm
        bsf PORTC  ,2
        CHK?RP   CCP4CON                  
        clrf      CCP4CON 
        movlw     9            
        movwf     CCP4CON            
          banksel 0 
        bcf      PORTC  ,2
        
      EndAsm
      '**** END ASSEMBLY INTURRUPT ROUTINE **** 
    @ INT_RETURN 
    End

  4. #4
    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
    A pbp version
    Code:
    '****************************************************************
    '*  Name    : PWM16.BAS                                      *
    '*  Author  : RICHARD                    *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 8/23/2014                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
      
    #CONFIG
                 __config        _CONFIG1,    _FOSC_INTOSC & _CP_OFF & _WDTE_ON  &  _PWRTE_ON  &  _MCLRE_ON  & _CLKOUTEN_OFF
                  __config      _CONFIG2, _PLLEN_ON & _LVP_OFF
    #ENDCONFIG
    
    
     
      include "dt_ints-14.bas"
      
    
    asm
    INT_LIST macro
          
         
         
         
          INT_HANDLER TMR1_INT, _Tm1, asm,yes
          endm
           INT_CREATE
           
         
           
    ENDASM   
    
     
        DEFINE OSC 32 
        
    
         
    
        sSEROUT_PIN VAR PORTA.0
        sBAUD CON 84  '16416 = 19200 BAUD 
    
        yTEMP VAR Byte 
     
        wDUTY  VAR WORD       
        
    
        '***********************************************
    START:
           ' disable analog functions
       OSCCON=$70
       ANSELA=0
       ANSELC=0
    
        '********************
        '16 BIT CCP1 PWM
        T1CON   = 0   ' Turn off Timer1 with a Prescaler value of 1:8
        PIE1    = %00000001    ' enable TMR1 overflow interrupt 
        INTCON  = %11000000    ' enable global and peripheral interrupts 
         ' LOW PORTC.2 ON MATCH TM1 WITH DUTY
        ccp4con=0
        wDUTY = 32768          ' 32768 = 50% DUTY PWM
        T1CON.0 = 1            ' TMR1 ON
        CCPR4H=$80;
        CCPR4L=0;
        PAUSE 800 
        SerOut2 sSEROUT_PIN,sBAUD,["START",13] 
         trisc = %11111001
        '***********************************************
          CCP4CON = %00001001  
    MAIN:
        yTEMP = yTEMP +1 
        SerOut2 sSEROUT_PIN,sBAUD,["LOOP ",Dec yTEMP,13] 
        PAUSE 500
    Goto MAIN
    
    
      '**** ASSEMBLY INTERRUPT ROUTINE ****  
    Tm1: 
      Asm
        bsf PORTC  ,2
        CHK?RP   CCP4CON                  
        clrf      CCP4CON 
        movlw     9            
        movwf     CCP4CON            
          banksel 0 
        bcf      PORTC  ,2
        
      EndAsm
      '**** END ASSEMBLY INTURRUPT ROUTINE **** 
    @ INT_RETURN 
    End
    Impressive Richard!

    What was the difference CCP4?

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    no any ccp will work I tested it on 2,3 and 4 , the issue was the Bank selection a pic18 doesn't need it a pic16 will die without it

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    I didn't pickup what ver of pbp talat was using , and I'm not sure if the chk?rp macro exists in early versions


    but try
    Code:
    Asm
    PWM_INT
    CHK?RP   CCP1CON
    movlw 9 ; compare mode
    clrf CCP1CON ; clear reg
    movwf CCP1CON ; Set reg
    CHK?RP  PWM_VAL
    movf PWM_VAL+1,W ; hb
    CHK?RP   CCPRIL
    movwf CCPR1H ; put
    CHK?RP  PWM_VAL
    movf PWM_VAL,W ; lb
    CHK?RP   CCPRIL
    movwf CCPR1L ; put
    banksel 0
    bsf INTCON,7 ;GIE = 1
    bsf INTCON,6 ;PEIE = 1 Enable peripheral interrupts
    bsf PIE1,0 ; TMR1IE= 1 Enable TMR1
    bcf PIR1,0 ; TMR1IF = 0 Clear Timer1 Int Flag
    
    RETFIE ; Return from Interrupt
    EndAsm
    if that fails try

    banksel CCP1CON OR WHATERVER SFR

    OR
    read data sheet and find what bank each sfr is in


    with all that bank switching the effort may be pointless

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    post 35 is wrong pie1 is not in bank0 either

    Code:
    Asm
    PWM_INT
    CHK?RP   CCP1CON
    movlw 9 ; compare mode
    clrf CCP1CON ; clear reg
    movwf CCP1CON ; Set reg
    CHK?RP  PWM_VAL
    movf PWM_VAL+1,W ; hb
    CHK?RP   CCPRIL
    movwf CCPR1H ; put
    CHK?RP  PWM_VAL
    movf PWM_VAL,W ; lb
    CHK?RP   CCPRIL
    movwf CCPR1L ; put
    banksel 0
    bsf INTCON,7 ;GIE = 1
    bsf INTCON,6 ;PEIE = 1 Enable peripheral interrupts
    CHK?RP   PIE1
    bsf PIE1,0 ; TMR1IE= 1 Enable TMR1
    BANKSEL 0
    bcf PIR1,0 ; TMR1IF = 0 Clear Timer1 Int Flag
    
    RETFIE ; Return from Interrupt
    EndAsm

    it just gets worse

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 : 3

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