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

Newbee question: ex_tones

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







Newbee question: ex_tones
PostPosted: Thu Sep 14, 2006 1:29 pm     Reply with quote

Good afternoon !

Im using mostly the same part of the ex_tones example for generating sound from 1khZ to 70 Khz. If i attach the oscilloscope to see what i get as output, all looks fine accept one small problem.

I use this calculation for generating 1 KHZ at 8mhz :
Code:

const long KHZ_1[1]   =   {2500};    // one khz
const long KHZ_2[1]   =   {5000}; // 2 khz
const long KHZ_4[1]   =   {10000};// 4 khz
...
const long KHZ_16[1]  =   {40000}; // 16khz
const long KHZ_20[1]  =   {50000}; // 20 khz

... you get the picture.

in the main part i call
Code:
KHZ_16[0], 6000

This will play cca 3 seconds of the 16khz sound.

If i call :
Code:
KHZ_20[0], 7507

this will play 3 seconds of 20khz signal.

I can reach all freq. ( till 70 khz ) but i can not play the 20 and above khz sound longer than 1 second ( sometimes even less than second ) and i want output it for 3 seconds. I can see on the osciloscop that the freq. is right but the time is much too quick. This happens only on freq. greater than 20 khZ. How should i the make calculation ?
Newbee
Guest







PostPosted: Fri Sep 15, 2006 5:30 am     Reply with quote

any idea ?
by the way, can this be hardware problem ?

I have tested values till 20 and 20 khz with no problems.
All other freq. greater than 20 khZ do not work. Confused
Guest








PostPosted: Fri Sep 15, 2006 8:19 am     Reply with quote

Havenīt looked at the code but maybe some variable is not long enough to hold the correct value after this frequency and time. Look at the calculations, figure out the ones that receive big values and change them to int32.
Ttelmah
Guest







PostPosted: Fri Sep 15, 2006 8:29 am     Reply with quote

First screaming comment. The idea of using arrays to hold single values, is ludicrous. Either just declare the values (no in arrays), or have a single array, and 'define' symbolic names for the entries.
However the reason the code will fail, is that it is not designed to handle values this fast (think audio only). As the other poster has said, think value sizes. Maximum value for the tone, is 65535 (long), and with your 2.5* scaling, this is 26214Hz. Even before this though, the frequencies will show a significant error...

Best Wishes
Newbee
Guest







PostPosted: Fri Sep 15, 2006 10:07 am     Reply with quote

Hallo !

Quote:
First screaming comment.

Very Happy Mr. Green

Quote:
figure out the ones that receive big values and change them to int32.
i changed that but it looks like there is still problem with calculations.
Code:

const long  KHZ_1   =   2500;      
const long  KHZ_2   =   5000;                  
const long  KHZ_4   =   10000;                  
const long  KHZ_8   =   20000;   


Quote:
Maximum value for the tone, is 65535 (long), and with your 2.5* scaling, this is 26214Hz.
. I have made the changes but im missing the calculations, becuse the problem is the same. What ( accept listed before ) must i change to reach over 20 khZ and where can i make this changes. ? Shocked
Newbee
Guest







PostPosted: Sun Sep 17, 2006 11:58 am     Reply with quote

Becuse the freq over 22 khz is very difficult to see on the old osciloscop ( like mine ) i have several problems.
I can not check if im doing wright or not.
I have added LED to light after the khz cycle is over and this if functioning until i pass 20 khz ( last post ).
I have changed vars to int32 (max 2,147,483,647) but program stucks again over 20khz.

Can someone give me some example what else should i do becuse i dont understand the parts related to total delay time and number of periods.
How should i make recalculation and is this all to it or do i have to change something else.


This is the main part of original( without my changes )tones.c code from ccs that im using. Main changes i did was that i have changed the freq note to 1khz - 30 khz.

Code:
const long  KHZ_1   =   2500;                                 
const long  KHZ_2   =   5000;            

void do_delay(int ms_delay, int num_ms, int us_delay, int num_us)  {
 int i;

 for(i=0;i<num_ms;i++)
  delay_ms(250);
 delay_ms(ms_delay);
 for(i=0;i<num_us;i++)
  delay_us(250);
 delay_us(us_delay);
}


void generate_tone(long frequency, long duration)
{
   int32 total_delay_time;                      // in microseconds
   long total_ms_delay_time, total_us_delay_time;
   int num_us_delays, num_ms_delays, ms_delay_time, us_delay_time;
   long num_periods;

   total_delay_time = (1000000/frequency)/2-10; // calculate total delay time (10 for error)

   total_ms_delay_time = total_delay_time/1000; // total delay time of ms
   num_ms_delays = total_ms_delay_time/250;     // number of 250ms delays needed
   ms_delay_time = total_ms_delay_time%250;     // left over ms delay time needed

   total_us_delay_time = total_delay_time%1000; // total delay time of us (ms already acounted for)
   num_us_delays = total_us_delay_time/250;     // number of 250us delays needed
   us_delay_time = total_us_delay_time%250;     // left over us delay time needed

   num_periods = ((int32)duration*1000)/(1000000/frequency);

   while((num_periods--) != 0)
   {
      do_delay(ms_delay_time, num_ms_delays, us_delay_time, num_us_delays);
      output_high(TONE_PIN);
      do_delay(ms_delay_time, num_ms_delays, us_delay_time, num_us_delays);
      output_low(TONE_PIN);
   }

   return;
}
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sun Sep 17, 2006 8:06 pm     Reply with quote

Code:
num_periods = ((int32)duration*1000)/(1000000/frequency);


If duration is 6000 and frequency is 50000 then num_periods would be 300000 which

Code:
long num_periods;

won't fit.
Newbee
Guest







PostPosted: Mon Sep 18, 2006 4:50 am     Reply with quote

/* dur. freq. # periods
// (6000 * 1000 )/(1000000/50000 )= 6000000 / 20 = 300 000
I have changed the num_periods to int32 and now i have this situation:

Code:
const struct note
{ long tone;
  long length;
} khz[2] = { KHZ_1, 2000, // playing time cca 2 second
          KHZ_20,2000  // playing time cca 7 seconds
         };
LED lights


If i set 22 khz i can hear 1KHZ and 22khz stats to play, but then again program hangs and there is no light on cycle end
( he never ends ).

Code:
This is my KHZ table:
const long  KHZ_1   =   2500;                           
const long  KHZ_2   =   5000;                  
const long  KHZ_4   =   10000;                  
const long  KHZ_8   =   20000;                  
const long  KHZ_16  =   40000;                  
const long  KHZ_18  =   45000;                  
const long  KHZ_20  =   50000;
const long  KHZ_22  =   55000;                                 
const long  KHZ_24  =   60000;
const long  KHZ_26  =   65000;                                 
const long  KHZ_28  =   70000;                  
const long  KHZ_30  =   75000;


Is there something else i should change to get proper khz output ?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Sep 18, 2006 6:37 am     Reply with quote

Do you own a calculator?

Code:
total_delay_time = (1000000/frequency)/2-10; // calculate total delay time (10 for error)


= (1000000/55000)/2 - 10
= 18/2 - 10
= 9 - 10
which isn't going to work
Ttelmah
Guest







PostPosted: Mon Sep 18, 2006 7:24 am     Reply with quote

Also
Code:

const long  KHZ_28  =   70000;                   
const long  KHZ_30  =   75000;


Won't work.

The whole of this post, has the 'ring', of somebody trying to use a 'nut to crack a sledgehammer'... The 'tones' program is designed to produce audio tones. There are a whole set of new problems that will appear as signals (not tones, once you are above 20KHz), get faster.
Realistically, if you want to develop hgh frequency signal with reasonable accuracies, look at using the hardware timer modules.

Best Wishes
Newbee
Guest







PostPosted: Mon Sep 18, 2006 8:23 am     Reply with quote

Quote:
Do you own a calculator?

Im laughting for last 2 minutes :-)). Mark you rule !. Keep that spirit !

Ttelmah is "unfortunately" right. I can play freq. till 24 khz but after that, freq. get mixed up and you can see them and sometimes hear them (26 khz (!?)), so "i guess" that he is not playing right freq. :-)

If there is a chance i want to stick to this code. Now i partly know how is this working so if there is possibility to continue on this code, please be so kind and point me into right direction.

Quote:
There are a whole set of new problems that will appear..

Becuse you both know why will there be problems, please give me idea how to solve this..
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