Interupt driven servo PWM output


Closed Thread
Results 1 to 10 of 10

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Interupt driven servo PWM output

    ok, here is the full code... i didn't think the top bit was necessary...

    @ __config _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_ON_1H & _PRICLKEN_ON_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    @ __config _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_220_2L
    @ __config _CONFIG2H, _WDTEN_OFF_2H & _WDTPS_32768_2H & _CCP2MX_PORTC1_3H & _PBADEN_OFF_3H
    @ __config _CONFIG3H, _CCP3MX_PORTE0_3H & _HFOFST_OFF_3H & _T3CMX_PORTC0_3H & _P2BMX_PORTC0_3H & _MCLRE_INTMCLR_3H
    @ __config _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    @ __config _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
    @ __config _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
    @ __config _CONFIG6L, _WRT0_OFF_6L
    @ __config _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
    @ __config _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
    @ __config _CONFIG7H, _EBTRB_OFF_7H

    INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas" ; Include if using PBP interrupts

    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler TMR0_INT, _SERVO_HANDLER, PBP, yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM

    Define OSC 64 ' Set clock speed

    OSCCON.7 = 0
    OSCCON.6 = 1
    OSCCON.5 = 1
    OSCCON.4 = 1
    OSCCON.1 = 0
    OSCCON.0 = 0
    OSCTUNE.6 = 1

    ANSELA = %00000000
    ANSELB = %00000000
    ANSELC = %00000000
    ANSELD = %00000000
    ANSELE = %00000000

    TRISB = 0
    PORTB = 0
    TRISC = 0
    PORTC = 0
    TRISD = 0
    PORTD = 0
    TRISE = 0
    PORTE = 0
    TRISA = 0
    ALOOP VAR WORD

    SERVO1 VAR LATD.0
    SERVO1POS VAR BYTE
    PRELOAD1 VAR WORD
    PRELOAD2 VAR WORD
    PRELOAD3 VAR WORD
    SERVOCMDMODE VAR BYTE
    SERVOPWMCOUNTER VAR WORD

    PRELOAD1 = 63556 ' 990us '63556
    TMR0_CON_PL1 CON %10000010 ' 1:8
    PRELOAD2 = 65528 ' 4us '65528
    TMR0_CON_PL2 CON %10000010 ' 1:8
    PRELOAD3 = 29556 ' 17990us '29556
    TMR0_CON_PL3 CON %10000010 ' 1:8

    @ INT_ENABLE TMR0_INT
    TMR0L = PRELOAD3.LOWBYTE
    TMR0H = PRELOAD3.HIGHBYTE
    T0CON = TMR0_CON_PL3
    SERVOCMDMODE = 1

    MAIN:
    SERVO1POS = 0
    PAUSE 5000
    SERVO1POS = 128
    PAUSE 5000
    SERVO1POS = 255
    PAUSE 5000
    GOTO MAIN

    SERVO_HANDLER:
    IF SERVOCMDMODE = 2 Then
    IF SERVOPWMCOUNTER < 256 THEN
    T0CON = TMR0_CON_PL2
    TMR0L = PRELOAD2.LOWBYTE
    TMR0H = PRELOAD2.HIGHBYTE
    IF SERVO1POS < SERVOPWMCOUNTER THEN
    SERVO1 = 0
    LATD.1 = 0
    LATD.2 = 0
    LATD.3 = 0
    ELSE
    SERVO1 = 1
    LATD.1 = 0
    LATD.2 = 1
    LATD.3 = 0
    ENDIF
    SERVOPWMCOUNTER = SERVOPWMCOUNTER + 1
    ELSE
    T0CON = TMR0_CON_PL3
    TMR0L = PRELOAD3.LOWBYTE
    TMR0H = PRELOAD3.HIGHBYTE
    SERVO1 = 0
    LATD.1 = 0
    LATD.2 = 0
    LATD.3 = 1
    SERVOCMDMODE = 1
    ENDIF
    @ INT_RETURN
    ENDIF

    IF SERVOCMDMODE = 1 Then
    T0CON = TMR0_CON_PL1
    TMR0L = PRELOAD1.LOWBYTE
    TMR0H = PRELOAD1.HIGHBYTE
    SERVOCMDMODE = 2
    SERVO1 = 1
    LATD.1 = 1
    LATD.2 = 0
    LATD.3 = 0
    SERVOPWMCOUNTER = 1
    @ INT_RETURN
    ENDIF

    @ INT_RETURN

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


    Did you find this post helpful? Yes | No

    Default Re: Interupt driven servo PWM output

    Hi,
    OK, so you're running at 64MHz, that gives it bit more time but I still think that interrupting at 4us intervals is way to fast, there's only 64 instruction cycles between each interrupt and I think it takes more than that for the DT-Ints system just to save and later restore the system variables. And, even if it does work the code would spend most of its time in transit to and from the interrupt handler.

    /Henrik.

  3. #3
    Join Date
    Aug 2013
    Posts
    2


    Did you find this post helpful? Yes | No

    Post Re: Interupt driven servo PWM output

    The time between transit and interrupt handler is the main culprit here in your servo pulse width modulation. Sometimes the turn around time makes us wait more and it doesn't work in the end even though everything seems right. Happens with me all the time. Connected a lipo battery once as a power supply and it never worked because of misconfiguration.
    Good luck to you.

  4. #4
    Join Date
    Aug 2013
    Posts
    2


    Did you find this post helpful? Yes | No

    Default Re: Interupt driven servo PWM output

    Quote Originally Posted by LettyLocke View Post
    The time between transit and interrupt handler is the main culprit here in your servo pulse width modulation. Sometimes the turn around time makes us wait more and it doesn't work in the end even though everything seems right. Happens with me all the time. Connected a lipo battery once as a power supply and it never worked because of misconfiguration.
    Good luck to you.
    No more updates regrading the issue? I think the interval time of interrupts must be increased in order for it to work. Interrupt handlers are very tricky to work with.

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default Re: Interupt driven servo PWM output

    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. Servo and pwm
    By Meriachee in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 29th May 2009, 14:26
  2. how to change servo motor PWM
    By dboy in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 21st September 2008, 07:02
  3. Help needed - MOSFET driven by PWM
    By bcd in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 2nd April 2008, 05:02
  4. 16F877a Interupt driven Hserin
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd November 2006, 00:38
  5. 16f73 pwm capture interupt
    By EDWARD in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 22nd July 2005, 22:22

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts