- Wed Feb 02, 2011 2:17 pm
#119066
OK I am sure I came across this issue a while ago and never solved it and now it is back again.
I am using a PIC24F04KA200 and using the ADC to read a simple analogue voltage from a normal 10k pot. That is it. Works perfect when plugged into the programmer, but if I unplug the programmer and power from battery (or even leave the programmer plugged in but disconnect the data programming pin of the ICSP (also the pin I am reading the ADC level from, and cannot change this) then the reading floats.
Powering up without the programmer or as described above, at first the level will remain correct, but as soon as the pot is moved changin the voltage, the value will read randomly. Sometimes it reads correctly but most of the time its noisy.
Plugging it back into the programmer and works 100%.
I have 0.1uF cap on the Vdd and Vss pins and the MCLR set to external with the 10k, 330R and 0.1uF cap as described to do so in the datasheet.
The circuit contains nothing else other than a transistor to come on at a certain level (but that isn't even touched for this test code so won't be doing anything).
The board and code couldn't be simpler yet cannot figure out what is causing it - presumably the code:
I am using a PIC24F04KA200 and using the ADC to read a simple analogue voltage from a normal 10k pot. That is it. Works perfect when plugged into the programmer, but if I unplug the programmer and power from battery (or even leave the programmer plugged in but disconnect the data programming pin of the ICSP (also the pin I am reading the ADC level from, and cannot change this) then the reading floats.
Powering up without the programmer or as described above, at first the level will remain correct, but as soon as the pot is moved changin the voltage, the value will read randomly. Sometimes it reads correctly but most of the time its noisy.
Plugging it back into the programmer and works 100%.
I have 0.1uF cap on the Vdd and Vss pins and the MCLR set to external with the 10k, 330R and 0.1uF cap as described to do so in the datasheet.
The circuit contains nothing else other than a transistor to come on at a certain level (but that isn't even touched for this test code so won't be doing anything).
The board and code couldn't be simpler yet cannot figure out what is causing it - presumably the code:
Code: Select all
adc.c
================================
#define FCY 4000000UL
#include <p24fxxxx.h>
#include <libpic30.h>
void ADCInitialize(int inputs)
{
AD1PCFG = inputs ^ 0x1C3F; // select analog input pin passed in, making all others digital
AD1CON1 = 0x00E0; // automatic
AD1CSSL = 0; // no scanning required
AD1CON2 = 0; // use MUXA, AVss and AVdd are used as Vref+/-
AD1CON3 = 0x1F00; // Tad = 32; Tcy = 500ns (must be >75ns)
AD1CON1bits.ADON = 1; // turn on the ADC
}
int ADCReadAuto(int ch)
{
AD1CHS = ch; // 1. select analog input channel
AD1CON1bits.SAMP = 1; // 2. start sampling
while (!AD1CON1bits.DONE); // 3. wait for the conversion to complete
return ADC1BUF0; // 4. read the conversion result
}
main.c
===========================
void Initialize()
{
// 8Mhz clock
// No clock dividing (default is 4Mhz)
CLKDIV = 0x3000;
TRISA = 0; // All outputs
TRISB = 0;
PORTA = 0;
PORTB = 0;
AD1PCFG = 0x1FFF; // All digital I/Os (top 3 bits band gaps and vregs)
// Set pot pin to ground
_RB4 = 0;
}
int main()
{
Initialize();
// Indicate power-up
while (1)
{
while (1)
{
// Update the current trigger levels
_TRISB4 = 1;
ADCInitialize(2);
unsigned int mRTLevel = ADCReadAuto(2);
if (mRTLevel > 100)
BLIPLED();
}
}
}