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.

PIC16F877 ADC stalled?

Status
Not open for further replies.
currently, my code seem to do the endless loop and the while(GO) loop correctly.... however, the output seem to stay constant regardless of what the input is....

#include <system.h>

//Target PIC16F877 configuration word
#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _HS_OSC & _CP_OFF

//Set clock frequency
#pragma CLOCK_FREQ 20000000

void main( void )
{
//Configure port A - E
trisa = 0x2F;
trisb = 0x00;
trisc = 0x00;
trisd = 0x00;

//Initialize port A - E
portd = 0x00;
porta = 0x00;
portc = 0x00;
portb = 0x00;

//Configure A/D pins
adcon0 = 10011001b; //Set clock Fosc/32, ch1(AN0), GO = 0, ADC Operating
adcon1 = 0x80; //Right Justified, All ADC pins on

delay_ms(100);
// intcon = 0x00;

// int check;
volatile unsigned int ADC_VALUE;

// ADIE = 0; /* Masking the interrupt */
// ADIF = 0; /* Resetting the ADC interupt bit */
ADC_VALUE = 0000000000b;

while( 1 ) //Endless loop
{

adcon0 = 10001101b; //turn on ADC
while ((adcon0 & 0x04) == 0x04) //waiting until ADC conversion is finished
{
portd = 11111111b;
}
portd = 00000000b; //test that ADC exits since Go bit suppose to change to 0 when ADC finishes
delay_ms(1);
ADC_VALUE = ADRESL;
ADC_VALUE += (ADRESH << 8);
if (ADC_VALUE.9 == 1) //if equal or bigger than 2.5 Volts
{
portb = 11111111b;
}
else
{
portb = 00000000b;
}
portc = 11111111b;
// portb = ADRESH;
portc = 00000000b;
// delay_ms(1);
}
}
 
Is your PIC really running at 20Mhz? Because the diagram of the dev board shows a 4Mhz crystal. If it's at 4Mhz, you should change your #pragma CLOCK_FREQ and adcon0 setups to reflect this. Shouldn't stop the ADC from working though it'll be slower.
I am supplying the input through a power supply unit where i can adjust the voltage...
It's hard to see on the diagram exactly where AN0 (Pin2) of the PIC goes to on the DEV board. I assume the input to the pin never went above 5V. If your power supply surged or supplied a voltage higher than 5V, then the AN0 input is fried if you didn't use a series resistor (4K7 is a good value). If so, try a different input pin.
 
Thank you for your help... I finally got it working... I copied a piece of coding from an ADC sample.... now my ADC is working correctly... don't know why it is working though.... I had my TA's check it out and they have no idea why it is working either as the sample code that i copied should do what i had written anyway..... so while it is now working, i am still puzzled by why it is working...
 
Could be some weird compiler bug. I guess you could look at the disassembly listing and try to figure it out. It's good you got it going; happy programming!
 
Status
Not open for further replies.

Latest threads

Back
Top