|
|
View previous topic :: View next topic |
Author |
Message |
pebbert9
Joined: 31 Dec 2010 Posts: 39
|
adc wake from sleep |
Posted: Sat Feb 11, 2012 5:56 pm |
|
|
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: 9221 Location: Greensville,Ontario
|
|
Posted: Sat Feb 11, 2012 8:47 pm |
|
|
pretty sure you have to enable global interrupts to get ANY enabled interrupts to work... |
|
|
pebbert9
Joined: 31 Dec 2010 Posts: 39
|
|
Posted: Sat Feb 11, 2012 9:48 pm |
|
|
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
|
|
Posted: Sat Feb 11, 2012 10:33 pm |
|
|
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
|
|
Posted: Sat Feb 11, 2012 10:51 pm |
|
|
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 |
|
|
|
|
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
|