ProblemwithPWMfrequency


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    Join Date
    Oct 2011
    Posts
    52

    Default ProblemwithPWMfrequency

    Hi to every one
    My aim is to generate 5kHZ SPWM so that when filtered will give sine waves(50hz). I havebeen reading different application manual and PIC16877 and Pic16f777 datashets for almost two days but I 'm still in darkness the problem comes for that frequency my duty cycle will have 10bit resolution. here are the question I have and the code I wrote, any one can help me.

    (1) I simulate it using pic16f877 but I dont see any output

    Code:
    define OSC 4
    Wsave var byte   $70system
    Wsave1 var byte  $A0system
    Wsave2 var byte  $120system
    Wsave3 var byte  $1A0system
    
    
        'pointer for phase one in sinearray
                    'pointer for phase two  in sine array
    ADCON0 = %00000000
    ADCON1 = %00000000
    LED1   VAR  PORTB.1
    
    TRISB = %11111101
    TRISC = %11111001  'PMW output for CCP1 AND CCP2
    TRISA = %11111111
    PR2 =  199   'set for 5Khz HPWM
    CCP1CON = %00001100 'set CCP1 for PWM OPERATION
    CCP2CON = %00001100 'set CCP2 for PWM OPERATION
    T2CON = %00000100  'TIMER2ON and prescale of 1:1 
    index var byte
    temp var word
    timerone var word 
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System          emp 
    ASM
     
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,   _test,   ASM,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
     
    T1CON = 000001                 ; Prescaler = 1;1, TMR1 ON
    @   INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts 
     
    timerone = 64980 ;gives about 50 htz sine  
                       
    test: 
     T1CON = 000000 'stop the timer
        TMR1L = timerone.byte0  'reload the timer
        TMR1H = timerone.byte1
        T1CON = 000001          ' start the timer
        gosub display
        CCP1CON.4 = Temp.0 ' bit 0
        CCP1CON.5 = Temp.1 ' bit 1       
        CCPR1L = Temp>>2  'Bit 2-7 
        
      display:
    lookup2 index[115,102,90,79,70,62,57,53,52,53,57,62,70,79,90,102,115,128,141,154,166,177,186,194,199,203,204,203,199,194,186,177,166,154,141,128],temp
      return          
       
       index=index+1
        if index =36 then index =0      
     @    INT_RETURN
    please any one one help me to solve this problem

    thankx

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    Several things.
    1) There is no "main loop" in the program so the execution falls thru into the interrtupt handler and then it hits the @INT RETURN macro which makes it return to what?

    2) You have the display subroutine located in the middle of your code. When you're using subroutines you need to make sure that the program can't "reach" them without actually being called by a GOSUB. In your program the display subroutine gets exectued one time when called by the GOSUB (which is all OK) but then it gets executed again after you've loaded the CCP1 registers and since there's a RETURN at the end of the subroutine it jumps to somewhere it shouldn't.

    3) You have the interrupt handler declared as ASM even though you're using PBP statements in the handler. I've said this to you in one of your other threads covering the same topic: Do NOT do that if you don't know what you're doing. If you don't understand WHY you can either read up on it here on the forum but if you don't care about the details simply follow the rules given: If the handler is in PBP then declare it as type PBP - not ASM.

    /Henrik.

  3. #3
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi every one

    Thanks HenrikOlsson for your coments on this code. I worked on your coments and do some monifications, here is challenge I faced
    (1) I declare my handler as PBP type when I compile the code gives the following error WARNING: Unable to open INCLUDE file REENTERPBP-14.BAS although I include that file in the compilation folder why is this error?

    (2) I'm confusing why the program declared as ASM type having PBP statement and the Label does not have an underscore before it gives error while that declared as ASM type having PBP statement and the Label have an underscore before it gives gives NO error.But accorgni to Darrel PBP type label should have un underscore before it.

    code
    Code:
    define OSC 4
    Wsave var byte   $70system
    Wsave1 var byte  $A0system
    Wsave2 var byte  $120system
    Wsave3 var byte  $1A0system
    
    
        'pointer for phase one in sinearray
                    'pointer for phase two  in sine array
    ADCON0 = %00000000
    ADCON1 = %00000000
    LED1   VAR  PORTB.1
    
    TRISB = %11111101
    TRISC = %11111001  'PMW output for CCP1 AND CCP2
    TRISA = %11111111
    PR2 =  199   'set for 5Khz HPWM
    CCP1CON = %00001100 'set CCP1 for PWM OPERATION
    CCP2CON = %00001100 'set CCP2 for PWM OPERATION
    T2CON = %00000100  'TIMER2ON and prescale of 1:1 
    index var byte
    temp var word
    timerone var word 
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System          emp
    INCLUDE "ReEnterPBP-14.bas"  
    ASM
     
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,   _TEST,  PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
     
    T1CON = 000001                 ; Prescaler = 1;1, TMR1 ON
    @   INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts 
     
    timerone = 64980 ;gives about 50 htz sine 
    Main:
           PAUSE 5
     
    GOTO Main
    TEST: 
     T1CON = 000000 'stop the timer
        TMR1L = timerone.byte0  'reload the timer
        TMR1H = timerone.byte1
        T1CON = 000001          ' start the timer
        gosub display
        CCP1CON.4 = Temp.0 ' bit 0
        CCP1CON.5 = Temp.1 ' bit 1       
        CCPR1L = Temp>>2  'Bit 2-7 
        index=index+1
      if index =36 then index =0 
         
     @    INT_RETURN
     
     display:
      lookup index,[115,102,90,79,70,62,57,53,52,53,57,62,70,79,90,102,115,128,141,154,166,177,186,194,199,203,204,203,199,194,186,177,166,154,141,128],temp
        
      return    'return to the subroutine 
      return    'return to the original GOSUB
    thankx

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    1) As far as I can see the file is called ReEnterPBP.bas. For some reason you're trying to include ReEnterPBP-14.bas.

    2) The compiler (PBP) adds an underscore in front of all variables and labels so the label Test becomes _Test in the compiled program. This is so that any variables and labels you create does not interfere with whatever system variables PBP creates internally. The interrupt declaration, ie this part...
    Code:
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,   _TEST,  PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    ...is, as you can see by ASM/ENDASM directives assembly code which is passed directly to the assembler. So the label needs (_test) needs to have the underscore infront of it here because once the assembler is invoked your Test: label is called _Test. I hope that makes sense.

  5. #5
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    thankx for the explaination about underscore I understood it very well why under score. But your statement below is not clear to me
    "As far as I can see the file is called ReEnterPBP.bas. For some reason you're trying to include ReEnterPBP-14.bas".
    thankx

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    If you go to Darrel's website and download the .zip file for DT_INTS-14 you get two (2) files. One is called DT_INTS-14.bas and the other is called ReEnterPBP.bas. In your program you are correctly icluding DT_INTS-14.bas but you are, incorrectly trying to include a file called ReEnterPBP-14.bas - your code:
    Code:
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System          emp
    INCLUDE "ReEnterPBP-14.bas"   '<-----Here, you have the wrong filename.
    See, the file your're TRYING to include is ReEnterPBP-14.bas but the file is called ReEnterPBP.bas (if you haven't renamed it in which case I don't what the problem is).

    /Henrik.

  7. #7
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Thankx it compile fine but the output is stationary. I 'm confusing why is it.I attach simulation of the output as seen from ISIS

    [CODE]
    define OSC 4
    Wsave var byte $70system
    Wsave1 var byte $A0system
    Wsave2 var byte $120system
    Wsave3 var byte $1A0system
    ADCON0 = %00000000
    ADCON1 = %00000000
    LED1 VAR PORTB.1

    TRISB = %11111101
    TRISC = %11111001 'PMW output for CCP1 AND CCP2
    TRISA = %11111111
    PR2 = 199 'set for 5Khz HPWM
    CCP1CON = %00001100 'set CCP1 for PWM OPERATION
    CCP2CON = %00001100 'set CCP2 for PWM OPERATION
    T2CON = %00000100 'TIMER2ON and prescale of 1:1
    index var byte
    temp var word
    timerone var word
    INCLUDE "DT_INTS-14.bas"
    INCLUDE "ReEnterPBP.bas"
    ASM

    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler TMR1_INT, _TEST, PBP, yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM

    T1CON = 000001 ; Prescaler = 1;1, TMR1 ON
    @ INT_ENABLE TMR1_INT ; Enable Timer 1 Interrupts

    timerone = 64980 ;gives about 50 htz sine
    Main:
    PAUSE 5

    GOTO Main
    TEST:
    T1CON = 000000 'stop the timer
    TMR1L = timerone.byte0 'reload the timer
    TMR1H = timerone.byte1
    T1CON = 000001 ' start the timer
    gosub display
    CCP1CON.4 = Temp.0 ' bit 0
    CCP1CON.5 = Temp.1 ' bit 1
    CCPR1L = Temp>>2 'Bit 2-7
    index=index+1
    if index =36 then index =0

    @ INT_RETURN
    Code:
     
     display:
      lookup index,[115,102,90,79,70,62,57,53,52,53,57,62,70,79,90,102,115,128,141,154,166,177,186,194,199,203,204,203,199,194,186,177,166,154,141,128],temp
        
      return    'return to the subroutine 
      return    'return to the original GOSUB
    Attached Images Attached Images

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi,
    Not that I think it's a problem but why TWO returns in the subroutine? The execution will never get to the second one.

    Are you sure that the output really IS "stationary", it's not just that you have the horisontal sweep set to fast?
    Wrap the ISR in a LED1 = 1 and LED1 = 0 and sample PortB.1 too with your "scope", that way you'll see IF and and what frequency the interrupt ACTUALLY runs as well as how long it takes to execute.
    With 4MHz crystal, PR2 set to 199 and prescaler set to 1:1 you should be getting 5kHz PWM but your "scope" (if I read it correctly) shows a period of 400us which means a PWM frequency is 2.5kHz, why is that?

Members who have read this thread : 0

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