Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Analog to Digital conversion?

Status
Not open for further replies.
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. :)
 
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.
 
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? :D
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top