|
|
View previous topic :: View next topic |
Author |
Message |
aasief
Joined: 12 Feb 2009 Posts: 12 Location: cape town
|
Sleep while adc conversion |
Posted: Mon Jun 22, 2009 6:03 am |
|
|
Hi Everyone
I'm using the onboard adc on the pic 16F877. But when it takes reading it gives alot of noise on the power line. I've been told to put the pic in sleep mode during adc conversion but I'm having trouble.
This was my code before:
Code: |
#include"16f877.h"
#device ADC=16
#use delay(clock=4000000)
#define use_portb_lcd
#include"lcd.c"
void main()
{
float temp,bintemp,tempsp,bintempsp;
int x;
lcd_init();
set_tris_d(255);
lcd_gotoxy(1,1);
printf(lcd_putc" Thermo ");
lcd_gotoxy(1,2);
printf(lcd_putc"PowerUp");
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0_AN1_VSS_VREF);
delay_ms(2000);
while(1)
{ for(x=0;x<100;x++)
{
set_adc_channel(1);
delay_ms(10);
bintemp=read_adc();
temp=(bintemp/65535)*50;
Tempr: if(!input(pin_d0)) //testing menu button
{ delay_ms(50);
while(input(pin_d0)) //
{
lcd_gotoxy(1,1);
tempsp=(bintempsp/65535)*50;
printf(lcd_putc "SetPoint");
lcd_gotoxy(1,2);
printf(lcd_putc"+/-%2.0f'C",tempsp);
if(!input(pin_d1)) //testing increment button
{ delay_ms(100);
if(tempsp<=50)bintempsp=bintempsp+1311; //testing max temp limit
};
if(!input(pin_d3)) //testing decrement button
{ delay_ms(100);
if(tempsp>=0)bintempsp=bintempsp-1311; //testing min temp limit
};
}
}
}
lcd_gotoxy(1,1);
printf(lcd_putc"T=%2.1f'C ",temp);
lcd_gotoxy(1,2);
printf(lcd_putc"SP=%2.0f'C ",tempsp);
if(bintempsp<bintemp)output_high(pin_c0),output_low(pin_c1);
if(bintempsp>bintemp)output_high(pin_c1),output_low(pin_c0);
}
}
|
Can I use the "sleep_ulpw1(15000)" instruction on the 877 pics? |
|
|
Ttelmah Guest
|
|
Posted: Mon Jun 22, 2009 7:34 am |
|
|
Sleep_ulpw, is for use with the ultra low power 'wake up' pin, not the ADC.
Your chip doesn't have this pin, so 'no'.
Now, to use 'sleep' for the ADC, is a good idea, but it only reduces noise from the PIC. If your noise is really bad, it sounds as if you may have other problems....
The way to do it is as follows:
1) Select the internal ADC clock (you already have this).
2) Seletc the required channel.
3) Disable the global interrupt.
4) Enable the ADC interrupt.
5) Clear the ADC interrupt.
6) Trigger an ADC _conversion only_ cycle.
7) Sleep.
8) When you wake, read the value.
The code for this is:
Code: |
//For your initialisation
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0_AN1_VSS_VREF);
//Since you are only reading one ADC channel that I can see,
//include the selection here
set_adc_channel(1);
//Now for the actual reading: - include in your loop.
disable_interrupts(GLOBAL); //If you are using any other interrupts
//You also need to disabl3e the other interrupt sources if used.
enable_interrupts(INT_ADC); //We want the ADC to wake from sleep
clear_interrupts(INT_ADC);
read_adc(ADC_START_ONLY); //Trigger the ADC to start
sleep();
delay_cycles(1); //A 'NOP' instruction - this is 'prefetched' when asleep.
//We will get here, when the ADC interrupt triggers.
bintemp=read_adc(ADC_READ_ONLY); //get the value read while
//chip was asleep.
//If other interrupts are being used, re-enable them here.
|
The noise from the PIC itself, is not normally that bad. It will currently be being made worse, by using ADC_CLOCK_INTERNAL, which is _only_ designed to be used when the chip is asleep, or running at very low speeds.
Also, look carefully at your Vref design. The load on the Vef pin, changes when a reading is made, and unless the reference can handle this, you will get errors from this...
Best Wishes |
|
|
aasief
Joined: 12 Feb 2009 Posts: 12 Location: cape town
|
|
Posted: Mon Jun 22, 2009 8:04 am |
|
|
THANK YOU VERY MUCH Ttelmah.
I've been looking in many books and in the ccs manual but it doesn't explain that well. Your explanation is much easier.
|
|
|
|
|
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
|