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 16F876A

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



Joined: 11 Nov 2006
Posts: 181
Location: Birmingham, UK

View user's profile Send private message

ADC 16F876A
PostPosted: Tue Jun 05, 2007 2:38 pm     Reply with quote

Hi,
I am trying to configure portA on the 16F876A so that AN0 and AN1 are analogue inputs. AN3 is fed by an external 2.5V reference. I want a voltage out of pin4 which I can vary with the setup_vref function (to vary LCD backlight intensity via suitable buffering). The code I have, which does not work properly but puts out a fixed 1.6V on p4 is:

int dac;
main()
{
setup_adc(ADC_CLOCK_DIV_64);
setup_adc_ports(AN0_AN1_VSS_VREF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
lcd_init(); //lcd on portC
dac = 0;
for(;;)
{
if(dac < 15)
dac++;
else
dac = 0;
//change voltage on pin 4
setup_vref(VREF_LOW | dac);
delay_ms(1000);
}//endfor
}

I have not found the compiler manual very clear at all and am confused!
Can anyone help please?

Thanks

Brian
Ttelmah
Guest







PostPosted: Tue Jun 05, 2007 3:08 pm     Reply with quote

Look at the define VREF_A2. Without this, the Vref is not output.

Best Wishes
Guest








PostPosted: Tue Jun 05, 2007 4:30 pm     Reply with quote

Hi Ttelmar,

Obvious when someone explains it!

As a teacher, I get really fed up with how poorly most help guides are put together - the authors just don't seem able to explain things clearly and simply and examples are often too complicated.

Anyway, thanks again.

Brian
Ttelmah
Guest







PostPosted: Wed Jun 06, 2007 2:47 am     Reply with quote

It is a common problem with the CCS stuff, and is made 'worse' by the way that some functions have several different actual 'behaviours' hidden under the skin.
The way to work with the PIC, and CCS, is to use three documents together:
First the PIC data sheet. Though you are working with a 'high level' language, you are dealing directly with the hardware of the chip, and there is no substitute for the data sheet.
Then the .h file for the processor.
Then the compiler manual.

Looking at your problem with these, you can see that to output the Vref voltage, bit 'CVROE' needs to be set (Register 13-1 in the data sheet). This is in the same physical register as the bits to set to select the high range/low range, and the bit to enable the ADC, so presumably would be accessed by the CCS setup_vref function.
Then going to the .h file, looking for the defines used in this function, you find the VREF_A2 define, which accesses the required bit, and though it has no 'comment' explaining what it does, the name itself suggests it's operation.

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