Help please with Assembly (PBPro user)


Results 1 to 10 of 10

Threaded View

  1. #8
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Thumbs up Tilt trig atan tan-1 arctan atan2

    Here is some code to determine tilt using at least a two axis accelerometer. It uses trig.inc
    This does not do the a/d conversions, or pulse measurement conversions, that would have to be added.

    Code:
    define OSC 20
    'code working with PIC-MT-USB
    include "TRIG.inc"
    
    main:
    define       LED     PortD.3
    
    DEFINE LCD_DREG PORTD             ; Set LCD Data port                B C   D
    DEFINE LCD_DBIT 4                 ; Set starting Data bit (0 or 4)   4 0
    DEFINE LCD_RSREG PORTD            ; Set LCD Register Select port     A A
    DEFINE LCD_RSBIT 0                ; Set LCD Register Select bit      3 2
    DEFINE LCD_EREG PORTD             ; Set LCD Enable port              A A
    DEFINE LCD_EBIT 2                 ; Set LCD Enable bit               1 5
    DEFINE LCD_BITS 4                 ; Set LCD bus size (4 or 8 bits)   4 4
    DEFINE LCD_LINES 2                ; Set number of lines on LCD       2 2
    define LCD_RWREG PORTD
    define LCD_RWBIT 1
    ADCON1=15
    
    TRISA=%00000011                         ' Set PORTA  0-1-3 used for AD
    TRISB=%00000000                         ' Set PortB
    TRISC=%00000000  
    TRISD=%00000100  
    '       Vdd     5 volts
    '       Vss     Ground
    '       Vo      20K potentiometer (or ground)
    '       DB0-3   No connect
    
    a   var word         
    xresult var word      'result of accelerometer sensor in x axis
    yresult var word      'result of accelerometer sensor in y axis
    zresult var word      'result of accelerometer sensor in z axis
    correct var bit        'able to keep equation the same, and flip angle
    xdir    var bit          'shows direction of x tilt
    ydir    var bit          'shows direction of y tilt
    zdir    var bit          'shows direction of z tilt
    
    
    Lcdout $fe, 1   ' Clear LCD screen
    Pause 500       ' Wait for LCD to startup
    toggle portD.3
                                         
    xresult = 553                       'is 41 away from center
    zresult = 297                       'is 215 away from center
    'to try it on your calculator tan^-1(41/215) = 10.796543 degrees
                                        'xresult is acceleration allong the x axis
    if xresult > 512 then               'get difference from neutral to gravity reading
        xresult = xresult - 512         'since we are using 10 bit result, half is 512
        xdir = 0
    else                                'accelerometer reads 512 with no gravity
        xresult = 512 - xresult         'and above or below depending on direction tilt
        xdir = 1
    endif
    
    if yresult > 512 then               'yresult is acceleration allong the y axis
        yresult = yresult - 512
        ydir = 0
    else
        yresult = 512 - yresult
        ydir = 1
    endif
    
    if zresult > 512 then               'zresult is acceleration allong the z axis
        zresult = zresult - 512
        zdir = 0
    else
        zresult = 512 - zresult
        zdir = 1
    endif
    
    if xresult < zresult then           'if x/z is < 1 then x/z
        xresult = xresult * 30000       'we will have to correct angle if this is case
        y = div32 zresult
        correct = 0
    else
        zresult = zresult * 30000       'else z/x to keep our equation within limits
        y = div32 xresult               
        correct = 1
    endif
        x = 30000                       'multiply this by same value 30000 as above
    call atan2
    if correct = 0 then y = 9000 - y    '90.00 degrees - y = corrected angle
    
    lcdout $FE,1,#y/100,".",#y//100," degrees"            'y = angle 1080 = 10.80 degrees
    lcdout $FE,$C0,"x=",#xdir," y=",#ydir," z=",#zdir
    end
    here is the location of the trig.inc file

    http://www.picbasic.co.uk/forum/atta...4&d=1234739854
    Last edited by ScaleRobotics; - 26th May 2010 at 16:57.

Similar Threads

  1. Adding PBPro code to an ASM file - how to?
    By wjsmarine in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 9th January 2010, 19:49
  2. Picbasic Pro Demo assembly errors
    By mikeRho in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 2nd January 2008, 07:41
  3. Assembly Guru : Question
    By Archangel in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 20th May 2007, 04:51
  4. from assembly to basic
    By shantanu in forum Off Topic
    Replies: 4
    Last Post: - 1st February 2007, 15:50
  5. How do you do this in Assembly
    By ice in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th May 2005, 07:47

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