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.

PIC18F87J60 MikroC ADC question

Status
Not open for further replies.

MattQ

New Member
Hey, so I'm pretty new to MikroC, but I can't for the life of me figure out why this isn't working. All it's displaying is 0xFF, regardless of my analog input. The code compiles just fine, but the output isn't changing. Thoughts? Thanks.

unsigned int ADC_Result;

void main() {
CMCON = 0x03;
ADCON1 = 0x30; // set all to digital, this to replace ANSEL/ANSELH
TRISA = 0x00;
TRISC = 0;
TRISB = 0;
do {
ADC_Result = ADC_Read(2); // Get 10-bit results of AD conversion
PORTB = ADC_Result; // Send lower 8 bits to PORTB
PORTC = ADC_Result >> 8; // Send 2 most significant bits to RC1, RC0
} while(1);

}
 
1. Make sure the pins are input and also ANALOG
try this:

Code:
void main() {
    CMCON = 0x03;
    ADCON1 =[COLOR="Red"] 0x00[/COLOR]; // set all to digital, this to replace ANSEL/ANSELH
    TRISA = [COLOR="Red"]0b00000100[/COLOR]; //Bit 2 is input now.
    TRISC = 0;
    TRISB = 0;
    do {
        ADC_Result = ADC_Read(2); // Get 10-bit results of AD conversion
        PORTB = ADC_Result; // Send lower 8 bits to PORTB
        PORTC = ADC_Result >> 8; // Send 2 most significant bits to RC1, RC0
    } while(1);

The 0x00 is for using AVSS and AVDD and all ANALOG
The 0b00000100 is for setting TRISA BIT 2 to input. Since thats the ADC pin it has to be input to read data.

The 0b00000100 can also be replaced by hex 0x4 or simply decimal 4
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top