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

Can EX_Tones.c run on 4MHZ clock?

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



Joined: 27 Oct 2003
Posts: 12

View user's profile Send private message

Can EX_Tones.c run on 4MHZ clock?
PostPosted: Fri Dec 31, 2004 7:06 pm     Reply with quote

I modified ex_tones.c to run on 16F627A, and using it's 4Mhz INTRC. However, the frequency is not playback correctly. Is it possible to run this example in 4Mhz clock?
Thank you Smile
Guest
Guest







PostPosted: Sat Jan 01, 2005 7:39 am     Reply with quote

Is the a few precent off, or way off? If it is only a few percent it is probably the error of the internal RC clock. There is no fundamental reason it can't run at 4MHz.

Sherpa Doug
Victor



Joined: 27 Oct 2003
Posts: 12

View user's profile Send private message

PostPosted: Sat Jan 01, 2005 8:15 pm     Reply with quote

The frequecy is totally out initially. After I change the error correction
Code:

total_delay_time = (1000000/frequency)/2-316;

It play correctly from octave0 C-note[0] to octave2 G-note[2] only. At octave2 G Note, total_delay_time become negative value after minus of 316. Sad

How to fix this problem and make it play up to octave 3?

Thank you. Smile

Code:

#include <16F627A.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)

#define TONE_PIN  PIN_B0

//         NOTE                 FREQUENCY
//                Octave0  Octave1  Octave2  Octave3
const long C[4]  ={ 262,     523,    1047,    2093};
const long Db[4] ={ 277,     554,    1109,    2217};
const long D[4]  ={ 294,     587,    1175,    2349};
const long Eb[4] ={ 311,     622,    1245,    2489};
const long E[4]  ={ 330,     659,    1329,    2637};
const long F[4]  ={ 349,     698,    1397,    2794};
const long Gb[4] ={ 370,     740,    1480,    2960};
const long G[4]  ={ 392,     784,    1568,    3136};
const long Ab[4] ={ 415,     831,    1661,    3322};
const long A[4]  ={ 440,     880,    1760,    3520};
const long Bb[4] ={ 466,     923,    1865,    3729};
const long B[4]  ={ 494,     988,    1976,    3951};

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)

   // By changing error to 316, it play correctly from C_note[0] to G_note[2] only
   total_delay_time = (1000000/frequency)/2-316;
   
   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;
}



void main(void)
{
   
   while(1)
   {
    // Octave 2
     generate_tone(c[2],300); //ok
     generate_tone(d[2],300); //ok
     generate_tone(e[2],300); //ok
     generate_tone(f[2],300); //ok
     generate_tone(g[2],300); //ok

     // Octave 2, A_note
     generate_tone(a[2],300); // stuck here, total_delay_time < 0

   }
}
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