PMDC SERVO MOTOR WITH quadrature encoder DRIVE ?


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    Join Date
    May 2007
    Location
    Republic Serbia
    Posts
    105


    Did you find this post helpful? Yes | No

    Default

    Here is code which can measure and put to Y variable defined as LONG position of mechanic on machine to 32bit and count up and down.
    I am not good programmer but code work good.
    Code:
    ' Pic 18F4431 @ 20MHz true XTAL
        define osc 20
        DEFINE HSER_RCSTA 90h              ' Enable serial port & continuous receive
        DEFINE HSER_TXSTA 24h              ' Enable transmit, BRGH = 1
        DEFINE HSER_SPBRG 64               ' 19200 Baud @ 20MHz, 0.16%
        DEFINE HSER_CLROERR 1              ' Clear overflow automatically
        ADCON0=0                           'Turn of ADC
        ANSEL0=0                           'Make Port A inputs Digital
    
        PortA = 0
        PortB = 0
        PortC = 0
    
        TRISA = %011100                    'QEI pins as inputs.
        TRISB = %00000000
        TRISC = %10000000                  'USART RX pin as input.
    
        QEICON=%10011000
        DFLTCON = %00111000                ' enable error filter for all capture inputs
        MAXCNTL = %11010100                'low set for maxcnt of 12500 becouse have gear 6.25:1
        MAXCNTH = %00110000                'hig set for maxcnt of 12500 becouse have gear 6.25:1
        'MAXCNTL = %00000001               'lowbyte = 207
        'MAXCNTH = %00000000               'highbyte = 7 but after math: highbyte * 256 = 1792 + 207 = 1999
        'QEICON = %00010100                'Setup QEI, 4X mode, reset on index. 
        PosLow VAR BYTE                    'Storage for the low byte of encoder count
        PosHigh VAR BYTE                   'Storage for the high byte of encoder count
        PosTemp VAR BYTE                   'Temporary storage for low byte of encder count
        Position VAR WORD                  'Actual encoder position, 16bit word.
        y var long                         'Total pulse count storage variable 32bit
        x var byte                         'Number of full shaft rotation of 360'
        z var BIT                          'Actual bit if any move of shaft to any direction
        h var BIT                          'direction bit 0 for left , 1 for right turn of encoder
        z = 0                                 
        x = 0 
        y = 0
        h = 0
        POSCNTL = 0                        ' clear lowbyte of counter for start
        POSCNTH = 0                        ' clear highbyte of counter for start 
        PIR3.2 = 0 
        CLEAR
            
        Start:
        Gosub GetPosition
        y =  (12500 * x) + position                    ' increment  total counter
        if z = 1 and h = 0 then y = y-(12500-position) ' dectement  total counter
        HSEROUT [DEC y]                                 ' debug via rs232 @ 19200
        Pause 100                                       'Wait....  
        Goto Start                                     '...and do it again.
    
    
    GetPosition:
        z = 0
        PosHigh = POSCNTH                           'Get high byte of counter
        PosLow = POSCNTL                            'Get low byte of counter
        PosTemp = POSCNTL                           'Get low byte again  
        If PosLow - PosTemp = 0 then Goto Done      'Compare, if equal we're done.
        PosHigh = POSCNTH                           'If not, get values again.
        PosLow = POSCNTL
        z = 1 
    Done: 
        Position = POSHIGH * 256 + PosLow           'Put high and lowbyte together. 
        h = QEICON.5                                'QEICON.5 for right = 1 for left = 0 
        if PIR3.2 = 1 and h =1 then x = x+ 1
        if x <> 0 and PIR3.2 = 1 and h =0 then x = x- 1
        PIR3.2 = 0                                  'PIR3.2 maxcount transition down or up was detected
        h = 0
        RETURN  
        END
    Last edited by phoenix_1; - 12th September 2009 at 13:02.

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by phoenix_1 View Post
    Here is code which can measure and put to Y variable defined as LONG position of mechanic on machine to 32bit and count up and down.
    I am not good programmer but code work good.
    What is a good programmer???
    If it works as you want then it is good.
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Wink

    Hi, Dave

    For HSPLL Osc ...

    I just run a 12 Mhz XTAL on a 18F4420, PLL Engaged ... everything fine.

    So, May be the XTAL ... or may be your board layout ( 40 Mhz is HF !!! ) or ... poor quality capacitors.

    Alain
    ************************************************** ***********************
    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 " !!!
    *****************************************

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi, Dave

    For HSPLL Osc ...

    I just run a 12 Mhz XTAL on a 18F4420, PLL Engaged ... everything fine.

    So, May be the XTAL ... or may be your board layout ( 40 Mhz is HF !!! ) or ... poor quality capacitors.

    Alain
    I am using a resonator so maybe that is off a bit on the specs.

    All this is on a bread board so maybe the problem is there but I have 4550s running at 48Mhz on same type of boards. Using the PicKit2 for the power supply. Maybe more caps across VSS/VDD is needed.

    I got busy with something else so I have not had time to play with it lately. Still sitting on the bench... waiting...
    Dave
    Always wear safety glasses while programming.

Similar Threads

  1. Quadrature encoder and ASM Interrupts. questions..
    By godfodder in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th March 2013, 14:45
  2. Best quadrature encoder for buck?
    By jrprogrammer in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd June 2009, 13:58
  3. More Servo Woes
    By chrisshortys in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 13th May 2009, 08:40
  4. saving RCREG to word
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 15th September 2008, 13:51
  5. encoder wowes
    By wallaby in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 6th December 2005, 21:56

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