Erroneous mathematical processing?


Results 1 to 11 of 11

Threaded View

  1. #4
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    169


    Did you find this post helpful? Yes | No

    Default Re: Erroneous mathematical processing?

    Okay here is the code for the PIC18F26K22:

    Code:
    include "trig18.inc"
    
    
    Define OSC 16 'Clock will be configured to 16MHz
    OSCCON.6 = 1    ' Set Internal Osc to 16Mhz
    OSCCON.5 = 1    ' Set Internal Osc to 16Mhz
    OSCCON.4 = 1    ' Set Internal Osc to 16Mhz
    OSCCON.1 = 0    ' Use Internal Osc through Primary Clock (not Directly) to Access PLL Block
    OSCCON.0 = 0    ' Use Internal Osc through Primary Clock (not Directly) to Access PLL Block
    OSCTUNE.6 = 0   '16MHz osc - PLL Disabled
    
    'Configure AN2 as Analog Inputs (PortA2)
    ANSELA = %00000000 'Everything Digital
    ANSELB = %00000000 'Everything Else Digital
    ANSELC = %00000000 'Everything Else Digital
    
    TRISB.7 = 0 'Outout
    Serial_out  var PORTB.7   'Serial out pin for debugging/developing comms
    n   var byte
    
    main:
        pause 2000    
        serout2 Serial_out,84, ["Test 123",13,10]    
        Pause 1000
    
        ang=3000
        serout2 Serial_out,84, ["Ang: ",dec ang,13,10]
        'sincos example:
        'input ang in degrees.dd example: ang = 3000 (= 30.00 degrees)
        'call sincos
        'result: x = 15004 , Y = 25981
        'so, 15004/30000 = sin(ang) = 0.5001 and 25981/30000 = cos(ang) = 0.8660
        call sincos
        serout2 Serial_out,84, ["Sin ang: ",dec x,13,10]
        serout2 Serial_out,84, ["Cos ang: ",dec y,13,10]
        serout2 Serial_out,84, [13,10]
        
        x = 12000
        y = 8000
        serout2 Serial_out,84, ["x input: ",sdec x,13,10]
        serout2 Serial_out,84, ["y input: ",sdec y,13,10]
        
        call atan2
        'result: x = 14422 = hypotenuse or distance to x,y, y = angle to x,y in degrees.dd = 5631 (=56.31 degrees), ang = 6133 = radians 0 to 65535 
        serout2 Serial_out,84, ["hypotenuse: ",sdec x,13,10]
        serout2 Serial_out,84, ["Ang in degrees.dd: ",sdec y/100,".",dec2 y,13,10]
        serout2 Serial_out,84, ["Ang in Rad: ",dec ang,13,10]
        serout2 Serial_out,84, [13,10]
        
        
        x = -12000
        y = 8000
        serout2 Serial_out,84, ["x input: ",sdec x,13,10]
        serout2 Serial_out,84, ["y input: ",sdec y,13,10]
        
        call atan2
        'result: x = 14422 = hypotenuse or distance to x,y, y = angle to x,y in degrees.dd = 3037 (=303.70 degrees), ang = 26634 = radians 0 to 65535 
        serout2 Serial_out,84, ["hypotenuse: ",sdec x,13,10]
        serout2 Serial_out,84, ["Ang in degrees.dd: ",sdec y/100,".",dec2 y,13,10]
        serout2 Serial_out,84, ["Ang in Rad: ",dec ang,13,10]
        serout2 Serial_out,84, [13,10]
        
        x = -12000
        y = -8000
        serout2 Serial_out,84, ["x input: ",sdec x,13,10]
        serout2 Serial_out,84, ["y input: ",sdec y,13,10]
        
        call atan2
        'result: x = 14422 = hypotenuse or distance to x,y, y = angle to x,y in degrees.dd = 23632 (=236.32 degrees), ang = 38899 = radians 0 to 65535 
        serout2 Serial_out,84, ["hypotenuse: ",sdec x,13,10]
        serout2 Serial_out,84, ["Ang in degrees.dd: ",sdec y/100,".",dec2 y,13,10]
        serout2 Serial_out,84, ["Ang in Rad: ",dec ang,13,10]
        serout2 Serial_out,84, [13,10]
        
        x = 12000
        y = -8000
        serout2 Serial_out,84, ["x input: ",sdec x,13,10]
        serout2 Serial_out,84, ["y input: ",sdec y,13,10]
        
        call atan2
        'result: x = 14422 = hypotenuse or distance to x,y, y = angle to x,y in degrees.dd = 12367 (=123.67 degrees), ang = 59405 = radians 0 to 65535 
        serout2 Serial_out,84, ["hypotenuse: ",sdec x,13,10]
        serout2 Serial_out,84, ["Ang in degrees.dd: ",sdec y/100,".",dec2 y,13,10]
        serout2 Serial_out,84, ["Ang in Rad: ",dec ang,13,10]
    
    
    Goto main
    Results of the 18F26K22 code running on a 18F26K22:

    Name:  K22_Output.png
Views: 2371
Size:  13.8 KB


    Here is the (COPY & PASTED) code for the PIC18F26K83 - note the only difference is the osc setting method:

    Code:
    include "trig18.inc"
    
    
    'Configure OSC postscaler to provide 16mhz system clock
    OSCFRQ = %0101
    
    Define OSC 16
    
    'Configure AN2 as Analog Inputs (PortA2)
    ANSELA = %00000000 'Everything Digital
    ANSELB = %00000000 'Everything Else Digital
    ANSELC = %00000000 'Everything Else Digital
    
    TRISB.7 = 0 'Outout
    Serial_out  var PORTB.7   'Serial out pin for debugging/developing comms
    n   var byte
    
    main:
        pause 2000    
        serout2 Serial_out,84, ["Test 123",13,10]    
        Pause 1000
    
        ang=3000
        serout2 Serial_out,84, ["Ang: ",dec ang,13,10]
        'sincos example:
        'input ang in degrees.dd example: ang = 3000 (= 30.00 degrees)
        'call sincos
        'result: x = 15004 , Y = 25981
        'so, 15004/30000 = sin(ang) = 0.5001 and 25981/30000 = cos(ang) = 0.8660
        call sincos
        serout2 Serial_out,84, ["Sin ang: ",dec x,13,10]
        serout2 Serial_out,84, ["Cos ang: ",dec y,13,10]
        serout2 Serial_out,84, [13,10]
        
        x = 12000
        y = 8000
        serout2 Serial_out,84, ["x input: ",sdec x,13,10]
        serout2 Serial_out,84, ["y input: ",sdec y,13,10]
        
        call atan2
        'result: x = 14422 = hypotenuse or distance to x,y, y = angle to x,y in degrees.dd = 5631 (=56.31 degrees), ang = 6133 = radians 0 to 65535 
        serout2 Serial_out,84, ["hypotenuse: ",sdec x,13,10]
        serout2 Serial_out,84, ["Ang in degrees.dd: ",sdec y/100,".",dec2 y,13,10]
        serout2 Serial_out,84, ["Ang in Rad: ",dec ang,13,10]
        serout2 Serial_out,84, [13,10]
        
        
        x = -12000
        y = 8000
        serout2 Serial_out,84, ["x input: ",sdec x,13,10]
        serout2 Serial_out,84, ["y input: ",sdec y,13,10]
        
        call atan2
        'result: x = 14422 = hypotenuse or distance to x,y, y = angle to x,y in degrees.dd = 3037 (=303.70 degrees), ang = 26634 = radians 0 to 65535 
        serout2 Serial_out,84, ["hypotenuse: ",sdec x,13,10]
        serout2 Serial_out,84, ["Ang in degrees.dd: ",sdec y/100,".",dec2 y,13,10]
        serout2 Serial_out,84, ["Ang in Rad: ",dec ang,13,10]
        serout2 Serial_out,84, [13,10]
        
        x = -12000
        y = -8000
        serout2 Serial_out,84, ["x input: ",sdec x,13,10]
        serout2 Serial_out,84, ["y input: ",sdec y,13,10]
        
        call atan2
        'result: x = 14422 = hypotenuse or distance to x,y, y = angle to x,y in degrees.dd = 23632 (=236.32 degrees), ang = 38899 = radians 0 to 65535 
        serout2 Serial_out,84, ["hypotenuse: ",sdec x,13,10]
        serout2 Serial_out,84, ["Ang in degrees.dd: ",sdec y/100,".",dec2 y,13,10]
        serout2 Serial_out,84, ["Ang in Rad: ",dec ang,13,10]
        serout2 Serial_out,84, [13,10]
        
        x = 12000
        y = -8000
        serout2 Serial_out,84, ["x input: ",sdec x,13,10]
        serout2 Serial_out,84, ["y input: ",sdec y,13,10]
        
        call atan2
        'result: x = 14422 = hypotenuse or distance to x,y, y = angle to x,y in degrees.dd = 12367 (=123.67 degrees), ang = 59405 = radians 0 to 65535 
        serout2 Serial_out,84, ["hypotenuse: ",sdec x,13,10]
        serout2 Serial_out,84, ["Ang in degrees.dd: ",sdec y/100,".",dec2 y,13,10]
        serout2 Serial_out,84, ["Ang in Rad: ",dec ang,13,10]
    
    
    Goto main
    (Erroneous) Results of the 18F26K83 code running on a 18F26K83:

    Name:  K83_Output.png
Views: 2006
Size:  13.0 KB

    I've also attached my slightly modifed Trig18.inc file - only modified to allow the use of longs for PIC18s. Same inc file is used for both examples.
    Attached Files Attached Files

Similar Threads

  1. Replies: 4
    Last Post: - 19th October 2016, 12:02
  2. Mathematical calculation
    By amindzo in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 27th October 2013, 00:07
  3. how to get the mathematical function sin
    By minssss in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 18th July 2010, 17:13
  4. Mathematical problem
    By iugmoh in forum General
    Replies: 2
    Last Post: - 25th January 2008, 18:24
  5. Mathematical Precedence
    By keithdoxey in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th October 2006, 21:44

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