![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Guest
|
Calculation of pulse rate |
Posted: Tue Jan 19, 2010 11:18 pm |
|
|
Code: | for (x=5;x<128;x++)
{
read1 = read_adc();
newresult1 = (float)read1;
if ( newresult1 > max_adc1 )
{
max_adc1 = newresult1;
time1=x ;
for (time1;x<128;x++)
{
read2 = read_adc();
newresult2= (float)read2;
if ( newresult2 > max_adc2 )
{
max_adc2 = newresult2;
time2=x ;
}
else
total_time = time2 - time1;
pulse_rate = 60 / total_time;
}
}
}
sprintf(String_Buff,pulse_rate);
LCD_Print_String(YELLOW, BLACK, 75, 75,String_Buff);
|
Hi, basically I want to calculate the pulse rate between 2 waveform and X is my time domain (between 5 to 128).
Why is it that when I display the value out onto my LCD (nokia6610) , they are in a funny code? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 19, 2010 11:33 pm |
|
|
Quote: | sprintf(String_Buff, pulse_rate); |
You don't have a format string in your sprintf() statement. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Wed Jan 20, 2010 12:11 am |
|
|
Code: | sprintf(String_Buff, "%d", pulse_rate); |
ok thanks alot of on that. but why is it that my value is wrong, is there any errors in my code? i want to get the peaks of every waveform to calculate the pulse rate |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Jan 20, 2010 4:25 am |
|
|
We can't tell.
The problem is that the results at every stage, depend on what values are in variables when you arrive at the code, and on the types of the variables. You show neither....
There are some comments.
Start with 'read1'. This needs to be an int16 or larger, assuming that you have the ADC set to return a 10 bit value. If not, it can bean 8bit integer.
Then you convert the value to a float (the cast is not needed, conversion is automatic when you pass a variable to a larger type).
You then waste a _lot_ of time. It'd be much better for maxadc1, to be an integer of the same type as read1, and to perform the comparison on integers, rather than float values.
Then the question is what you are actually trying to measure.
If you are looking for a 'peak', then your detection needs to be something like:
Code: |
int16 maxadc1=0;
int16 maxadc2=0;
int16 temp;
int x, time1, time2;
for (x=5;x<128;x++) {
temp = read_adc();
if ( temp < max_adc1 ) {
//If the signal starts to _fall_,the highest point
//was the last reading
time1=x-1;
for (;x<128;x++) {
temp = read_adc();
if (temp < max_adc2 ) {
time2=x-1; //again the 'peak', is one count before the signal
//falls.
break;
}
else
//Signal is rising
maxadc2=temp;
}
}
else
//signal is rising
maxadc1=temp;
}
total_time = time2 - time1;
pulse_rate = 60 / total_time;
|
As you currently work, if the signal is rising, it'll be detected immediately, and drop straight through both tests....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Thu Jan 21, 2010 9:38 am |
|
|
Ok, thanks for your reply. Somebody suggested to me using a timer to measure the time between the peak intervals instead of using the X - axis. The X axis are actually pixels on my LCD screen.
Code: |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);
set_timer0(0) ; //this sets timer0 register to 0
int16 maxadc1=0;
int16 maxadc2=0;
int16 temp,time1, time2,total_time, pulse_rate;
unsigned int x, axis1;
for (x=5;x<128;x++)
{
temp = read_adc();
if ( temp > max_adc1 ) // if the new result is higher than the current max adc value, it will overwrite the new result as the max adc. if the new result is smaller, it will go on the find the next peak
{
max_adc1 = temp; // overwrite the new result as the new max adc.
}
else
axis1 = x;
time1=get_timer0(); //this will read the timer0 register value, the time of the 1st peak value.
for (axis1;x<128;x++) // repeating the same process the get the next peak, after the 1st peak .
{
temp = read_adc();
if ( temp > max_adc2 ) // same as above
{
max_adc2 = temp;
}
else
time2=get_timer0(); //this will read the timer0 register value , the time of the 2nd peak value.
total_time = time2 - time1;
pulse_rate = 60 / total_time; |
Am i correct with my code? if i am wrong, please guide me. Thanks in advance |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Sat Jan 23, 2010 12:11 am |
|
|
Can anybody help me with this, why am I unable to get the 2nd timer0 value? I tested it out by setting it to 100, but when I display it on my screen, it states -80.
Code: |
for (x=5;x<128;x++)
{
temp = read_adc();
while(max_adc1<(float)temp)
{
max_adc1 = temp; // overwrite the new result as the new max adc.
}
set_timer0(50);
axis1 = x;
time1=get_timer0(); //this will read the timer0 register value, the time of the 1st peak value.
}
for (axis1;x<128;x++) // repeating the same process the get the next peak, after the 1st peak .
{
temp = read_adc();
while(max_adc2<(float)temp)
{
max_adc2 = temp;
}
set_timer0(100);
time2=get_timer0(); //this will read the timer0 register value , the time of the 2nd peak value.
total_time = time2 - time1;
pulse_rate = 6000 / total_time;
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|