This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

soft ware pwm

Hi! I have written the code for software PWM whose PWM frequency is 20KHz

Method:
1) Generate PWM1 and PWM2 of 20KHz frequency using PWM command
2) wait for TMR2 to PR2 match interrupt Flag
3) when this flag=1 then high PWM3 port pin and start timer1 whose max delay is 50usec (20KHz)

but for 50% Duty Cycle I have reduce Timer1 delay to 25Usec by setting its TMR1H and TMR1L variables and jump to Step '1'

When Timer1 OV interrupt generated then i have low PWM3 Port Pin so its frequency is equal to 20Khz but i have received 0.146V on PWM3 port pin instead of 2.5V and it is not working correctly , i have also attached the code if anybody find the mistake then it will be highly appreciated

--------------------------------------------------------------------------------

C:\\documents and setings\\mandeep\\my projects\\PWM.txt (7.56 GB)

Parents
  • Hi!.

    yes. the atachment was wrong. sorry.
    here is my code.

    
    duty_1          var byte
    duty_2          var byte
    duty_3          var byte
    duty_4          var byte
    duty_5          var byte
    
    mot1        VAR  ACC.2   'PWM1
    mot2        VAR  ACC.1     'PWM2
    mot3        var  ACC.0       'PWM3
    mot4        var  ACC.3         'PWM4
    mot5        var  ACC.4           'PWM5
    
    
    
    
        adcon1 = 7
        IEA = %00000000
        TR0 = %00000
    
    
        duty_1  = 255
        duty_2  = 255
        duty_3  = 255
        duty_4  = 255
        duty_5  = 255
        gosub drive0
    
    
    
    gogo:
            GOSUB   forward150
    
            GoTo gogo
    End
    
    drive:
        PWM 1,duty_1,800
        PWM 2,duty_2,800
        PWM 3,duty_3,800
        PWM 4,duty_4,800
        PWM 5,duty_5,800
        return
    
    drive0:
        PWM 1,0,800
        PWM 2,0,800
        PWM 3,0,800
        PWM 4,0,800
        PWM 5,0,800
        Return
    
    
    forward150:
    High  mot1
    High  mot2
    High  mot3
    High  mot4
    High  mot5
        duty_1  = 100
        duty_2  = 100
        duty_3  = 100
        duty_4  = 100
        duty_5  = 100
    GoSub drive
    pause 100
    Return
    
    from ex
    
    
    
    
    PWM1 AND PWM2 IS WORK
    
    BUT NO SIGNAL OUT PORT A.0  B.3  A.4
    
    
    

Reply
  • Hi!.

    yes. the atachment was wrong. sorry.
    here is my code.

    
    duty_1          var byte
    duty_2          var byte
    duty_3          var byte
    duty_4          var byte
    duty_5          var byte
    
    mot1        VAR  ACC.2   'PWM1
    mot2        VAR  ACC.1     'PWM2
    mot3        var  ACC.0       'PWM3
    mot4        var  ACC.3         'PWM4
    mot5        var  ACC.4           'PWM5
    
    
    
    
        adcon1 = 7
        IEA = %00000000
        TR0 = %00000
    
    
        duty_1  = 255
        duty_2  = 255
        duty_3  = 255
        duty_4  = 255
        duty_5  = 255
        gosub drive0
    
    
    
    gogo:
            GOSUB   forward150
    
            GoTo gogo
    End
    
    drive:
        PWM 1,duty_1,800
        PWM 2,duty_2,800
        PWM 3,duty_3,800
        PWM 4,duty_4,800
        PWM 5,duty_5,800
        return
    
    drive0:
        PWM 1,0,800
        PWM 2,0,800
        PWM 3,0,800
        PWM 4,0,800
        PWM 5,0,800
        Return
    
    
    forward150:
    High  mot1
    High  mot2
    High  mot3
    High  mot4
    High  mot5
        duty_1  = 100
        duty_2  = 100
        duty_3  = 100
        duty_4  = 100
        duty_5  = 100
    GoSub drive
    pause 100
    Return
    
    from ex
    
    
    
    
    PWM1 AND PWM2 IS WORK
    
    BUT NO SIGNAL OUT PORT A.0  B.3  A.4
    
    
    

Children
  • Anybody knows which chip the deep man is using?

  • Looks like some kind of BASIC...?

  • I don't have you COMPILER install in my computer so I can not compile your program. But I see one could be mistake in your program.

    In the data sheet it said:

    "The prescaler counter is cleared on writes to the TMR1H or TMR1L registers".

    It could be true because your duty cycle now is only 12.4uS instead of 49.6uS. Which mean the LED on time is 1/4 of what you have planned.

    So in your TMR1_ISR, after you write new value into timer1, set bits for prescale too.

    TMR1_ISR:
    LOW PWM3
    LOW led
    timer1=65473 '(50% DUTY CYCLE of 50US=25usec)

    T1CKPS0=1 ' set prescale bits for 1:4
    T1CKPS1=0 ' set prescale bits

    TMR1=0
    REGISTER RESTORE

    Also, in the data sheet said:
    "For writes, it is recommended that the user simply stop the timer and write the desired values. A write contention may occur by writing to the timer registers, while the register is incrementing. This may produce an unpredictable value in the timer register". I doubt it would help???

    In this line:

    SYMBOL FTMR1 = PIr1.0 ' TIMER1 OVERFLOW FLAG

    Is PIr ok with your compiler? I don't know if its case sensitive.

    Tom

  • The unknown compiler may or may not be - but the symbols for SI units certainly are!

    "your duty cycle now is only 12.4uS (sic) instead of 49.6uS (sic)"

    The symbol for seconds is a lowercase 's';
    An uppercase 'S' means siemens - the unit of conductance (reciprocal of resistance).