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

Peak detector

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







Peak detector
PostPosted: Mon Apr 02, 2007 4:24 pm     Reply with quote

I did this code for a sine peak detector , but it's not so precise . Does some have any idea how can I improve it to get more precison?And another question , I'm just put a diode for the negative cicle of the sine wave , is only that going to protect the Pic16f877 againt negative inputs. And the peak it's lower than 5V. Thanks

void main()
{


Long int cont=0;
float value,value1,value2, peak=0,peak1=0;
set_tris_d(0);
set_tris_c(0);
set_tris_b(0b01000000);
output_d(0);
output_c(0);


LCD_init();
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_DIV_32);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);



set_adc_channel (0);
delay_us(10);


while (cont<250>peak)
peak=value;
value2=read_adc();
if (value1>peak)
peak=value1;
if (value2>peak)
peak=value2;

cont++;
}


peak= (5*(peak+1)/1023);
}
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

Re: Peak detector
PostPosted: Mon Apr 02, 2007 7:26 pm     Reply with quote

Leo22 wrote:
Does some have any idea how can I improve it to get more precison?


You know that the waveform coming into the A/D is a half-wave rectified sine. Integrate over one half-period (instead of looking for a peak mesurement). Knowing the frequency of the sine and the integral, you can calculate the peak.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Apr 03, 2007 8:19 am     Reply with quote

What are you going to use this peak for? If you know the signal is a really good sine then kender's idea will give a very good answer. But if the further the signal differs from a true sign the more error.

If you are measuring AC voltage or power you probably want RMS not peak. You can calculate RMS from kender's integral.

If you want the peak value so you can adjust gain to prevent clipping then you want the real peak whether it is a sine or not.

To protect the A/D from the negative half cycle a resistor and a Schottky diode work well. A common signal diode (1N914) will work up to audio frequencies. Don't use 1N4000 series diodes above about 400 Hz.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Leo22
Guest







PostPosted: Tue Apr 03, 2007 4:03 pm     Reply with quote

I have to mesuare a gain between to sine waves, but to mesuare the RMS value I need a external circuit, I want to put the sine wave directly to the PIC.
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Wed Apr 04, 2007 2:08 am     Reply with quote

I think you need to define your requirements a little better and then look at the underlying math.

  1. Are you detecting fixed frequency waveforms?
  2. Are the waveforms same frequency, different amplitude?

The easiest way, for a fixed frequency system is a true peak detector (i.e. series diode, shunt cap). Anything more complex than this (or using the MCU for the sampling/peak detection/smoothing) will be very waveform dependent (unless you can achieve tru-RMS performance).
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Apr 04, 2007 11:01 am     Reply with quote

Leo22 wrote:
I have to mesuare a gain between to sine waves, but to mesuare the RMS value I need a external circuit, I want to put the sine wave directly to the PIC.

RMS is just the Root of the Mean Square, all doable in PIC software, no hardware required. What is the frequency of the sine waves?
_________________
The search for better is endless. Instead simply find very good and get the job done.
Leo22
Guest







PostPosted: Wed Apr 04, 2007 1:31 pm     Reply with quote

It's 1khz , can I get the rms value with Pic? How can I do that? Thanks for your attention
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Apr 04, 2007 2:33 pm     Reply with quote

It is the Root of the Mean of the Square. So you take each datapoint, square it, average it over some period of time like one cycle, and take the square root.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Rocket



Joined: 29 Aug 2005
Posts: 27

View user's profile Send private message

PostPosted: Thu Apr 05, 2007 2:45 pm     Reply with quote

Have a look at 'EX_RMSDB.C' in the Examples directory.

SHALOM!
_________________
J.I.L.
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Sun Apr 08, 2007 2:33 am     Reply with quote

One thing you may want to keep in mind is that for either peak detection or RMS to actually work you must sample the signal using the Nyquist criteria. On the analog domain this is not really a problem, but on the digital domain you must sample the signal at least twice as fast as the maximum frequency. For 1kHz you are probably ok, but nonetheless you should verify it.
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