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

    Code:
    '************************************************* ***************
    '* Name : *
    '* Author : [select VIEW...EDITOR OPTIONS] *
    '* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 09.08.2014 *
    '* Version : 1.0 *
    '* Notes : 16F1827 *
    '* : *
    '************************************************* ***************
    ' DEVİCE 16F1827
    
    ASM
    __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
    __config _CONFIG2, _PLLEN_ON & _LVP_OFF & _LVP_OFF & _STVREN_OFF; & _VCAPEN_OFF
    ENDASM
    CLEAR
    DEFINE OSC 32
    OSCCON=%01110000 '32
    
    DEFINE INTHAND PWM_INT
    
    PWM_VAL VAR WORD BANK0 SYSTEM
    VALUE VAR WORD
    
    INIT:
    APFCON0 = 0 'SET PORTB.3 TO CCP1
    T1CON = %00100000 'Prescaler 1:4
    
    PIE1.0 = 1 'TMR1IE TMR1 Interrupt Enable
    PIR1.0 = 0 'TMR1IF TMR1 Interrupt Flag
    INTCON.7 = 1 'GIE Global Interrupt Enable
    INTCON.6 = 1 'PEIE Peripheral Interrupt Enable
    CCP1CON = 9 'compare mode
    OUTPUT PORTB.3 'CCP PIN
    
    
    T1CON.0 = 1 'TMR1ON SET TMR1
    PAUSE 1000
    VALUE=1000
    
    MLOOP:
    VALUE=VALUE+1
    PWM_VAL = VALUE
    Goto MLOOP
    
    
    Asm
    PWM_INT
    movlw 9 ; compare mode
    clrf CCP1CON ; clear reg
    movwf CCP1CON ; Set reg
    movf PWM_VAL+1,W ; hb
    movwf CCPR1H ; put
    movf PWM_VAL,W ; lb
    movwf CCPR1L ; put
    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
    
    END
    Now we know that what you want to do is possible, thanks to Richard. Can anyone work out what is wrong with Talat's code?

  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

    Talat

    I do not understand why you included the lines in red in you ISR

    Code:
    '************************************************* ***************
    '* Name : *
    '* Author : [select VIEW...EDITOR OPTIONS] *
    '* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 09.08.2014 *
    '* Version : 1.0 *
    '* Notes : 16F1827 *
    '* : *
    '************************************************* ***************
    ' DEVİCE 16F1827
    
    ASM
    __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
    __config _CONFIG2, _PLLEN_ON & _LVP_OFF & _LVP_OFF & _STVREN_OFF; & _VCAPEN_OFF
    ENDASM
    CLEAR
    DEFINE OSC 32
    OSCCON=%01110000 '32
    
    DEFINE INTHAND PWM_INT
    
    PWM_VAL VAR WORD BANK0 SYSTEM
    VALUE VAR WORD
    
    INIT:
    APFCON0 = 0 'SET PORTB.3 TO CCP1
    T1CON = %00100000 'Prescaler 1:4
    
    PIE1.0 = 1 'TMR1IE TMR1 Interrupt Enable
    PIR1.0 = 0 'TMR1IF TMR1 Interrupt Flag
    INTCON.7 = 1 'GIE Global Interrupt Enable
    INTCON.6 = 1 'PEIE Peripheral Interrupt Enable
    CCP1CON = 9 'compare mode
    OUTPUT PORTB.3 'CCP PIN
    
    
    T1CON.0 = 1 'TMR1ON SET TMR1
    PAUSE 1000
    VALUE=1000
    
    MLOOP:
    VALUE=VALUE+1
    PWM_VAL = VALUE
    Goto MLOOP
    
    
    Asm
    PWM_INT
    movlw 9 ; compare mode
    clrf CCP1CON ; clear reg
    movwf CCP1CON ; Set reg
    movf PWM_VAL+1,W ; hb
    movwf CCPR1H ; put
    movf PWM_VAL,W ; lb
    movwf CCPR1L ; put
    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
    
    END
    As I do not have a 16F1827 I can not do any testing. But after some thought I used an 18F452 to test your code and it did not work. I then compared data sheets for the 16F1827 and 18F452 and finding no reason for the lines in red I commented them out reprogramed the 18F452 and it ran. But I had to add a pause 10 in the main loop so my voltmeter could display the readings.

    Here is my version

    Code:
    '************************************************* ***************
    '* Name : *
    '* Author : [select VIEW...EDITOR OPTIONS] *
    '* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 09.08.2014 *
    '* Version : 1.0 *
    '* Notes : 16F1827 *
    '* : *
    '************************************************* ***************
    ' DEVİCE 16F1827
    
    ASM
    __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
    __config _CONFIG2, _PLLEN_ON & _LVP_OFF & _LVP_OFF & _STVREN_OFF; & _VCAPEN_OFF
    ENDASM
    CLEAR
    DEFINE OSC 32
    OSCCON=%01110000 '32
    
    DEFINE INTHAND PWM_INT
    
    PWM_VAL VAR WORD BANK0 SYSTEM
    
    
    INIT:
    APFCON0 = 0 'SET PORTB.3 TO CCP1
    T1CON = %00100000 'Prescaler 1:4
    
    PIE1.0 = 1 'TMR1IE TMR1 Interrupt Enable
    PIR1.0 = 0 'TMR1IF TMR1 Interrupt Flag
    INTCON.7 = 1 'GIE Global Interrupt Enable
    INTCON.6 = 1 'PEIE Peripheral Interrupt Enable
    CCP1CON = 9 'compare mode
    OUTPUT PORTB.3 'CCP PIN
    
    
    T1CON.0 = 1 'TMR1ON SET TMR1
    PAUSE 1000
    
    
    MLOOP:
    PWM_VAL = PWM_VAL+1
    PAUSE 10
    Goto MLOOP
    
    
    Asm
    PWM_INT
    movlw 9 ; compare mode
    clrf CCP1CON ; clear reg
    movwf CCP1CON ; Set reg
    movf PWM_VAL+1,W ; hb
    movwf CCPR1H ; put
    movf PWM_VAL,W ; lb
    movwf CCPR1L ; put
    bcf PIR1,0 ; TMR1IF = 0 Clear Timer1 Int Flag
    
    RETFIE ; Return from Interrupt
    EndAsm
    
    END
    I am very interested to know if my version works on the 16F1827 that you have.

  3. #3
    Join Date
    Nov 2009
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: 16 bit PWM using CCP1

    Thanks
    Yes, you're right.
    My first version was like you, did not work. To better understand the problem I have added including red lines.
    I tried again and the result is the same version. In the start-up process produces two pulses and then stops.

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