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 support@ccsinfo.com

How compute the Sampling frequency of Pic18F4550

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



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

How compute the Sampling frequency of Pic18F4550
PostPosted: Wed Apr 28, 2010 7:52 pm     Reply with quote

Someone can tell me how to compute the frequency of sampling in this code considerate all the delays, I'm using a Pic18f4550 with 20Mhz of frequency
Code:
#include <18F4550.h>
#DEVICE ADC=8
//Configurado para trabajar con 20MHz
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV2,VREGEN
#use delay(clock=48MHZ)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)// RS232 Estándar


void main() {

   int32 Buffer_ADC0;
   float Valor_ADC0;       

   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_DIV_32);
   set_adc_channel(0);
   delay_us(10);
       
while (TRUE){


    set_adc_channel(0);
    delay_us(4);
    Buffer_ADC0 = read_adc();
    delay_us(10);
    Valor_ADC0 =  (float)Buffer_ADC0 *(0.0529);
    printf("%4.2f \r\n", Valor_ADC0);
   
          }   
  }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 28, 2010 7:59 pm     Reply with quote

Put a pulse in the loop and look at it on an oscilloscope or logic analyzer.
I put a 1us delay in it, to make sure the pulse would be long enough for
you to see on the scope:
Quote:

while (TRUE){
output_high(PIN_B0);
delay_us(1);
output_low(PIN_B0);

set_adc_channel(0);
delay_us(4);
Buffer_ADC0 = read_adc();
delay_us(10);
Valor_ADC0 = (float)Buffer_ADC0 *(0.0529);
printf("%4.2f \r\n", Valor_ADC0);

}


Or, toggle a pin. Measure the frequency and multiply it by 2 to get
the actual loop frequency.
Quote:

while (TRUE){
output_toggle(PIN_B0);
set_adc_channel(0);
delay_us(4);
Buffer_ADC0 = read_adc();
delay_us(10);
Valor_ADC0 = (float)Buffer_ADC0 *(0.0529);
printf("%4.2f \r\n", Valor_ADC0);

}
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Wed Apr 28, 2010 9:10 pm     Reply with quote

Razz Thank..
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Thu Apr 29, 2010 2:16 am     Reply with quote

In fact though, the main 'timing' element, will be the serial transmission. Not really the PIC's sampling speed. 4 characters minimum for the float, plus a space, plus the line feed, and carriage return. 7 characters. Default 8bits, no parity, one stop, so 70 bit times. About 7.3mSec. The last two characters will be 'in transmission', when the code loops, so the ADC conversion time, delays etc., will all occur while these are sending. The multiplication, and the divisions needed for the printf, will add up to no more than perhaps 1mSec, so will all be 'done' before the second character completes transmission, so the timing should be within a very few uSec, of this time. So about 135 bytes/sec.

Now, if this is being sent to another device, who's speed can be controlled, you could basically double this by going to 19200bps. Going 'beyond' this, then requires saving time in the loop. For instance, choosing to keep the number as an integer, and multiply by '53', then dividing by ten and sending using %4.2LW, might be acceptable, and will be slightly faster, allowing the baud rate to be increased yet further and still get full performance. Reducing the string length (do you really 'need' the trailing space, and both a LF, and a CR?) - if not just send the number, and CR, and the speed will increase by over 25%...
Go to 57600bps, switch to the integer arithmetic, and reduce the transmitted string to just five characters, and you could push something over 1000 samples/second.

Best Wishes
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