Problem in Tension/compression


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    89

    Default Problem in Tension/compression

    This is my code for length measurements for fatigue testing machine with PEAK-HOLD.
    I use 1000 PPR encoder with CH A and CH B for up/down, code working well when machine working in compression.
    but problem is when machine work in tension side, encoder count 65535 below 00

    i want this sequence

    <<---------- 00 --------->>
    Tension Compression
    6-5-4-3-2-1-0-1-2-3-4-5-6

    chip 16f88

    Code:
    DEFINE OSC 20
    INCLUDE "MODEDEFS.BAS"
    INCLUDE "MYLCD.BAS"
    
    ANSEL = 0 'SELECT ANALOG INPUTS 0 = NONE AND ALL DIGITAL
    ADCON0 = 0 'AD MODULE OFF & CONSUMES NO CURRENT
    CMCON = 7 'COMPARATORS OFF
    
    TRISA = %00000
    TRISB = %00000011  ' INT0 = ENCODER CH. A
                       ' PORTB.1 = ENCODER CH. B
    W0 VAR WORD
    B1 var word
    W0 = 0 
    B1 = 0
    
    PAUSE 500                           
    '-------------------------------------------------------------------------
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System
    INCLUDE "ReEnterPBP.bas"     ; Include if using PBP interrupts
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    INT_INT,  _ToggleLED1,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    OPTION_REG.6 = 0   ; 1 = Interrupt on rising edge of INT pin
                       ; 0 = Interrupt on falling edge of INT pin
                                          
    @   INT_ENABLE   INT_INT     ; enable external (INT) interrupts
    
    '-------------------------------------------------------------------------
    
    
    LOOP:
              if W0 > B1 then B1 = W0 ' (Peak-Hold), Hold the last value of W0 in B1
    
    
              LCDOUT $FE,1
              LCDOUT "Length = ", dec5 w0, " mm"
              lcdout $FE, $C0
              lcdout "Peak- Value ",dec5 B1, " mm"
              pause 500          
             GOTO LOOP
             
    ToggleLED1:                                    
            if PORTB.1 = 0 then
              W0 = W0+1
               else 
             w0 = w0-1 
             IF (W0 < 0) THEN
             W0 = 65535 + 1         
            endif 
            ENDIF
                
            @ INT_RETURN

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Code:
    W0 VAR WORD
    
             W0 = 65535 + 1
    You have a word variable (16 bits)...
    What's happens when you do the above math?
    %1111111111111111 + %0000000000000001 = %10000000000000000
    17 bits doesn't fit in a 16 bit variable.

    An idea...divide all your numbers in half and use 32767 ($7FFF) as your new 'ZERO' point, change the LCD out routine to show that.

  3. #3
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    89


    Did you find this post helpful? Yes | No

    Default

    When counts cross 65535, number will be 00000 and counts again upto 65535

    but when i code
    if W0 < 0 then W0 = 65535 + 1

    after this counter will 00000 and hang on 00001

    .

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by precision View Post
    When counts cross 65535, number will be 00000 and counts again upto 65535
    but when i code
    if W0 < 0 then W0 = 65535 + 1
    after this counter will 00000 and hang on 00001
    .
    That's my point...
    W0 cannot be less than 'ZERO' with PBP...
    PBP does not natively handle negative numbers using normal math routines. Yes there are a few commands that can handle signed integers, but not in the way you're looking to handle them. There are no 'minus' signs
    You must figure out your own method to handle those negative numbers.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    precision,

    Give this a try.
    Code:
    DEFINE OSC 20
    INCLUDE "MODEDEFS.BAS"
    INCLUDE "MYLCD.BAS"
    
    ANSEL = 0 'SELECT ANALOG INPUTS 0 = NONE AND ALL DIGITAL
    ADCON0 = 0 'AD MODULE OFF & CONSUMES NO CURRENT
    CMCON = 7 'COMPARATORS OFF
    
    TRISA = %00000
    TRISB = %00000011  ' INT0 = ENCODER CH. A
                       ' PORTB.1 = ENCODER CH. B
    W0     VAR WORD
    LastW0 VAR WORD
    B1     var word
    W0 = 0 
    B1 = 0
    
    PAUSE 500                           
    '-------------------------------------------------------------------------
    INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System
    INCLUDE "ReEnterPBP.bas"     ; Include if using PBP interrupts
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    INT_INT,  _ToggleLED1,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    OPTION_REG.6 = 0   ; 1 = Interrupt on rising edge of INT pin
                       ; 0 = Interrupt on falling edge of INT pin
                                          
    @   INT_ENABLE   INT_INT     ; enable external (INT) interrupts
    
    '-------------------------------------------------------------------------
    LCDOUT $FE,1
    
    LOOP:   
            IF W0 <> LastW0 then
                LastW0 = W0
                     ' (Peak-Hold), Hold the last value of W0 in B1
                if ABS(LastW0) > ABS(B1) then B1 = LastW0
    
                LCDOUT $FE,2
                LCDOUT "Length = ", Sdec LastW0, " mm    "
                lcdout $FE, $C0
                lcdout "Peak- Value ",Sdec B1, " mm    "
            ENDIF
    GOTO LOOP
             
    ToggleLED1:                                    
            if PORTB.1 = 0 then
                W0 = W0+1
            else 
                w0 = w0-1 
            ENDIF
    @ INT_RETURN
    The range will be +/-32767.

    Added: Modified slightly to eliminate changes while displaying.

    Modified again to simplify. ABS() works for both pos and neg numbers. Doh!
    It also fixes a problem if it moves slightly negative before going positive.
    I'll get it right one of these times
    <br>
    Last edited by Darrel Taylor; - 12th July 2007 at 03:24. Reason: Modified
    DT

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Just posting this for an email notification that I changed the program above.

    In case you're already trying it.
    <br>
    DT

Similar Threads

  1. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  2. Microcode Studio 18f2455 problem?????
    By volkan in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 21st May 2007, 21:04
  3. Hardware problem or what ?
    By Steve S. in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 4th March 2007, 21:39
  4. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59
  5. PORTA.PinNo = 1 ' problem
    By frank small in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 6th May 2004, 14:30

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