|
|
View previous topic :: View next topic |
Author |
Message |
saran Guest
|
external interrupt + adc read |
Posted: Wed Jul 04, 2007 3:31 am |
|
|
Hi to everybody ,
i am new to this CCS compiler ,
I have a doubt in my program,
where i am using external interrupt and ADC reading operation.
where the interrupt occurs witihn a very duration .. say25 interrupts per second ,but the interrupt is working fine when it is executed seperately,but when i need to read the adc channel 0 for every 20ms , the interrupt is not executing , only the adc values are stored ,
can anyone help,...
#include <18F4455.h>
#device ADC=10
#fuses NOWDT,HS,NOPROTECT,PUT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
static int16 a,b,v1;
#INT_EXT
void ext_isr(){
a++;
printf("\n\rpulses counted=%lu",a);
}
void main()
{
printf("program running\n\r");
while(1)
{
ext_int_edge(L_TO_H); // init interrupt triggering for button press
enable_interrupts(INT_EXT);// turn on interrupts
enable_interrupts(GLOBAL);//*/
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
set_adc_channel(0);
v1=read_adc();
printf("voltage=%lu\n\r",V1);
}
} |
|
|
Ttelmah Guest
|
|
Posted: Wed Jul 04, 2007 4:12 am |
|
|
First, use the 'code' buttons to post code. Makes it much easier to read!.
Now, some comments in the code:
Code: |
#include <18F4455.h>
#device ADC=10
#fuses NOWDT,HS,NOPROTECT,PUT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
//global variables are always static.
int16 a,b,v1;
int1 count_update=false;
#INT_EXT
void ext_isr(){
a++;
//This is a problem.
//The print, will take about 20mSec to execute - means counts _will
//be missed if they are faster than this...
//Also, because you then use a 'print' in both the interrupt, and
//the main, interrupts will be disabled during the print in the main...
//printf("\n\rpulses counted=%lu",a);
//So just set a flag, and do the print in the main
count_update=true;
}
void main() {
int16 delay_for;
printf("program running\n\r");
//Interrupt eges, and setup, should only be done _once_
ext_int_edge(L_TO_H); // init interrupt triggering for button press
clear_interrupts(INT_EXT); //otherwise changing the interrupt
//edge, may result in an 'extra' interrupt
enable_interrupts(INT_EXT);// turn on interrupts
enable_interrupts(GLOBAL);//*/
//ADC_CLOCK INTERNAL, is _not_ recommended for accurate readings
//unless used with 'sleep'.
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(ALL_ANALOG);
set_adc_channel(0);
while (TRUE) {
//You need to pause somewhere here:
for (delay_for=0;delay_for<1000;delay_for++) {
delay_us(5);
if (count_update) {
count_update=false;
printf("\n\rpulses counted=%lu",a);
}
}
//This will give a reading about every 20mSec, with the printing time
//and the loop overheads.
v1=read_adc();
printf("voltage=%lu\n\r",V1);
}
}
|
Best Wishes |
|
|
saran Guest
|
still the same problem |
Posted: Wed Jul 04, 2007 5:37 am |
|
|
Thank u for ur reply
But stiil the same problem exists
Interrupt routine is not working properly |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jul 04, 2007 8:04 am |
|
|
Code: | setup_adc_ports(ALL_ANALOG); | INT_EXT is connected to pin B0. Check your datasheet for this pin, it can be configured for multiple purposes of which one is analog input nr12. The pin can only be used for one function, digital input with interrupt or as an analog input, not both at the same time.
Check the 18F4550.h header file for all possible configuration settings and choose one better fitting your hardware. |
|
|
|
|
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
|