Continue to Site

# sine and cosine functions with microchip

Status
Not open for further replies.

#### Steve311

##### Member
Hello all, been a while since I’ve programmed but looking for insight on sine and cosine functions when using a pic? I’m familiar with the pic16F6xx series but that’s about it. Easiest and most accurate approach ? TIA

By storing one 45 degree table you can get all 360 degree values. Is 8 bit enough resolution?

Mike.

y=0;
m=1/256 or other fraction which makes a small number.

do forever
{ x=x - y*m;
y=y + x*m;
}

will trace out an almost perfect circle.

Can be done with only adds and shifts if the denominator of the small fraction is a power of two...

From the Cordic algorithm.

I first did this in Assembler on a PDP-8...

y=0;
m=1/256 or other fraction which makes a small number.

do forever
{ x=x - y*m;
y=y + x*m;
}

will trace out an almost perfect circle.

Can be done with only adds and shifts if the denominator of the small fraction is a power of two...

From the Cordic algorithm.
is that cordic boiled down? or something else?

edit: durrr, how did i miss the last sentence.

Last edited:
A good way to think about the cordic algorithm is it's like those nail and string patterns that form curves.

Mike.

I use a small routine that has 15 table entries and draws tiny lines between each 3° I use it in virtually all my display's it is for 90° but I use it for any angle..
C:
long cosine(int ang)
{
int x,y;
long tmp,count=0;
if(ang > 900) ang = 900 -(ang - 900);
for(x=0;x<30;x++)
{
y += 2;
if(ang == 30)
{
count += tmp;
count = 4000 - count;
return count;
}
if(ang < 30)
{
count += (tmp/30)* ang;
count = 4000 - count;
return count;
}
count += tmp;
ang -= 30;
}
return 0;
}

The lookup table is percentages of 4000 each 3° is determined and added to produce the fraction of 4000... Because I use fixed math I use this to work out radius from angle The angle is 0 ~ 900 and the length is multiplied by the answer and divided by 4000 !

Works for me..

Status
Not open for further replies.