Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include <18F6722.h> #device ICD = TRUE #fuses HS, NOLVP, NOWDT #use delay (clock=20000000) #USE rs232 (baud=9600, xmit = PIN_C6, rcv=PIN_C7) #include<utility.c> #include<math.h> void main() { int reading, i; int8 imin = 255; int8 imax = 0; float array[10]; float Volts; float a = 5.0, b = 255.0; int ctr = 0; setup_adc_ports(AN0_TO_AN1); setup_adc(ADC_CLOCK_INTERNAL); set_adc_channel(1); while(TRUE) { for(i=0; i<=9; i++) { reading = read_adc(); Volts = reading*(a/b); array[i] = Volts; printf("\r\n%f", array[i]); } if(ctr==9) break; ctr++; } for(i=0; i<=9; i++) { if(array[i]<imin) imin=array[i]; if(array[i]>imax) imax=array[i]; } }
I don't think that float type is suitable for your purpose. U have 10 bit adc, so use unsigned short type than.
Hi,
* find max level
* find min level
* if min level = 0 then error
* if max level = 1023 then error
* amplitude = (max_level - min_level) / 2
Klaus
The code I used in the second for loop finds the min and the max level correct?
For learning purpose, it's ok; What we're attempting to tell you is that the code could converge to final result faster by making few changes.
Considering that you are perhaps using AD with 8 bits resolution ( that mean, 256 possible values ), performing assynchronous readings, you surelly should take much more samples in order to make a reliable measurement. Anyway, from the statistical point of view, even if you do not reach all possible values, the more samples, the more accurate the result becomes.lets say, 49, then that means I would have 50 various values to look at to find the amplitude correct? Instead of having just 10 values
Considering that you are perhaps using AD with 8 bits resolution ( that mean, 256 possible values ), performing assynchronous readings, you surelly should take much more samples in order to make a reliable measurement. Anyway, from the statistical point of view, even if you do not reach all possible values, the more samples, the more accurate the result becomes.
As mentioned before in a different context of this thread, the timming for sending each sample with the above code is not deterministically defined, so you should use timer to provide precise interruptions on which each sample should be aqcuired. I'm particularly unaware if exists another way than FFT variants to take the fundamental frequency of a signal without having a reasonable and precise amount of samples per cycle.if I were to find the frequency for my sine wave how would I go about coding it?
As mentioned before in a different context of this thread, the timming for sending each sample with the above code is not deterministically defined, so you should use timer to provide precise interruptions on which each sample should be aqcuired. I'm particularly unaware if exists another way than FFT variants to take the fundamental frequency of a signal without having a reasonable and precise amount of samples per cycle.
float array[50];
int I;
int location1 = 1;
long location2 = 1;
float imin = 255;
float imax = 0;
for(I=0; I<=49; I++)
{
if(array[I]<imin)
{
imin = array[I];
location1 = I+1;
}
if(array[I]>imax)
{
imax = array[I];
location2 = I+1;
}
}
array[I] <= imin
array[I] >= imax
Isn't that logically:
for a minimum
Code:array[I] <= imin
for a maximum
Code:array[I] >= imax
If the maximum or minimum is the same as a previous value the location gets updated along with the min/max values.
As mentioned before in a different context of this thread, the timming for sending each sample with the above code is not deterministically defined, so you should use timer to provide precise interruptions on which each sample should be aqcuired. I'm particularly unaware if exists another way than FFT variants to take the fundamental frequency of a signal without having a reasonable and precise amount of samples per cycle.
it does not seem to give me the correct value for the frequency
You did not implement on this code any kind of interrupt driven timing to give you a precise and reliable sample rate.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?