ProblemwithPWMfrequency


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    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

  2. #2
    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.

  3. #3
    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

  4. #4
    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.

  5. #5
    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

  6. #6
    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?

  7. #7
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Thankx HenrikOlsson for the comments I worked on on them

    For what I know the gosub coment within subroutine takes two return. The first return will bring you back to the subroutine(TEST) and the second return will bring you to the original gosub comand if I'm not wrong.

    I havebeen worked on the code for almost whole day I'm still getting the same output and I try know my interrupt frequency by Toggling PortB.1 the problem comes I cant see the square wave comes from that pin. here is code and output 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"     ; Base Interrupt System          
    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
        TOGGLE LED1  
        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
    I don't understand why the output is 2.5Kh instead of 5khz

    help me on that problems
    thankx
    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,
    No you need one RETURN at the end of the subroutine. That RETURN makes the code return to line after the GOSUB, the @INT_RETURN makes it return from the interrupt handler.

    Are you sure you have the scope connected to the correct output?


    The first thing you need to do i make sure that the PIC and that simulator of yours is running properly and that you're using it properly. Remove all the interrupt and PWM stuff and then make a simple program that just toggles that LED1 output. Connect your "scope" and make sure you can see the output toggle on the scope.

    When and only when you have that working put the interrupt stuff back in but move the TOGGLE command to the interrupt handler - EXACTLY as shown here. Don't try to add all your PWM stuff.

    When and only when you can see that LED1 output toggling on the scope try adjusting the interrupt frequency and see if that works the way you want.

    EDIT: Now make sure that you can setup and control the dutycycle of the PWM module in the main loop (still no interrupts). Just toggle the dutycycle between two values and make sure you can see that on the scope.

    Now, when you have THAT working you are ready to start adding the PWM stuff.

    Don't try to skip a step until you have the previous one working and understand why and how.
    Last edited by HenrikOlsson; - 29th February 2012 at 18:31.

  9. #9
    Join Date
    Oct 2011
    Posts
    52


    Did you find this post helpful? Yes | No

    Default Re: ProblemwithPWMfrequency

    Hi HenrikOlsson

    (1) Thankx. I worked on the steps you told me and I manage to control my interrupt frequency which is (50*36=1800) for this frequency the time1 will be loaded with 64980 this value gives me (1667hz) which is the lowest frequency but the value 65080 gives me the frequency 1818. I think as you told me early the PBP tpyes take longer time compared to ASM types ,thankx for it and I deside to use the value which gives (1818hz).

    (2)But sorry you are stastment below is not clear to me.
    EDIT: Now make sure that you can setup and control the dutycycle of the PWM module in the main loop (still no interrupts). Just toggle the dutycycle between two values and make sure you can see that on the scope.

    Thankx

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