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 CCS Technical Support

problems with A/D interrupts

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








problems with A/D interrupts
PostPosted: Fri Dec 03, 2004 7:33 am     Reply with quote

I need to use A/D interrupt to measure a value of voltage from variable resistance to find its max or min while I'm doing another task.
But, my code may be wrong. I can only hear the song but PIC doesn't give a value of max and min to me.

I'm not sure about int_ad and ad_isr.
What I use in the code is correct ??

thank you Very Happy

Code:

#device ADC=10

boolean hook_ad=false;
void ad_isr();

#int_ad
void ad_isr()
{
   hook_ad = true;
}

void main()
{
   long duration;
   int16 i, value, min, max, avg, rate;

   enable_interrupts(GLOBAL);
   enable_interrupts(INT_AD);

   setup_port_a(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);     // PIN 2

      min=1023;
      max=0;

   if(hook_ad)
   {
         value = Read_ADC();
         printf(lcd_putc, "%ld", value);
         delay_ms(500);
         printf(lcd_putc, "\f");
         if(value<min)
            min=value;
         if(value>max)
            max=value;
         hook_ad=false;
   }
   else
   {
         PlaySong(200);
   }
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Dec 03, 2004 7:50 am     Reply with quote

You have to tell the ADC to start a conversion. See the example in the manual for the read_adc command.

also: setup_port_a() is an obsolete function name, the new name is SETUP_ADC_PORTS
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Dec 03, 2004 8:03 am     Reply with quote

You problem is that you PIC has went to bed Very Happy The problem is not telling the adc to start. The default for ADC_READ() is ADC_START_AND_READ which you don't want.

Code:

#device ADC=10

boolean hook_ad=false;
void ad_isr();

#int_ad
void ad_isr()
{
   hook_ad = true;
}

void main()
{
   long duration;
   int16 i, value, min, max, avg, rate;

   // Setup the port first
   setup_port_a(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);     // PIN 2

   enable_interrupts(INT_AD);
   enable_interrupts(GLOBAL);


      min=1023;
      max=0;

   // Start the initial conversion
   Read_ADC(ADC_START_ONLY);

   while(1)
   {
     if(hook_ad)
     {
           // Only read the value
           value = Read_ADC(ADC_READ_ONLY);
           printf(lcd_putc, "%ld", value);
           delay_ms(500);
           printf(lcd_putc, "\f");
           if(value<min)
              min=value;
           if(value>max)
              max=value;
           hook_ad=false;
           // Start the next conversion
           Read_ADC(ADC_START_ONLY);
     }
     else
     {
           PlaySong(200);
     }
   }
}
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Fri Dec 03, 2004 2:49 pm     Reply with quote

You may want to see my other thread:

http://www.ccsinfo.com/forum/viewtopic.php?t=21119
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Dec 03, 2004 4:44 pm     Reply with quote

future wrote:
You may want to see my other thread:

http://www.ccsinfo.com/forum/viewtopic.php?t=21119


Am I missing something? That doesn't even use interrupts.
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Fri Dec 03, 2004 5:18 pm     Reply with quote

The way the previous code is structured, using an inline function will work the same way without using interrupts.

Using them, it will have to go through all the interrupt dispatcher (almost 100 instructions) just to set a flag and still handle the read in main().

I think it is much like the thread I commented.

He says "while doing another task", we know that this is not possible. So call the function twice with a delay between calls and its done.

Depending on the PIC clock he is using, probably hook_ad is always true and the read will be limited by the main loop speed... so use an inline function and get higher speeds without spending time in the dispatcher.

This is just my opinion and how I would solve this problem.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Dec 03, 2004 5:43 pm     Reply with quote

I see it as an exercise for using ints Wink
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