Cordic trig assembly code for PIC18f


Closed Thread
Results 1 to 40 of 55

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by scalerobotics View Post
    For instance
    Code:
    x = 25980
    y = 15000
    .....and that is 30 degrees
    My perspective was all wrong in the above post. I was supposed to be lying down and facing East, that is the radian way.

    to convert to degrees ......

    Imagine a circle with degree headings, but instead of starting at the top, and going clockwise, as the numbers get higher, we are going to do something completely different. Starting at 90 degrees, we are going to go counterclockwise while our radians rise. From zero to 32,767 is the arc from 90 degrees (through 0) to 270 degrees. To complete the circle, our arc continues from 32,768 to 65,535 as it travels from 270 degrees to 90 degrees in a counter clockwise rotation.

    So, as you can see there is just a little bit of manipulation to get the result in angle, but it really isn't too bad.

    Thanks Joe.
    Last edited by ScaleRobotics; - 13th February 2009 at 08:52.

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


    Did you find this post helpful? Yes | No

    Default

    For anyone else like me that forgot all they learned about radians.... This should help. These go from 0 to 65,535.
    Attached Images Attached Images  
    Last edited by ScaleRobotics; - 5th February 2011 at 17:05.

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Thank You Walter,
    It is definitely going to take some time to get my head around this, unfortunate side effect of growing up during the Vietnam war, where we were expected to become soldiers who got shipped there and returned in a nicely bagged and boxed container. My algebra teacher was a retired navel captain, who taught spit about the subject but spoke extensively about his career and travels in Tahiti. That notwithstanding I will get this. I am sure nobody who participates in this forum is going to point at me in the store and say "hey there is the dummy who couldn't understand that code Walter wrote . . . " <b>So Anyway</b> the numbers you plugged in represent . . .Units, of my choosing . . .and to use this
    Code:
    include"trig.inc"
    main:
    ang=5461
    x=0
    y=0
    
    asm
        call sin_cos
    endasm
        
    Lcdout $fe, 1   ' Clear LCD screen
    
    lcdout $FE,1,#x,",",#y
    lcdout $FE,$C0,#ang
    Correct? Figure I do not need to know how to build a truck to drive one.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

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


    Did you find this post helpful? Yes | No

    Default

    Hey Joe,

    No you definitely do not need to know how it works. But if you find out, please let me know. The assembly code only seems to put me to sleep when I read it. Something about rotating angle, shifting bits zzzzz.....

    Your code will work, but it can be even simpler since it has the includes file in it. You don't have to get in to the asm endasm, as the sincos function is already there.

    Code:
    include"trig.inc"
    main:
    ang=5461
    x=0
    y=0
    
    call sincos
        
    Lcdout $fe, 1   ' Clear LCD screen
    
    lcdout $FE,1,#x,",",#y
    lcdout $FE,$C0,#ang
    It is that easy. Only a little bit harder if you want to do anything with the data returned in x and y (or ang and x for atan function)

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by scalerobotics View Post
    You don't have to get in to the asm endasm, as the sincos function is already there.
    Funny, that was a copy paste, I don't know where the asm / endasm went, glad to know it can be omitted ( call sincos ). Just 1 (I hope) more question: Where does the number ang=5461 come from ? I apologize if I seem tedious, I really haven't a clue. Thanks.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

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


    Did you find this post helpful? Yes | No

    Default

    That is the hardest part. Darn radians are 90 degrees off from the start.

    For example

    0 = 90 degrees
    5461 = 60 degrees
    10922 = 30 degrees
    16383 = 0 degrees (or North)
    21844 = 330 degrees
    32767 = 270 degrees
    38228 = 240 degrees

    and so on.....

    You definitely do not have to know this, but I think it is interesting:
    In the time it takes for a regular Sin, or Cos 16 bit function on a PIC18 to give a result, light travels 3000 kilometers
    The time it takes for this cordic 16 bit result, light travels 33.3 kilometers.
    How do you say "Your mileage may vary" when you use kilometers?
    Last edited by ScaleRobotics; - 13th February 2009 at 19:20.

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by scalerobotics View Post
    That is the hardest part. Darn radians are 90 degrees off from the start.

    For example

    0 = 90 degrees
    5461 = 60 degrees
    10922 = 30 degrees
    16383 = 0 degrees (or North)
    21844 = 330 degrees
    32767 = 270 degrees
    38228 = 240 degrees

    and so on.....

    You definitely do not have to know this, but I think it is interesting:
    In the time it takes for a regular Sin, or Cos 16 bit function on a PIC18 to give a result, light travels 3000 kilometers
    The time it takes for this cordic 16 bit result, light travels 33.3 kilometers.
    How do you say "Your mileage may vary" when you use kilometers?
    Moving counter clockwise from the 3 o'clock position . . .I see for every 30 degrees the numbers change 5461 or 182.03333333333333333333333333333 per degree or 65532 for a full circle which is a number that fits nicely in a word variable.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  2. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  3. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  4. 4 Chanel Dmx512 ready assembly code to PBP ?
    By syscoder in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 21st March 2007, 23:55
  5. Your Suggestions: Assembly code material?
    By dw_picbasic in forum General
    Replies: 1
    Last Post: - 2nd February 2007, 17:33

Members who have read this thread : 2

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