# Sin table in PIC....

Discussion in 'Microcontrollers' started by koolguy, May 16, 2013.

1. ### koolguyActive Member

Joined:
Aug 24, 2010
Messages:
2,196
Likes:
9
HI,

I am testing the sin function on simulator here is my code it is running but don't showing any value in PORTB register why???

Code (text):

#include <htc.h>
#include <math.h>
#include <stdio.h>

__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000

void main (void){
TRISB=0x00;
{
int i, a;
while(1)
a = sin(20);
PORTB=a;
}
}

Joined:
Mar 28, 2011
Messages:
9,531
Likes:
937
Location:
Rochdale UK
Sometimes I wonder about you..... If sin(20) = 0.9294 radians or 0.34202 degrees... How the hell is an int going to display it...

3. ### kubeekWell-Known Member

Joined:
Mar 11, 2006
Messages:
1,546
Likes:
192
Location:
Prague, Czechia (not Chechnya)
sin(x) is not in radians nor degrees, it is just a number between -1.0 and 1.0
Anyway the math.h defines the function as double sin(double); so unless you do a sin of pi/4 you will allways get 0 on the output.

Joined:
Jan 12, 1997
Messages:
-
Likes:
0

5. ### kubeekWell-Known Member

Joined:
Mar 11, 2006
Messages:
1,546
Likes:
192
Location:
Prague, Czechia (not Chechnya)

Also, didnt the compile give you some warning like possible loss of precision?

Joined:
Mar 28, 2011
Messages:
9,531
Likes:
937
Location:
Rochdale UK
Point being!! He has variable a as an int!!!

• Like x 1

Joined:
Mar 28, 2011
Messages:
9,531
Likes:
937
Location:
Rochdale UK
Just checked it out..... sin() returns radians....

sin(20) returns.. 0.9131
sin(30) returns..-0.9880
sin(45) returns.. 0.8513
sin(60) returns..-0.3049

8. ### kubeekWell-Known Member

Joined:
Mar 11, 2006
Messages:
1,546
Likes:
192
Location:
Prague, Czechia (not Chechnya)
Sin() doesn´t return radians, it allways returns a fraction without any units, but it is true that the parameter is in radians.
(sinus of an angle is equal to a ratio of two sides in a triangle, so it can´t have any units)

Joined:
Mar 28, 2011
Messages:
9,531
Likes:
937
Location:
Rochdale UK
What am I on...... Don't know my head from my arse today.....Of course you are correct... Sorry for the argument Kubeek......

I need a lie down or something

10. ### DerStrom8Super ModeratorMost Helpful Member

Joined:
Aug 9, 2010
Messages:
5,640
Likes:
537
Location:
Vermont (GMT-5:00)

But yeah, the value that is put into sin() has units of radians or degrees. i.e. sin(35 degrees) or sin(pi/2 radians).

Last edited by a moderator: May 17, 2013
• Like x 3
11. ### misterTWell-Known MemberMost Helpful Member

Joined:
Apr 19, 2010
Messages:
2,697
Likes:
368
Location:
Finland
The code never reach the line "PORTB=a;" because of this:

while(1)
a = sin(20);

• Like x 1
12. ### koolguyActive Member

Joined:
Aug 24, 2010
Messages:
2,196
Likes:
9
Thanks that what i want to know.....

13. ### misterTWell-Known MemberMost Helpful Member

Joined:
Apr 19, 2010
Messages:
2,697
Likes:
368
Location:
Finland
That is not what you need to know.

• Like x 1
14. ### WTP PepperActive Member

Joined:
Jan 24, 2012
Messages:
648
Likes:
41
Location:
UK
Why can't you just multiply by a scaling factor, converting to a uchar and passing that to the port?

15. ### koolguyActive Member

Joined:
Aug 24, 2010
Messages:
2,196
Likes:
9
yes i will do that..
but how to make look up table for sin wave 180 degree.....

Joined:
Mar 28, 2011
Messages:
9,531
Likes:
937
Location:
Rochdale UK
Remember the math library I gave you in another post!! This has a sine wave table ( actually cosine, but its the same ).

17. ### koolguyActive Member

Joined:
Aug 24, 2010
Messages:
2,196
Likes:
9
Can you explain what r u doing and how to make discrete value?

Joined:
Mar 28, 2011
Messages:
9,531
Likes:
937
Location:
Rochdale UK
How fast is your sine wave???

Joined:
Apr 19, 2010
Messages:
2,697
Likes:
368
Location:
Finland
20. ### MrAlWell-Known MemberMost Helpful Member

Joined:
Sep 7, 2008
Messages:
11,070
Likes:
963
Location:
NJ
Hello,

If you already have the sin(a) function you can easily generate it using that. Just multiply by a constant to form the table value. The highest value is 1 and that is at 90 degree, and the lowest value is -1 and that is at 270 degrees (convert degrees to radians as needed) but we usually only need the values for 0 to 90 degrees which returns a float value 0 to 1. So if 0x0FFF is the highest value you need at 90 degrees, then multiply all the return values of sin(a) by 0x0FFF.

For example in a simplified language:

for k=0 to 19 do
a=sin(k/19*pi/4)
y=sin(a)
Y=y*0x0FFF
Table[k]=Y
end for

That creates a table that is entered into ram that covers 0 to 90 degrees, and that provides enough values to cover the full range of 0 to 360 degrees.

21. ### Brian GriffinMember

Joined:
May 23, 2013
Messages:
49
Likes:
8
Location:
Kuala Lumpur
A sine table in a microcontroller is straightforward.

Since microcontrollers don't read or store decimal numbers, these sine values must be scaled. Precalculate these sine wave values as a microcontroller will have a tough time working on it.

Then discretize the sine values for one period. Example, for 256-value sine table, sin(x/256*2pi*127) + 127 and rounded. So after getting these 256 values, dump them inside the array. The values should be 0 - 255 and plotting this in excel will get you a sine wave.

For a controllable synthesis of sine wave, it's advisable to read up on Digital Direct Synthesis (DDS). It is a simple algorithm to generate a sine wave on a microcontroller. However, some filtering is required on the output.

I made my own sine wave generators (it's for the toy music boxes) all using this method, without involving math.h or whatsoever.