pic16f8761 adc

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
crazy88
Posts: 5
Joined: Fri Feb 22, 2008 12:20 pm
Contact:

pic16f8761 adc

Post by crazy88 »

Hello

I'm trying to use the ADC of a pic16f876A. What I would ideally want is the ability to alter the potentiometer onboard the v3 matrix dev board, and have this light led's accordingly. Is it possible to use the potentiometer as an input on the v3 board?

Is there any advice, or example code which would help me do this? I'm struggling to get the ADC setup properly.

Thanks

Paul

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pic16f8761 adc

Post by Benj »

Hello Paul

To retreive the output from the Potentiometer you first need to setup a sample for Analogue channel 1. This is done by configuring the ADCON0 and ADCON1 registers. You will also have to configure the appropriate tris register to make the I/O pin an Input. Make sure that the result of the conversion is setup to be left justified.

Before you start the sample by setting the Go bit, setup the channel and everything else and turn on the ADC module. Then wait for a few cycles to allow the ADC capacitor to charge up.

Once you have performed the sample, eg when the go bit returns to 0, the result can be retreived from the adresh register. For the full 10-bit result you will have to do some data manipulation to combine the two bytes correctly.

The PICmicro device datasheet should have everything you need to design the sample routine.

The jumper at J14 position 1 will have to be moved to the analogue position and then pot RV3 will be used to control the analogue signal.

If you wish to use the LDR instead then you will need to sample analogue channel 0 and again move the appropriate jumper on J14.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pic16f8761 adc

Post by Benj »

Code to Sample ADC channel 1 as a 8-bit byte.

char SampleADC(void)
{
char old_tris, cnt;

old_tris = trisa; //Store Old Tris
trisa = trisa | 0x02; //Convert Pin to Input
adcon1 = 0x04; //Set RA1 to analogue mode
adcon0 = 0x89; //turn ADC on and set to sample channel AN1

//wait the acquisition time
cnt = 0;
while (cnt < 40) cnt++;

//begin conversion and wait until it has finished
adcon0 = adcon0 | 0x04;
while (adcon0 & 0x04) ;

//restore old tris value, and reset adc registers
trisa = old_tris;
adcon1 = 0x07;
adcon0 = 0x80;

return adresh;
}

crazy88
Posts: 5
Joined: Fri Feb 22, 2008 12:20 pm
Contact:

Re: pic16f8761 adc

Post by crazy88 »

Thanks so much for the reply Benj, exactly what i was after! :) I now have RV3 turning the leds on gradually as i alter it.

THANKS :lol: :lol:

crazy88
Posts: 5
Joined: Fri Feb 22, 2008 12:20 pm
Contact:

Re: pic16f8761 adc

Post by crazy88 »

Hi, im wondering if you can help me again please.

I'm now trying to further the design here in this thread. I wish to implement an external analogue input (the RSSI pin of a ZigBee module), rather than the onboard potentiometer.

I'm struggling to get all the inputs, outputs and registers set up correctly. Which input pin of PORTA will i use? I tried using RA3, however, i am now struggling with my VREF+ and VREF-! I remembered to change my registers, and also switch J14 to position 3, but still no luck!

And advice would be really appreciated!

Thanks

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pic16f8761 adc

Post by Benj »

Hello

Are you using the VREF+ and VREF- pins or are you just wanting a sample based on the chips GND and VCC pins?

Also are you wanting an 8-bit result or the full 10-bits of resolution.

crazy88
Posts: 5
Joined: Fri Feb 22, 2008 12:20 pm
Contact:

Re: pic16f8761 adc

Post by crazy88 »

I have just been trying another method:

I am using the VREF+/- from the ZigBee IC. Basically, I am using the ZigBee Vcc as VREF+ and its GND as VREF- ...is this correct?

To do this i have my ADCON1 register set to 0x08; and ADCON0 to 0x89;

ZigBee Vcc is connected to RA3 (VREF+)
ZigBee GND is connected to RA2 (VREF-)
ZigBee RSSI is connected to RA1 (analogue i/p)

Will this clash with the onboard potentiometer?

I only need the 8bit resolution for now as I am trying to implement a system which detects when the ZigBee module is closest, and therefore RSSI is at its highest. When this happens the pic is to output a pulse to a laptop.

I have the whole system working with the onboard potentiometer, just struggling to implement this new analogue input.

Thanks

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pic16f8761 adc

Post by Benj »

Hello

Yes your VREF+ and VREF- should be connected correctly and your register values look about right.

However as you mentioned the potentiometer will affect the result of the sample. Maybe you could use a different channel. Eg AN4. Alternatively you could desolder the Pot from the board.

crazy88
Posts: 5
Joined: Fri Feb 22, 2008 12:20 pm
Contact:

Re: pic16f8761 adc

Post by crazy88 »

Am I right in thinking that the J14 jumper is only there to swap the analogue/digital inputs of RA0-3? And therefore if I use RA4 i do not need to alter any jumpers, just ensure that RA4 is set as an analogue input in the registers of my pic?

Thanks again for the help!!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: pic16f8761 adc

Post by Benj »

Hello

On the HP488 Dev board the digital I/O on PortA and PortB is connected to pull down resistors. The jumper disconnects these pull down resistors so that the analogue signal is clean. If you were to use RA4 then you would have to take the pull downs into account. You could if you wished use a device such as our ECIO to host the program as you can plug these modules straight into a prototype breadboard or your own circuitry. Alternatively you could use AN0 and remove the LDR from the circuit board or use AN1 and remove the pot.

Post Reply