Analog to Digital conversion?

Status
Not open for further replies.

littletransistor

New Member
Hey there!

I've learned some A-D - and is it a way to let the A-D conversion to a maximum of 4-bits instead of 10-bits?

I searched the tutorials for the PK2 (Pic Kit 2) Lessons but didn't found anything.

Here's the code - I took some from Mike Predko's Evil Genius book and it's default to 10 bits. Anything I should add there?

Code:
#include <htc.h>

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT
& BORDIS & IESODIS & FCMDIS);

int main()
{
ANSEL = 0x01;
TRISC = 0x00;
PORTC = 0x00;
TRISA = 0xFF;
ADCON0 = 0b000000001;
ADCON1 = 0b000100000;

int ADCState = 0;
int ADCValue = 0;
int array[5] = {0x00, 0x01, 0x02, 0x04, 0x08};
int i=0;

while(1)
{
PORTC = ADCValue;

switch(ADCState)
{
case 0:
GODONE = 1;
ADCState++;
break;
case 1:
if(!GODONE)
ADCState++;
break;
case 2:
ADCValue = ADRESH;
ADCState = 0;
break;
}
}
}
Btw - the µC is a PIC 16F690.

3v0

Coop Build Coordinator
Forum Supporter
I am not sure why you would want a 4 bit result?

You can convert the 10 bit result to a 4 bit result by shifting it right 6 times.

You have two sources of information. The ADC section in the datasheet for the processor and the doucmentation for the ADC setup function for the C compiler.

Nigel Goodwin

Super Moderator
Most Helpful Member
I am not sure why you would want a 4 bit result?

You can convert the 10 bit result to a 4 bit result by shifting it right 6 times.
Or better still set the A2D to 8 bit mode, and shift right 4 times instead.

blueroomelectronics

Well-Known Member
Or left justify, swap the high byte and mask off the upper nibble.

littletransistor

New Member
Ok, got it.

But strangely, when I rammed up the potentiometer to max, it gives me only 1011 output (from the 4 LEDs, and the max output should be 1111). I'm not sure where's the last 3 number goes, but I have typed:

ADCValue = ADCValue >> 4

Is it the conversion too fast, or too slow, or something else?

Last edited:
Status
Not open for further replies.

Loading