|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
What is wrong?: "subscript out of range" |
Posted: Fri Feb 18, 2005 11:11 am |
|
|
Hi, I don't know why this will not compile. It gives an error of "subscript out of range" for fs[7].What's the problem with this code?
Code: |
#include <18F458.h>
#device ADC=8 //set for 8 bit A/D
#use delay(clock=20000000) //40Mhz
#fuses HS, OSCSEN, BROWNOUT, BORV20, STVREN, NODEBUG, NOLVP //High Speed, Oscillator switching enabled, Brownout reset at 2.0V, Stack full causes rest, Low Voltage programming
float fs[7];
unsigned int16 q=440;
void main()
{
fs[0] = (440/q)*2439; //Octave 0
fs[1] = (440/q)*1220; //Octave 1
fs[2] = (440/q)*610; //etc.
fs[3] = (440/q)*305;
fs[4] = (440/q)*152;
fs[5] = (440/q)*76;
fs[6] = (440/q)*38;
fs[7] = (440/q)*19;
} | [/code] |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Fri Feb 18, 2005 11:16 am |
|
|
Try it with the following declaration
|
|
|
Guest
|
|
Posted: Fri Feb 18, 2005 11:25 am |
|
|
Yeah I tried that with that little test program, but here is my real program and it has the same problem. Could it have to do with running out of RAM? Please try to compile this code and see whats wrong. Thanks
Code: |
//Control Program
#include <18F458.h>
#device ADC=8 //set for 8 bit A/D
#use delay(clock=40000000) //40Mhz
#fuses HS, OSCSEN, BROWNOUT, BORV20, STVREN, NODEBUG, NOLVP //High Speed, Oscillator switching enabled, Brownout reset at 2.0V, Stack full causes rest, Low Voltage programming
//Calibration variables
unsigned int j = 0; //ones place
unsigned int k = 4; //tens place
unsigned int m = 4; //hundreds place
unsigned int r = 0;
unsigned int16 q =440;
//Tuning mode variables
unsigned int16 i = 0;
unsigned int16 i2 = 0;
unsigned int sig[1300]; //array for signal data
unsigned int test=0; //To test if input is above voltage threshold
unsigned int16 freq=0; //Global variable for the frequency of the incoming signal
unsigned int16 peaks=0; //Global vairables for number of peaks counted
int default_note = 0; //Global variable to keep the value of the note to be displayed after tuning
int default_octive = 0; //Global variable to keep the value of the note to be displayed after tuning
unsigned int16 fs[7];
float Ts[7];
unsigned int x;
int convert(int n)
{
if(n==0)
{
return 0b00111111;
}
if(n==1)
{
return 0b00000110;
}
if(n==2)
{
return 0b01011011;
}
if(n==3)
{
return 0b01001111;
}
if(n==4)
{
return 0b01100110;
}
if(n==5)
{
return 0b01101101;
}
if(n==6)
{
return 0b01111101;
}
if(n==7)
{
return 0b00000111;
}
if(n==8)
{
return 0b01111111;
}
if(n==9)
{
return 0b01100111;
}
}
int display_note(int n)
{
if(n==0) //A
{
return 0b01110111;
}
if(n==1) //A#
{
return 0b11110111;
}
if(n==2) //B
{
return 0b01111100;
}
if(n==3) //C
{
return 0b00111001;
}
if(n==4) //C#
{
return 0b10111001;
}
if(n==5) //D
{
return 0b01011110;
}
if(n==6) //D#
{
return 0b11011110;
}
if(n==7) //E
{
return 0b01111001;
}
if(n==8) //F
{
return 0b01110001;
}
if(n==9) //F#
{
return 0b11110001;
}
if(n==10) //G
{
return 0b01101111;
}
if(n==11) //G#
{
return 0b11101111;
}
}
void count_up(void)
{
delay_ms(100);
if(j <= 8)
{
j=j+1;
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
j=0;
r = convert(j);
output_d(r);
if(k <= 8)
{
k=k+1;
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
k=0;
//r = convert(k);
output_a(k);
m=m+1;
//r = convert(m);
output_c(m);
delay_ms(100);
}
}
}
void count_down(void)
{
delay_ms(100);
if(j >= 1)
{
j=j-1;
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
j=9;
r = convert(j);
output_d(r);
if(k >= 1)
{
k=k-1;
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
k=9;
//r = convert(k);
output_a(k);
m=m-1;
//r = convert(m);
output_c(m);
delay_ms(100);
}
}
}
void get_freq(void)
{
peaks=0; //reset previous values
for(i=0;i<1296;i++)
{
i2=i+2;
if(sig[i2]>=sig[i2-1] && sig[i2]>=sig[i2-2] && sig[i2]>=sig[i2+1] && sig[i2]>=sig[i2+2])
{
peaks=peaks+1; //Tests to see if current sample is greater that the 2 before and after
}
}
freq = peaks/Ts[x]; //Frequency of the signal
}
void look_up(float u, int16 v) // u = concert A, v = freq
{
float H;
float G;
unsigned int octive = 0;
unsigned int note = 0;
unsigned int r;
unsigned int s;
unsigned int i = 0;
G = u / 16;
u = u / 16;
while(G <= v)
{
G = G * 2;
octive = octive+1;
}
octive = octive - 1;
s = 1;
while(s <= octive)
{
u = u * 2;
s = s+1;
}
for ( i = 0; u < v; i++)
{
u = u * 1.0297F;
}
H = u/v;
if(bit_test(i, 0) == 1) //odd and that it is high
{
note = (i-1)/2;
output_e(0x04); //to high
}
if(bit_test(i, 0) == 0) //even and that it is too low
{
note = i/2;
if(H < 1.020000000000000F)
{
output_e(0x02); //exact
}
else
{
output_e(0x01); //to low
}
}
if(note == 12)
{
octive = octive +1;
note = 0;
}
//r = convert(octive);
//output_d(r);
output_c(octive); //Wire up PORT C to LEDs for know and it will display octive in binary
default_note = note; //A global variable to keep the value of the note while finished tuning
default_octive = octive;
r = display_note(note);
output_d(r); //Wire up PORT D to 7 - segment display and it will display the note
delay_ms(100);
}
void calib_m() //Frequency determination mode
{
set_tris_a(0xFF); //tens display
set_tris_d(0xFF); //ones display
set_tris_c(0xFF); //hundreds display
set_tris_b(0x00);
set_tris_e(0x00);
r = convert(7); //S
output_d(r);
r = display_note(7); //E
output_a(r);
r = convert(5); //T
output_c(r);
delay_ms(10000); //Set
r = display_note(0);
output_d(r);
delay_ms(10000); //A
//r = convert(j);
output_d(j);
//r = convert(k);
output_a(k);
//r = convert(m); //Show last setting of Concert A
output_c(m);
q = (int16)m*100 + (int16)k*10 + (int16)j; //q is the decimal representation on the 7-segment display
while(bit_test(input_e(), 0) == 1) //not set, switch out of calibration mode
{
if(bit_test(input_b(), 5) == 1) //reset
{
j=0;
k=4;
m=4;
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
if((341 <= q ) && (q <= 539))
{
if(bit_test(input_b(), 3) == 1)
{
count_up();
}
if(bit_test(input_b(), 2) == 1)
{
count_down();
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
}
if(q == 340) //Cant go below 340Hz
{
if(bit_test(input_b(), 3) == 1)
{
count_up();
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
}
if(q == 540) //Cant go above 540Hz
{
if(bit_test(input_b(), 2) == 1)
{
count_down();
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
}
else
{
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
}
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
}
void tunning_m()
{
float fs[6];
float Ts[6];
unsigned int x= 0;
//set_tris_a(0xF0);
set_tris_d(0xFF); //Wire up PORT D to 7 - segment display and it will display the note
set_tris_c(0xFF); //Wire up PORT C to LEDs for know and it will display octive in binary
set_tris_b(0x00);
set_tris_e(0xFF);
enable_interrupts(INT_AD);
enable_interrupts(global);
setup_adc_ports(RA0_ANALOG); //Set A0 for analog input
setup_adc(ADC_CLOCK_DIV_32); //clock frequency
set_adc_channel(0); //Use PORT A0 to read incoming signal
fs[0] = (440/q)*2439; //Octave 0
fs[1] = (440/q)*1220; //Octave 1
fs[2] = (440/q)*610; //etc.
fs[3] = (440/q)*305;
fs[4] = (440/q)*152;
fs[5] = (440/q)*76;
fs[6] = (440/q)*38;
fs[7] = (440/q)*19;
for (x =0; x <= 7; x++)
{
Ts[x] = fs[x] * 2 * 1300;
}
//x = x - 1; We may have to adjust if x = 8;
while(bit_test(input_e(), 1) == 1)
{
if(bit_test(input_b(), x) == 0)
{
for (x = 0; x <= 7; x ++)
{
if(bit_test(input_b(), x) == 1)
{
test = 0; //initialize test
test = read_adc();
if(test>=170) //Wait until input signal is above certain threshold (So noise isn't "tuned")
{
output_a(0b00100000); //"Tuning" (green)
for(i=0; i<1300 ;i++) //take 1300 samples (.325 seconds worth))
{
sig[i] = read_adc();
delay_us(fs[x]); //4kHz sampling frequency
}
get_freq(); //obtain frequency of digital signal
look_up(q,freq); //Display the note of the signal
}
else
{
output_a(0b00010000); //Output "ready" light (yellow)
freq=0; //reset frequency
r = display_note(default_note); //Display last note tuned
//output_c(r);
output_d(r);
r = convert(default_octive); //Display last octive tuned
//output_d(r);
output_d(r);
}
}
}
}
if(bit_test(input_b(), x) == 1)
{
test = 0; //initialize test
test = read_adc();
if(test>=170) //Wait until input signal is above certain threshold (So noise isn't "tuned")
{
output_a(0b00100000); //"Tuning" (green)
for(i=0; i<1300 ;i++) //take 1300 samples (.325 seconds worth))
{
sig[i] = read_adc();
delay_us(fs[x]); //4kHz sampling frequency
}
get_freq(); //obtain frequency of digital signal
look_up(q,freq); //Display the note of the signal
}
else
{
output_a(0b00010000); //Output "ready" light (yellow)
freq=0; //reset frequency
r = display_note(default_note); //Display last note tuned
//output_c(r);
output_d(r);
r = convert(default_octive); //Display last octive tuned
//output_d(r);
output_d(r);
}
}
}
}
void tone_gen_m()
{
while(bit_test(input_e(), 2) == 1)
{
}
}
void main()
{
while(1)
{
if(bit_test(input_e(), 0) == 1)
{
calib_m();
}
if(bit_test(input_e(), 1) == 1)
{
tunning_m();
}
if(bit_test(input_e(), 2) == 1)
{
tone_gen_m();
}
}
}
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Feb 18, 2005 11:25 am |
|
|
This isn't VB |
|
|
Guest
|
|
Posted: Fri Feb 18, 2005 11:30 am |
|
|
Yeah I tried that with that little test program, but here is my real program and it has the same problem. Could it have to do with running out of RAM? Please try to compile this code and see whats wrong. Thanks
Code: |
//Control Program
#include <18F458.h>
#device ADC=8 //set for 8 bit A/D
#use delay(clock=40000000) //40Mhz
#fuses HS, OSCSEN, BROWNOUT, BORV20, STVREN, NODEBUG, NOLVP //High Speed, Oscillator switching enabled, Brownout reset at 2.0V, Stack full causes rest, Low Voltage programming
//Calibration variables
unsigned int j = 0; //ones place
unsigned int k = 4; //tens place
unsigned int m = 4; //hundreds place
unsigned int r = 0;
unsigned int16 q =440;
//Tuning mode variables
unsigned int16 i = 0;
unsigned int16 i2 = 0;
unsigned int sig[1300]; //array for signal data
unsigned int test=0; //To test if input is above voltage threshold
unsigned int16 freq=0; //Global variable for the frequency of the incoming signal
unsigned int16 peaks=0; //Global vairables for number of peaks counted
int default_note = 0; //Global variable to keep the value of the note to be displayed after tuning
int default_octive = 0; //Global variable to keep the value of the note to be displayed after tuning
unsigned int16 fs[7];
float Ts[7];
unsigned int x;
int convert(int n)
{
if(n==0)
{
return 0b00111111;
}
if(n==1)
{
return 0b00000110;
}
if(n==2)
{
return 0b01011011;
}
if(n==3)
{
return 0b01001111;
}
if(n==4)
{
return 0b01100110;
}
if(n==5)
{
return 0b01101101;
}
if(n==6)
{
return 0b01111101;
}
if(n==7)
{
return 0b00000111;
}
if(n==8)
{
return 0b01111111;
}
if(n==9)
{
return 0b01100111;
}
}
int display_note(int n)
{
if(n==0) //A
{
return 0b01110111;
}
if(n==1) //A#
{
return 0b11110111;
}
if(n==2) //B
{
return 0b01111100;
}
if(n==3) //C
{
return 0b00111001;
}
if(n==4) //C#
{
return 0b10111001;
}
if(n==5) //D
{
return 0b01011110;
}
if(n==6) //D#
{
return 0b11011110;
}
if(n==7) //E
{
return 0b01111001;
}
if(n==8) //F
{
return 0b01110001;
}
if(n==9) //F#
{
return 0b11110001;
}
if(n==10) //G
{
return 0b01101111;
}
if(n==11) //G#
{
return 0b11101111;
}
}
void count_up(void)
{
delay_ms(100);
if(j <= 8)
{
j=j+1;
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
j=0;
r = convert(j);
output_d(r);
if(k <= 8)
{
k=k+1;
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
k=0;
//r = convert(k);
output_a(k);
m=m+1;
//r = convert(m);
output_c(m);
delay_ms(100);
}
}
}
void count_down(void)
{
delay_ms(100);
if(j >= 1)
{
j=j-1;
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
j=9;
r = convert(j);
output_d(r);
if(k >= 1)
{
k=k-1;
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
k=9;
//r = convert(k);
output_a(k);
m=m-1;
//r = convert(m);
output_c(m);
delay_ms(100);
}
}
}
void get_freq(void)
{
peaks=0; //reset previous values
for(i=0;i<1296;i++)
{
i2=i+2;
if(sig[i2]>=sig[i2-1] && sig[i2]>=sig[i2-2] && sig[i2]>=sig[i2+1] && sig[i2]>=sig[i2+2])
{
peaks=peaks+1; //Tests to see if current sample is greater that the 2 before and after
}
}
freq = peaks/Ts[x]; //Frequency of the signal
}
void look_up(float u, int16 v) // u = concert A, v = freq
{
float H;
float G;
unsigned int octive = 0;
unsigned int note = 0;
unsigned int r;
unsigned int s;
unsigned int i = 0;
G = u / 16;
u = u / 16;
while(G <= v)
{
G = G * 2;
octive = octive+1;
}
octive = octive - 1;
s = 1;
while(s <= octive)
{
u = u * 2;
s = s+1;
}
for ( i = 0; u < v; i++)
{
u = u * 1.0297F;
}
H = u/v;
if(bit_test(i, 0) == 1) //odd and that it is high
{
note = (i-1)/2;
output_e(0x04); //to high
}
if(bit_test(i, 0) == 0) //even and that it is too low
{
note = i/2;
if(H < 1.020000000000000F)
{
output_e(0x02); //exact
}
else
{
output_e(0x01); //to low
}
}
if(note == 12)
{
octive = octive +1;
note = 0;
}
//r = convert(octive);
//output_d(r);
output_c(octive); //Wire up PORT C to LEDs for know and it will display octive in binary
default_note = note; //A global variable to keep the value of the note while finished tuning
default_octive = octive;
r = display_note(note);
output_d(r); //Wire up PORT D to 7 - segment display and it will display the note
delay_ms(100);
}
void calib_m() //Frequency determination mode
{
set_tris_a(0xFF); //tens display
set_tris_d(0xFF); //ones display
set_tris_c(0xFF); //hundreds display
set_tris_b(0x00);
set_tris_e(0x00);
r = convert(7); //S
output_d(r);
r = display_note(7); //E
output_a(r);
r = convert(5); //T
output_c(r);
delay_ms(10000); //Set
r = display_note(0);
output_d(r);
delay_ms(10000); //A
//r = convert(j);
output_d(j);
//r = convert(k);
output_a(k);
//r = convert(m); //Show last setting of Concert A
output_c(m);
q = (int16)m*100 + (int16)k*10 + (int16)j; //q is the decimal representation on the 7-segment display
while(bit_test(input_e(), 0) == 1) //not set, switch out of calibration mode
{
if(bit_test(input_b(), 5) == 1) //reset
{
j=0;
k=4;
m=4;
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
if((341 <= q ) && (q <= 539))
{
if(bit_test(input_b(), 3) == 1)
{
count_up();
}
if(bit_test(input_b(), 2) == 1)
{
count_down();
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
}
if(q == 340) //Cant go below 340Hz
{
if(bit_test(input_b(), 3) == 1)
{
count_up();
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
}
if(q == 540) //Cant go above 540Hz
{
if(bit_test(input_b(), 2) == 1)
{
count_down();
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
}
else
{
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
}
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
}
void tunning_m()
{
float fs[6];
float Ts[6];
unsigned int x= 0;
//set_tris_a(0xF0);
set_tris_d(0xFF); //Wire up PORT D to 7 - segment display and it will display the note
set_tris_c(0xFF); //Wire up PORT C to LEDs for know and it will display octive in binary
set_tris_b(0x00);
set_tris_e(0xFF);
enable_interrupts(INT_AD);
enable_interrupts(global);
setup_adc_ports(RA0_ANALOG); //Set A0 for analog input
setup_adc(ADC_CLOCK_DIV_32); //clock frequency
set_adc_channel(0); //Use PORT A0 to read incoming signal
fs[0] = (440/q)*2439; //Octave 0
fs[1] = (440/q)*1220; //Octave 1
fs[2] = (440/q)*610; //etc.
fs[3] = (440/q)*305;
fs[4] = (440/q)*152;
fs[5] = (440/q)*76;
fs[6] = (440/q)*38;
fs[7] = (440/q)*19;
for (x =0; x <= 7; x++)
{
Ts[x] = fs[x] * 2 * 1300;
}
//x = x - 1; We may have to adjust if x = 8;
while(bit_test(input_e(), 1) == 1)
{
if(bit_test(input_b(), x) == 0)
{
for (x = 0; x <= 7; x ++)
{
if(bit_test(input_b(), x) == 1)
{
test = 0; //initialize test
test = read_adc();
if(test>=170) //Wait until input signal is above certain threshold (So noise isn't "tuned")
{
output_a(0b00100000); //"Tuning" (green)
for(i=0; i<1300 ;i++) //take 1300 samples (.325 seconds worth))
{
sig[i] = read_adc();
delay_us(fs[x]); //4kHz sampling frequency
}
get_freq(); //obtain frequency of digital signal
look_up(q,freq); //Display the note of the signal
}
else
{
output_a(0b00010000); //Output "ready" light (yellow)
freq=0; //reset frequency
r = display_note(default_note); //Display last note tuned
//output_c(r);
output_d(r);
r = convert(default_octive); //Display last octive tuned
//output_d(r);
output_d(r);
}
}
}
}
if(bit_test(input_b(), x) == 1)
{
test = 0; //initialize test
test = read_adc();
if(test>=170) //Wait until input signal is above certain threshold (So noise isn't "tuned")
{
output_a(0b00100000); //"Tuning" (green)
for(i=0; i<1300 ;i++) //take 1300 samples (.325 seconds worth))
{
sig[i] = read_adc();
delay_us(fs[x]); //4kHz sampling frequency
}
get_freq(); //obtain frequency of digital signal
look_up(q,freq); //Display the note of the signal
}
else
{
output_a(0b00010000); //Output "ready" light (yellow)
freq=0; //reset frequency
r = display_note(default_note); //Display last note tuned
//output_c(r);
output_d(r);
r = convert(default_octive); //Display last octive tuned
//output_d(r);
output_d(r);
}
}
}
}
void tone_gen_m()
{
while(bit_test(input_e(), 2) == 1)
{
}
}
void main()
{
while(1)
{
if(bit_test(input_e(), 0) == 1)
{
calib_m();
}
if(bit_test(input_e(), 1) == 1)
{
tunning_m();
}
if(bit_test(input_e(), 2) == 1)
{
tone_gen_m();
}
}
}
|
|
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Fri Feb 18, 2005 11:36 am |
|
|
In your real program you got even further from the solution declaring the array of another element less : [6] |
|
|
Guest
|
|
Posted: Fri Feb 18, 2005 11:48 am |
|
|
Yeah I tried that with that little test program, but here is my real program and it has the same problem. Could it have to do with running out of RAM?
Code: |
//Control Program
#include <18F458.h>
#device ADC=8 //set for 8 bit A/D
#use delay(clock=40000000) //40Mhz
#fuses HS, OSCSEN, BROWNOUT, BORV20, STVREN, NODEBUG, NOLVP //High Speed, Oscillator switching enabled, Brownout reset at 2.0V, Stack full causes rest, Low Voltage programming
//Calibration variables
unsigned int j = 0; //ones place
unsigned int k = 4; //tens place
unsigned int m = 4; //hundreds place
unsigned int r = 0;
unsigned int16 q =440;
//Tuning mode variables
unsigned int16 i = 0;
unsigned int16 i2 = 0;
unsigned int sig[1300]; //array for signal data
unsigned int test=0; //To test if input is above voltage threshold
unsigned int16 freq=0; //Global variable for the frequency of the incoming signal
unsigned int16 peaks=0; //Global vairables for number of peaks counted
int default_note = 0; //Global variable to keep the value of the note to be displayed after tuning
int default_octive = 0; //Global variable to keep the value of the note to be displayed after tuning
unsigned int16 fs[7];
float Ts[7];
unsigned int x;
int convert(int n)
{
if(n==0)
{
return 0b00111111;
}
if(n==1)
{
return 0b00000110;
}
if(n==2)
{
return 0b01011011;
}
if(n==3)
{
return 0b01001111;
}
if(n==4)
{
return 0b01100110;
}
if(n==5)
{
return 0b01101101;
}
if(n==6)
{
return 0b01111101;
}
if(n==7)
{
return 0b00000111;
}
if(n==8)
{
return 0b01111111;
}
if(n==9)
{
return 0b01100111;
}
}
int display_note(int n)
{
if(n==0) //A
{
return 0b01110111;
}
if(n==1) //A#
{
return 0b11110111;
}
if(n==2) //B
{
return 0b01111100;
}
if(n==3) //C
{
return 0b00111001;
}
if(n==4) //C#
{
return 0b10111001;
}
if(n==5) //D
{
return 0b01011110;
}
if(n==6) //D#
{
return 0b11011110;
}
if(n==7) //E
{
return 0b01111001;
}
if(n==8) //F
{
return 0b01110001;
}
if(n==9) //F#
{
return 0b11110001;
}
if(n==10) //G
{
return 0b01101111;
}
if(n==11) //G#
{
return 0b11101111;
}
}
void count_up(void)
{
delay_ms(100);
if(j <= 8)
{
j=j+1;
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
j=0;
r = convert(j);
output_d(r);
if(k <= 8)
{
k=k+1;
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
k=0;
//r = convert(k);
output_a(k);
m=m+1;
//r = convert(m);
output_c(m);
delay_ms(100);
}
}
}
void count_down(void)
{
delay_ms(100);
if(j >= 1)
{
j=j-1;
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
j=9;
r = convert(j);
output_d(r);
if(k >= 1)
{
k=k-1;
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
else
{
k=9;
//r = convert(k);
output_a(k);
m=m-1;
//r = convert(m);
output_c(m);
delay_ms(100);
}
}
}
void get_freq(void)
{
peaks=0; //reset previous values
for(i=0;i<1296;i++)
{
i2=i+2;
if(sig[i2]>=sig[i2-1] && sig[i2]>=sig[i2-2] && sig[i2]>=sig[i2+1] && sig[i2]>=sig[i2+2])
{
peaks=peaks+1; //Tests to see if current sample is greater that the 2 before and after
}
}
freq = peaks/Ts[x]; //Frequency of the signal
}
void look_up(float u, int16 v) // u = concert A, v = freq
{
float H;
float G;
unsigned int octive = 0;
unsigned int note = 0;
unsigned int r;
unsigned int s;
unsigned int i = 0;
G = u / 16;
u = u / 16;
while(G <= v)
{
G = G * 2;
octive = octive+1;
}
octive = octive - 1;
s = 1;
while(s <= octive)
{
u = u * 2;
s = s+1;
}
for ( i = 0; u < v; i++)
{
u = u * 1.0297F;
}
H = u/v;
if(bit_test(i, 0) == 1) //odd and that it is high
{
note = (i-1)/2;
output_e(0x04); //to high
}
if(bit_test(i, 0) == 0) //even and that it is too low
{
note = i/2;
if(H < 1.020000000000000F)
{
output_e(0x02); //exact
}
else
{
output_e(0x01); //to low
}
}
if(note == 12)
{
octive = octive +1;
note = 0;
}
//r = convert(octive);
//output_d(r);
output_c(octive); //Wire up PORT C to LEDs for know and it will display octive in binary
default_note = note; //A global variable to keep the value of the note while finished tuning
default_octive = octive;
r = display_note(note);
output_d(r); //Wire up PORT D to 7 - segment display and it will display the note
delay_ms(100);
}
void calib_m() //Frequency determination mode
{
set_tris_a(0xFF); //tens display
set_tris_d(0xFF); //ones display
set_tris_c(0xFF); //hundreds display
set_tris_b(0x00);
set_tris_e(0x00);
r = convert(7); //S
output_d(r);
r = display_note(7); //E
output_a(r);
r = convert(5); //T
output_c(r);
delay_ms(10000); //Set
r = display_note(0);
output_d(r);
delay_ms(10000); //A
//r = convert(j);
output_d(j);
//r = convert(k);
output_a(k);
//r = convert(m); //Show last setting of Concert A
output_c(m);
q = (int16)m*100 + (int16)k*10 + (int16)j; //q is the decimal representation on the 7-segment display
while(bit_test(input_e(), 0) == 1) //not set, switch out of calibration mode
{
if(bit_test(input_b(), 5) == 1) //reset
{
j=0;
k=4;
m=4;
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
if((341 <= q ) && (q <= 539))
{
if(bit_test(input_b(), 3) == 1)
{
count_up();
}
if(bit_test(input_b(), 2) == 1)
{
count_down();
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
}
if(q == 340) //Cant go below 340Hz
{
if(bit_test(input_b(), 3) == 1)
{
count_up();
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
}
if(q == 540) //Cant go above 540Hz
{
if(bit_test(input_b(), 2) == 1)
{
count_down();
}
q = (int16)m*100 + (int16)k*10 + (int16)j;
}
else
{
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
delay_ms(100);
}
}
r = convert(j);
output_d(r);
//r = convert(k);
output_a(k);
//r = convert(m);
output_c(m);
}
void tunning_m()
{
float fs[6];
float Ts[6];
unsigned int x= 0;
//set_tris_a(0xF0);
set_tris_d(0xFF); //Wire up PORT D to 7 - segment display and it will display the note
set_tris_c(0xFF); //Wire up PORT C to LEDs for know and it will display octive in binary
set_tris_b(0x00);
set_tris_e(0xFF);
enable_interrupts(INT_AD);
enable_interrupts(global);
setup_adc_ports(RA0_ANALOG); //Set A0 for analog input
setup_adc(ADC_CLOCK_DIV_32); //clock frequency
set_adc_channel(0); //Use PORT A0 to read incoming signal
fs[0] = (440/q)*2439; //Octave 0
fs[1] = (440/q)*1220; //Octave 1
fs[2] = (440/q)*610; //etc.
fs[3] = (440/q)*305;
fs[4] = (440/q)*152;
fs[5] = (440/q)*76;
fs[6] = (440/q)*38;
fs[7] = (440/q)*19;
for (x =0; x <= 7; x++)
{
Ts[x] = fs[x] * 2 * 1300;
}
//x = x - 1; We may have to adjust if x = 8;
while(bit_test(input_e(), 1) == 1)
{
if(bit_test(input_b(), x) == 0)
{
for (x = 0; x <= 7; x ++)
{
if(bit_test(input_b(), x) == 1)
{
test = 0; //initialize test
test = read_adc();
if(test>=170) //Wait until input signal is above certain threshold (So noise isn't "tuned")
{
output_a(0b00100000); //"Tuning" (green)
for(i=0; i<1300 ;i++) //take 1300 samples (.325 seconds worth))
{
sig[i] = read_adc();
delay_us(fs[x]); //4kHz sampling frequency
}
get_freq(); //obtain frequency of digital signal
look_up(q,freq); //Display the note of the signal
}
else
{
output_a(0b00010000); //Output "ready" light (yellow)
freq=0; //reset frequency
r = display_note(default_note); //Display last note tuned
//output_c(r);
output_d(r);
r = convert(default_octive); //Display last octive tuned
//output_d(r);
output_d(r);
}
}
}
}
if(bit_test(input_b(), x) == 1)
{
test = 0; //initialize test
test = read_adc();
if(test>=170) //Wait until input signal is above certain threshold (So noise isn't "tuned")
{
output_a(0b00100000); //"Tuning" (green)
for(i=0; i<1300 ;i++) //take 1300 samples (.325 seconds worth))
{
sig[i] = read_adc();
delay_us(fs[x]); //4kHz sampling frequency
}
get_freq(); //obtain frequency of digital signal
look_up(q,freq); //Display the note of the signal
}
else
{
output_a(0b00010000); //Output "ready" light (yellow)
freq=0; //reset frequency
r = display_note(default_note); //Display last note tuned
//output_c(r);
output_d(r);
r = convert(default_octive); //Display last octive tuned
//output_d(r);
output_d(r);
}
}
}
}
void tone_gen_m()
{
while(bit_test(input_e(), 2) == 1)
{
}
}
void main()
{
while(1)
{
if(bit_test(input_e(), 0) == 1)
{
calib_m();
}
if(bit_test(input_e(), 1) == 1)
{
tunning_m();
}
if(bit_test(input_e(), 2) == 1)
{
tone_gen_m();
}
}
}
|
|
|
|
Guest
|
|
Posted: Fri Feb 18, 2005 12:02 pm |
|
|
never mind, I found the error. There was the same local variables initialized wrong in the subroutine.... sorry |
|
|
Hans Wedemeyer
Joined: 15 Sep 2003 Posts: 226
|
|
Posted: Sat Feb 19, 2005 4:34 pm |
|
|
Anonymous wrote: | never mind, I found the error. There was the same local variables initialized wrong in the subroutine.... sorry |
You may have an error in your subroutine (arrrrrrrgh VB known as functions in C !) however Libor told you where the Subscript of of range error comes from...
Remember this is C not cobol...
You delcared
unsigned int16 fs[7];
declares there are SEVEN locations in the fs array,
elsewhere in your code you use index fs[7] which is trying to access index 8 in the array.... you do not have 8 locations when you declare fs[7]....
0-6 is a count of 7
0-7 = a count of 8 |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Feb 20, 2005 4:32 am |
|
|
There are many more errors, after browsing the source for two minutes I stopped because I found the following list:
1) Global variables never used (and worse, have same name as local variables):
Code: | unsigned int16 fs[7];
float Ts[7]; |
2) Arrays declared too small. Except for the affore mentioned 'float Ts[6]', there is the same problem with 'float Ts[6]'.
3) An offset error in bit_test:
Code: | for (x =0; x <= 7; x++)
{
Ts[x] = fs[x] * 2 * 1300;
}
//x = x - 1; We may have to adjust if x = 8;
.
.
.
another for-loop with x
.
.
.
if(bit_test(input_b(), x) == 1)
{
| This needs some serious rework:
- The for-loop will exit with x == 8. The bit_test will give strange results when you are trying to test bit 8 (7 is the maximum allowed).
- The line for corrrecting x is in the wrong place, later in the source x is modified again.
- The for-loops will always exit with x being 8 (or 7 after correction). So, if it is always the same value, then it doesn't make sense to pass x as a variable to the bit_test(). Very likely you wanted some other behaviour here. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Feb 20, 2005 4:35 am |
|
|
There are many more errors, after browsing the source for two minutes I stopped because I found the following list:
1) Global variables never used (and worse, have same name as local variables):
Code: | unsigned int16 fs[7];
float Ts[7]; |
2) Arrays declared too small. Except for the affore mentioned 'float fs[6]', there is the same problem with 'float Ts[6]'.
3) An offset error in bit_test:
Code: | for (x =0; x <= 7; x++)
{
Ts[x] = fs[x] * 2 * 1300;
}
//x = x - 1; We may have to adjust if x = 8;
.
.
.
another for-loop with x
.
.
.
if(bit_test(input_b(), x) == 1)
{
| This needs some serious rework:
- The for-loop will exit with x == 8. The bit_test will give strange results when you are trying to test bit 8 (7 is the maximum allowed).
- The line for corrrecting x is in the wrong place, later in the source x is modified again.
- The for-loops will always exit with x being 8 (or 7 after correction). So, if it is always the same value, then it doesn't make sense to pass x as a variable to the bit_test(). Very likely you wanted some other behaviour here. |
|
|
|
|
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
|