CCS trigonometric calculation

Status
Not open for further replies.

Noman Yousaf

Full Member level 4
hi
when i put the value 0x7543 in an "int16", in RAM that is shown as it is. But
when i put 0x7543 in float, it gives an other 32 bit value in RAM which is "866A8D"

what does it mean?
an i want to calculate trigonometry like
cos45*sin90... etc. (in degrees)
how can i do it ?
urgent help is needed

andre_luis

Super Moderator
Staff member
when i put 0x7543 in float, it gives an other 32 bit value in RAM which is "866A8D"

When programming in C you don't have to concern on the actual formatting of numbers stored in memory, just write the formula and get the result.

Noman Yousaf

Full Member level 4
ok sir. but actually my concern is to calculate trigonometric values and want the result in decimal. like
the value of cos45.

FvM

Super Moderator
Staff member
Please try to ask clear questions. CCS is ambiguous, can mean at least

Code composer studio (an IDE and C compiler for TI processors)
Custom Computer Services (CCS C PIC compiler)

You also don't say where you see "an other 32 bit value in RAM". Is it in MPLab debugger? How do you "put 0x7543 in float"? Show actual code.

C trigonometric functions are using radian units for angles. Can be easily converter to degree 180° = pi in radian.

andre_luis

Super Moderator
Staff member
You perhaps need to search for some int2bcd function, not forgetting to first cast the result to int, like that:

Code:
int2bcd ( (int)cos(x) ) ;

You only have to be aware of the fact that normally the trigonometric functions are performed by the compiler in radian, so that the ultimate formula would be:

Code:
int2bcd ( (int)cos(x*PI/180) ) ;

Noman Yousaf

Full Member level 4
Code:
///////////////////////////////////////////////////

#include <16f887.h>

#use		delay(clock=4000000)

#fuses	BROWNOUT, HS, NOWDT
#fuses	NODEBUG, NOLVP, PROTECT

float  reg1;

void cal (void)
{
reg1 = (3.14/180);
}

void main()
{
reg1 = 0x7543;

while(1)
{
cal();
}
}

BradtheRad

Super Moderator
Staff member
It is common for computer routines to do trig functions in radians, not degrees. If we start with degrees then multiply by Pi/180, to get radians.

Noman Yousaf

Full Member level 4
in above code, i may get the right result but how can i get the result in decimal in degrees?
means simply please tell any simple code to covert bcd to decimal.
(0xff to 255)

BradtheRad

Super Moderator
Staff member
how can i get the result in decimal in degrees?

To get this, you do trig functions such as arcsin, arccos, etc.

- - - Updated - - -

Then convert from radians to deg by multiplying by 180/Pi.

ads-ee

Super Moderator
Staff member
means simply please tell any simple code to covert bcd to decimal.
(0xff to 255)

0xff nor 255 are BCD (Binary Coded Decimal). The first 0xff is an 8-bit binary number displayed in HEX the second 255 is a decimal number.

A BCD value for 255 displayed in hex notation is 0x255, where each nibble is coded as a decimal number.

Noman Yousaf

Full Member level 4
ok
now next problem is how to convert bcd to decimal?
can anybody send code for conversion?
(0xffff to 65535)

- - - Updated - - -

or hex to decimal

andre_luis

Super Moderator
Staff member
Did you try the code recommended at the post #5 ?
I'm assuming you'll search for int2bcd() functions on the Web.

Noman Yousaf

Full Member level 4
yes i am looking for int2bcd() sir.
i tried the code you sent but i am not expert in C so can you please send complete code?

Status
Not open for further replies.