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

read_adc() taking 2.5ms, 1000x too long

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



Joined: 02 Jun 2008
Posts: 2
Location: Princeton NJ

View user's profile Send private message Visit poster's website

read_adc() taking 2.5ms, 1000x too long
PostPosted: Mon Jun 02, 2008 8:54 pm     Reply with quote

Hi All,

I run the following code and I'm finding that the call to read_adc() is taking 2.5 ms. This is almost a thousand times longer than all sources of info tell me it should take. Even when I run the debugger MPLAB SIM it tells me this should take 3 to 4 us. I've tried the ADC pin tied to ground, to 5V, floating, it seems to have no effect.
Can anyone help me out here?

Thanks!

Code:

#include <16F688.h>
#device adc=8
#fuses HS,NOWDT,NOPROTECT,PUT
#include <stdlib.h>
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C4, rcv=PIN_C5)

#define TRIGGER PIN_A1


void main()
{
  int8 v1, thedata[50], i;
  int1 time_old;

  setup_oscillator(OSC_NORMAL);   //Will actually drive from Master's clock

   //setup ports
  set_tris_a(0B00000001);  //A0 input TIMECLOCK, all others output
  set_tris_c(0b00110111);   //C0 to C2 are ADC, C4 and C5 are RS232 comm
  setup_comparator(NC_NC_NC_NC);

   //set ADC
  setup_adc(ADC_CLOCK_DIV_2);   // originally tried _DIV_32
  setup_adc_ports(sAN6);      //pin C2 as primary ADC
  set_adc_channel(6);

  delay_ms(20);

  while (TRUE)
  {
    output_high(TRIGGER);
    v1 = read_adc();                      //Square wave is high for 2.5ms, low for 2.5ms
    output_low(TRIGGER);
    v1 = read_adc();
  }
 
//also tried reading 200 times between setting the pin high and low.
//this gave about 1/2 second between going high and low - could time
//with a stopwatch.
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 02, 2008 9:23 pm     Reply with quote

1. What is your compiler version ?

2. Are you testing this in actual hardware ?

3. Describe the external circuits, including component values, on
all i/o pins that you're using, and also on the oscillator pins and the
MCLR pin.
Ttelmah
Guest







PostPosted: Tue Jun 03, 2008 2:40 am     Reply with quote

Before blaming the ADC, try a simple test:
Code:

  while (TRUE)
  {
    output_high(TRIGGER);
    delay_us(50);
    output_low(TRIGGER);
    delay_us(50);
  }

A PCM programmer says, 'hardware details'.
Realistically, even at it's slowest clock, the ADC cannot take 2.5mSec, if your master clock _is_ 20MHz. The only way it can take this time, is that your master clock is actually vastly slower than you think. The 'loop', should involve about 20 instruction times for each ADC conversion, another couple to read each value, and perhaps four more to actually loop. If the loop is taking 5mSec, then it implies the master oscillator is actually running at perhaps 10000Hz, not 20MHz. So, test it with a simple loop like that shown. If this gives an output wavefrm at about 10KHz, then your oscillator is OK, If though it runs a lot slower, then you have found the 'real' problem.

Best Wishes
Brooksy



Joined: 02 Jun 2008
Posts: 2
Location: Princeton NJ

View user's profile Send private message Visit poster's website

Problem Solved - Was Oscillator Configuration
PostPosted: Tue Jun 03, 2008 5:47 am     Reply with quote

Thanks for the responses. I just tried something and it fixed the problem.

This was running on actual hardware. The square wave was observed on an oscilloscope. The thing I had been trying for the first time was running this PIC's clock from an oscillator on another PIC. The master PIC had the 20MHz oscillator and I connected the OSC2 pin on that chip to the OSC1 pin on the PIC in question. I just found out about this possibility on another post. This seemed to work fine, even doing printf() over RS232 the timing worked fine.

When I pulled out the single wire to OSC1 and replaced it with the 20Mhz oscillator itself, the usual configuration, the 2.5ms square wave generated with the code above became a 3us square wave. So I'm back in business.
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