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.

ADC 16f877 code beginner help

Status
Not open for further replies.

maria258

New Member
i am new using microcontrollers. at the moment im starting using 16f877 and need to start by programming the adc (10 bit). all i did for now is the code shown below. is it enough to read data? or does it need alteration?
i am using mplab to compile and proteus for schematics.
thanks
maria



#include <htc.h>
#include <pic.h>

unsigned int data;

void main(void)
{
//configuring input/outputs
PORTA = 0x00;
TRISA = 0xFF;
TRISB = 0x00;

//configuring ADC
ADCON0=0b00000001 //setting ADON on
ADCON1=0b10000000 //ADFM is set right


while(1)
{
ADCON0|= GODONE; //start conversion
{
break
}

if (ADRESH == 1)
{
data = ADRESL + 256;
}
else if (ADRESH == 2)
{
data = ADRESL + 512;
}
else if (ADRESH == 3)
{
data = ADRESL + 768;
}
else if (ADRESH == 0)
{
data = ADRESL;
}
}
}
 
You don't mention what speed your chip will be running at and so the value for Fosc division is unknown. To be safe set it to Fosc/32. You also don't need to combine the registers the way you have it, you can do it like,
Code:
void main(void){
    //configuring input/outputs
    PORTA = 0x00;
    TRISA = 0xFF;
    TRISB = 0x00;
    
    //configuring ADC
    ADCON0=0b10000001 //setting ADON on and Fosc/32
    ADCON1=0b11000000 //ADFM is set right and Fosc/32
    while(1){
        GODONE=1;		//start conversion
        while(GODONE);		//wait for it to complete
        data=ADRESH*256+ADRESL;	//get answer
    }
}
Note, you have to wait for the conversion to complete.

You will also need to set a config value to use it in an actual chip.

Also, when posting code use the code tags or type [code] before it and [/code] after it.

Mike.
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top