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

adc wake from sleep

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



Joined: 31 Dec 2010
Posts: 39

View user's profile Send private message

adc wake from sleep
PostPosted: Sat Feb 11, 2012 5:56 pm     Reply with quote

Hello,

I am trying to get this code to wake from sleep after the adc conversion.

It works fine if I don't go to sleep and just do a read_adc().

Does anyone see any obvious errors?

Thank you,

Code:
/*
    adc_sleep_test.c
*/

#include <18F24K22.h>
#device ICD=TRUE
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPROTECT,PUT,BROWNOUT,NODEBUG,NOHFOFST
#use delay(internal=16Mhz)
#use rs232(debugger)

void main()
{
   long temperature;
   
   setup_vref (VREF_2v048);
   
   setup_adc(ADC_CLOCK_DIV_16 | ADC_TAD_MUL_16);
   setup_adc_ports(sAN11 | VSS_FVR);
   
   enable_interrupts(INT_AD);
   
   While (TRUE)
   {
      set_adc_channel(11);
      delay_ms(1);
      clear_interrupt(INT_AD);
     
      read_adc(ADC_START_ONLY);
      sleep();
      delay_cycles(1);
      temperature=read_adc(ADC_READ_ONLY);
     
      //temperature=read_adc();
      printf("Temperature:%ld\r\n", temperature);
      delay_ms(1000);
     
   }// End While(TRUE)
}// End main()
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Feb 11, 2012 8:47 pm     Reply with quote

pretty sure you have to enable global interrupts to get ANY enabled interrupts to work...
pebbert9



Joined: 31 Dec 2010
Posts: 39

View user's profile Send private message

PostPosted: Sat Feb 11, 2012 9:48 pm     Reply with quote

I have tried enabling globals but it still doesn't work.

I saw some posts which stated that globals had to be disabled before going to sleep, is this true?

Any other ideas?

Code:
/*
    adc_sleep_test.c
*/

#include <18F24K22.h>
#device ICD=TRUE
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPROTECT,PUT,BROWNOUT,NODEBUG,NOHFOFST
#use delay(internal=16Mhz)
#use rs232(debugger)

void main()
{
   long temperature;
   
   setup_vref (VREF_2v048);
   
   setup_adc(ADC_CLOCK_DIV_16 | ADC_TAD_MUL_16);
   setup_adc_ports(sAN11 | VSS_FVR);
   
   enable_interrupts(INT_AD);
   enable_interrupts(GLOBAL);
   
   While (TRUE)
   {
      set_adc_channel(11);
      delay_ms(1);
      clear_interrupt(INT_AD);
     
      read_adc(ADC_START_ONLY);
      sleep();
      delay_cycles(1);
      temperature=read_adc(ADC_READ_ONLY);
     
      //temperature=read_adc();
      printf("Temperature:%ld\r\n", temperature);
      delay_ms(1000);
     
   }// End While(TRUE)
}// End main()
pebbert9



Joined: 31 Dec 2010
Posts: 39

View user's profile Send private message

PostPosted: Sat Feb 11, 2012 10:33 pm     Reply with quote

After looking at the data sheet some more, I saw that you must use ADC_CLOCK_INTERNAL when doing the conversion in sleep.

The following code is working now.

Code:
/*
    adc_sleep_test.c
*/

#include <18F24K22.h>
#device ICD=TRUE
#device ADC=10
#fuses INTRC_IO,NOWDT,NOPROTECT,PUT,BROWNOUT,NODEBUG,NOHFOFST
#use delay(internal=16Mhz)
#use rs232(debugger)

void main()
{
   long temperature;
   
   setup_vref (VREF_2v048);
   
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_adc_ports(sAN11 | VSS_FVR);
   
   enable_interrupts(INT_AD);
   
   While (TRUE)
   {
      set_adc_channel(11);
      delay_ms(1);
      clear_interrupt(INT_AD);
     
      read_adc(ADC_START_ONLY);
      sleep();
      delay_cycles(1);
      temperature=read_adc(ADC_READ_ONLY);
     
      printf("Temperature:%ld\r\n", temperature);
      delay_ms(1000);
     
   }// End While(TRUE)
}// End main()
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

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

PostPosted: Sat Feb 11, 2012 10:51 pm     Reply with quote

The data sheet for your micro will show which interrupts need to be enabled to wake up. Follow the path from your peripheral through to the wake up signal.

In my experience, from the hardware point of view, global interrupts do not need to be enabled to wake up, but to wake up from an analog read, peripheral interrupts need to be enabled along with ADC interrupts.

The catch is that CCS have merged peripheral interrupts in with global interrupts. IOW, to enable peripheral interrupts, you need to enable global interrupts.

For a project using a 12F675 I didn't want to enable global interrupts, but still wanted to wake up after an analog read. To do that I used the code:
Code:
#define PERIPHERAL 0x0B40
...
enable_interrupts(PERIPHERAL);
I figured that out by noting the values in the 12f675.h header file corresponded to address ("0B") and bit mask ("40") as specified in the data sheet.
_________________
Andrew
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