Sin table in PIC....

Status
Not open for further replies.

koolguy

Active Member
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:
#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;
}
}
 
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...
 
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.
 
Also, didnt the compile give you some warning like possible loss of precision?
 
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.

Point being!! He has variable a as an int!!!
 
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
 
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)
 
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)

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
 
Hahaha, don't worry about it Ian. I've had days like that

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).

[MODNOTE]Unhelpfull Post.[/MODNOTE]
 
Last edited by a moderator:
The code never reach the line "PORTB=a;" because of this:

while(1)
a = sin(20);
 
Why can't you just multiply by a scaling factor, converting to a uchar and passing that to the port?

yes i will do that..
but how to make look up table for sin wave 180 degree.....
 

Can you explain what r u doing and how to make discrete value?
 
Here is few methods and theory. I hope you at least try to read it.
**broken link removed**
 
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.
 
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.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…