CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Fixed point math library

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
viki2000



Joined: 08 May 2013
Posts: 233

View user's profile Send private message

Fixed point math library
PostPosted: Sun Jun 04, 2017 3:51 pm     Reply with quote

The concept is here: https://en.wikipedia.org/wiki/Fixed-point_arithmetic
I would like to generate a sine using a fixed point math library as Microchip has in "libq.h" with _Q15sinPI on page 232 next file:
http://ww1.microchip.com/downloads/en/DeviceDoc/50001456J.pdf
http://ww1.microchip.com/downloads/en//softwarelibrary/fixed%20point%20math%20library/50001456j.pdf

The idea is to have the argument of the function and the returned value in the range -32768 to 32767, no (real) floating numbers.
The existing sin() function from "math.h" described in PCD manual page 190, has as argument of the function and the returned value floating numbers:
https://www.ccsinfo.com/downloads/PCDReferenceManual.pdf

The way how I do it now is implicit, typecasting, by assigning the float variable value to an signed int16 variable:

Code:
void main()
{
float x;
signed int16 y;

   while(TRUE)
   {
          for(x=0; x<2*PI; x+=PI/32768){
          y = 32767*sin(x);
          putc(make8(y,1)); //MSB
          putc(make8(y,0)); //LSB
       }
   }
}


My question: - is any other more efficient way to do it? Is it any other fixed point function/math library as Microchip has "libq.h" with _Q15sinPI ?
temtronic



Joined: 01 Jul 2010
Posts: 9253
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jun 04, 2017 4:38 pm     Reply with quote

You don't say which PIC you're using or the resolution you need but I'd think a lookup table for 0-90* would/could be very fast and efficient.
It would depend upon what you need the numbers for....
Decades ago there was an article about 'magic sine waves' by Don Lancaster.

Jay
viki2000



Joined: 08 May 2013
Posts: 233

View user's profile Send private message

PostPosted: Sun Jun 04, 2017 4:59 pm     Reply with quote

I use PIC24HJ64GP202 or generally a PIC24 on 16 bit MCU.
I want to generate the sine wave using 1000 up to 10.000 points.
At 10K points the lookup table is not a nice option, actually even for 1000 points is big., so I decided to try a trigonometric function as sin().
I use 16bit for one DAC sample as resolution.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group