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 support@ccsinfo.com

ADC not reading value

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



Joined: 04 May 2009
Posts: 35
Location: India

View user's profile Send private message

ADC not reading value
PostPosted: Fri Nov 05, 2010 12:44 am     Reply with quote

The below code is written to read main voltage through PIC16F876A ADC
and store it in a variable, then it compares the stored voltage with a reference voltage stored in another variable and if the read value is greater than stored value than the controller will perform some action based on the given condition.

But in here the problem is that the adc is not reading the voltage, I have checked the hardware its all correct.
Code:

#include <16F876A.h>

#device adc=10               
#fuses XT,HS,WDT,NOPUT,NOPROTECT
#use delay(clock=20000000, RESTART_WDT)
#use fast_io(A)

unsigned int32 set_point=511,inv_out=0;
//-->(1023==300V)
//-->511==150V

void main()
{
set_tris_a(0XFF);
   
setup_adc_ports(AN0_AN1_AN3);                   //Setting ADC Channels
setup_adc(ADC_CLOCK_DIV_64);

set_adc_channel(1);      //using channel 1
delay_us(10);              //acquisition delay
inv_out=read_adc();

if(inv_out>set_point){
   delay_ms(5000);
   disable_interrupts(GLOBAL);
   delay_ms(5000);
   enable_interrupts(GLOBAL);
  }

}

In the above code voltage read is always above 150V and still the condition is false, whereas when I set the value of "inv_out" variable to "600" manually than the condition turns out to be true.

So I think the inv_out variable is not getting any value at execution.
Need suggestions what could be wrong.

Regards.
Ttelmah



Joined: 11 Mar 2010
Posts: 19348

View user's profile Send private message

PostPosted: Fri Nov 05, 2010 3:15 am     Reply with quote

Several comments:

1) Get your clock fuses right. Currently you are selecting both 4MHz or less oscillator, and >4MHz oscillator. That the chip runs at all, is 'luck'.

2) Do the same with the interrupts. You must _never_ enable the global interrupt without an interrupt handler present. Doing so, has a high risk of simply crashing the code.

3) For your chip, the ADC Tacq, is 20uSec, not 10uSec. This will only make things less accurate, but correct it.

4) You don't show any form of loop, as shown, the code won't actually 'do' anything to show that it is working....

Best Wishes
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