Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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;
}
}
 
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!!!
 
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.....
 
yes i will do that..
but how to make look up table for sin wave 180 degree.....

Remember the math library I gave you in another post!! This has a sine wave table ( actually cosine, but its the same ).
 
const int cosTable[] = {0x0037, 0x00A1, 0x0114, 0x017C, 0x01E8,
0x0253, 0x02BB, 0x0321, 0x0386, 0x03E7,
0x0446, 0x04A3, 0x04FA, 0x0550, 0x05A2,
0x05EF, 0x0638, 0x067E, 0x06BD, 0x06FA,
0x0730, 0x0763, 0x078E, 0x07B6, 0x07D8,
0x07F5, 0x080B, 0x081C, 0x0828};
const int tanTable[] = {0x0418, 0x041E, 0x042A, 0x043C, 0x0452,
0x0474, 0x049C, 0x04CA, 0x0506, 0x054E,
0x059E, 0x0604, 0x0682, 0x0710, 0x07C6};

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.

Latest threads

New Articles From Microcontroller Tips

Back
Top