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
pic16f8761 adc
- 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
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- 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
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;
}
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;
}
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: pic16f8761 adc
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
THANKS
Re: pic16f8761 adc
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
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
- 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
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: pic16f8761 adc
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
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
- 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
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: pic16f8761 adc
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!!
Thanks again for the help!!
- 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
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel