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

external clock triggering adc sampling

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



Joined: 25 Feb 2009
Posts: 9

View user's profile Send private message

external clock triggering adc sampling
PostPosted: Mon Mar 09, 2009 8:17 pm     Reply with quote

Hi,

I am trying to use an external clock input in my PIC18F2525. I want to use either the hi-2-lo or lo-2-hi edge of this clock pulse to trigger a portion of code that would sample for an a/d conversion. I want to sample only part of the analog signal that follows about 10-20 ms immediately after the clock's hi-to-lo or lo-to-hi edge.

I'm not sure exactly how to go about this. I know there's a configuration specified in the datasheet that shows an external clock signal being input on pin CLK1. But I don't know exactly how to accomplish what I stated above. Any guidance/sample code/explanations would be highly appreciated. Thanks in advance Smile

ashrau
_________________
ashrau
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Mar 10, 2009 12:04 am     Reply with quote

I think, the problem can be handled by connecting the event signal to an external interrupt rather than a clock input.

To achieve a delay, the interrupt can start a timer and the timer interupt the AD conversion. However, if the delay isn't required to be exact below a ms resolution, the event input can be polled in a periodical timer tic, without needing external interrupts.
ashrau



Joined: 25 Feb 2009
Posts: 9

View user's profile Send private message

PostPosted: Tue Mar 10, 2009 12:26 am     Reply with quote

FvM wrote:
I think, the problem can be handled by connecting the event signal to an external interrupt rather than a clock input.

To achieve a delay, the interrupt can start a timer and the timer interupt the AD conversion. However, if the delay isn't required to be exact below a ms resolution, the event input can be polled in a periodical timer tic, without needing external interrupts.


Thanks for the reply Smile.

I really don't understand what you meant though, probably because im new to microcontroller programing. How do i use the signal with an external interrupt (both in hardware and software terms). Thanks!
_________________
ashrau
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Mar 10, 2009 5:06 am     Reply with quote

You connnect the trigger to one of external interrupt pins (INT0..INT2), define an interrupt handler #INT_EXT in CCS C and enable it by enable_interrupts() and ext_int_edge().
ashrau



Joined: 25 Feb 2009
Posts: 9

View user's profile Send private message

PostPosted: Tue Mar 10, 2009 2:16 pm     Reply with quote

FvM wrote:
You connnect the trigger to one of external interrupt pins (INT0..INT2), define an interrupt handler #INT_EXT in CCS C and enable it by enable_interrupts() and ext_int_edge().


Thanks again for the help. Here's the code I have so far, which doesn't seem to be turning on the LEDs when I expect it to:

Code:
#include <18F2525.h>
#device adc=10
#fuses NOWDT,INTRC_IO, NOPUT, NOMCLR, BROWNOUT, LVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=4000000)

int target, heading;

#INT_EXT
compass() {
   delay_ms(5);
   heading = read_adc();
   clear_interrupt(INT_EXT);
   }

void main (void){

   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);

   target = read_adc();   //acquire initial reading from compass signal

   enable_interrupts(INT_EXT);
   ext_int_edge(L_TO_H);

   while(TRUE){
      if((abs(target-heading)>8)&&(target>heading)){
         output_low(PIN_A2);
         output_high(PIN_A1);
         }
      else if ((abs(target-heading)>8)&&(target<heading)){
         output_low(PIN_A1);
         output_high(PIN_A2);
      }
   }
}


I have a compass signal, that gives me a voltage that i pass through the adc and store in "target", as my initial reading. I take subsequent readings for the signal on the HI-LO edge of the clock signal, and compare them to the initial reading and turn an LED on or off depending on which direction the compass turned. I current have the clock input going into PIN_B0 on the PIC18F2525 and the compass signal going into PIN_A0. No LEDs turn on when i turn the compass either way. Is there anything that i am doing wrong here? Thanks in advance Smile
_________________
ashrau
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Mar 10, 2009 4:21 pm     Reply with quote

Without enabling global interrupts, no interrupt will be recognized.
ashrau



Joined: 25 Feb 2009
Posts: 9

View user's profile Send private message

PostPosted: Fri Mar 13, 2009 12:41 am     Reply with quote

Thanks a lot! The interrupt is working now. I really appreciate all your help Smile
_________________
ashrau
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