Victor
Joined: 27 Oct 2003 Posts: 12
|
|
Posted: Sat Jan 01, 2005 8:15 pm |
|
|
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.
How to fix this problem and make it play up to octave 3?
Thank you.
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
}
}
|
|
|