View previous topic :: View next topic |
Author |
Message |
ashrau
Joined: 25 Feb 2009 Posts: 9
|
external clock triggering adc sampling |
Posted: Mon Mar 09, 2009 8:17 pm |
|
|
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
ashrau _________________ ashrau |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Mar 10, 2009 12:04 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
ashrau
Joined: 25 Feb 2009 Posts: 9
|
|
Posted: Tue Mar 10, 2009 12:26 am |
|
|
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 .
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 |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Mar 10, 2009 5:06 am |
|
|
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(). |
|
![](templates/subSilver/images/spacer.gif) |
ashrau
Joined: 25 Feb 2009 Posts: 9
|
|
Posted: Tue Mar 10, 2009 2:16 pm |
|
|
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](images/smiles/icon_smile.gif) _________________ ashrau |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Mar 10, 2009 4:21 pm |
|
|
Without enabling global interrupts, no interrupt will be recognized. |
|
![](templates/subSilver/images/spacer.gif) |
ashrau
Joined: 25 Feb 2009 Posts: 9
|
|
Posted: Fri Mar 13, 2009 12:41 am |
|
|
Thanks a lot! The interrupt is working now. I really appreciate all your help ![Smile](images/smiles/icon_smile.gif) _________________ ashrau |
|
![](templates/subSilver/images/spacer.gif) |
|