Sinewave using PSoC3/5

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello,

   

I have used the following code to generate a sinewave for freqiency 1 kHz.. But the frequency o/p that I'm getting is 35 Hz. Can anyone suggest me where I'm goin wrong? Also how do I use sine function with GCC compiler. If not supported, what's the other alternative ?

   

 #include <device.h>
#include <math.h>

   

#define PI 3.14159265

   

void main()
{
   float frequency = 10000.0;
   float sample_per_seconds = 50000.0;
   int var=0;
   int i=0;
 
   VDAC8_1_Start();   
    
   for(;;)
   {
     if(i==50000)
   i = 0;
 
  var = 255*(0.5 *(sin(2*PI*frequency*i/sample_per_seconds))+0.5);
        i++;
  
  VDAC8_1_SetValue(var);
  CyDelayUs(20);
   }
}
 

0 Likes
4 Replies
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Since you do a lot of calculation inside of your loop, it might be just run much more slowly than you (esp. the sin() call might be slow). Actually, the recommended way is to use a DMA transfer. There used to be a application note about this, but its under review right now. But you can look at this app note: http://www.cypress.com/?rID=37793 (example #3) and this blog post: http://www.cypress.com/?rID=39390 to get an idea.

0 Likes
Anonymous
Not applicable

One word,WaveDAC8!

   

Heres more on that: Making Waves with the WaveDAC8 PSoC Creator Component

   

Its still a concept component,but I've used it and it does the job quite well,not to mention the extreme ease of use.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Yes, as posted before WaveDAC8 is a pretty good module for sine-waves.

   

 

   

I have another subject on your code: You write

   

if (i==50000) i=0;

   

Although it is obvois in this short program that this will work error-free I wouls insist in writing

   

 

   

if(i >= 50000) i= 0;

   

 

   

which would work correctly even when i is 50001 whatever the reason for this might be.

   

Save coding

   

Bob

0 Likes
Anonymous
Not applicable

1. Set the Psoc CPU clock to 48mh or as high as your chip can support.

   

2. remove the delay loop

   

3. This is the fastest you can do with your program.

   

4. flaoting point use a lot of instruction, if you are not goint to use wave8 as suggest. Then you should see if you can use a table to generate the output or use approxiamtion formula.

0 Likes