Hi. I am trying to make an ADC program to test the ADC function of the PIC16F877 microcontroller. I have got the code to compile, but it doesn't seem to work. The code seem to enter the while(GO) loop but it doesn't exit. This is strange as the Go bit from ADCON0 is suppose to be cleared once the ADC process is finished. So I am wondering either something is stalling the ADC process or the ADC process take over 2 min to run...
#include <system.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//Target PIC16F877 configuration word
#pragma DATA _CONFIG, _PWRTE_ON & _BODEN_OFF & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _HS_OSC & _CP_OFF
//Set clock frequency
#pragma CLOCK_FREQ 20000000
void interrupt( void )
{
//Handle timer0 interrupt
if( intcon & (1<<T0IF) )
{
clear_bit( intcon, T0IF ); //clear timer 0 interrupt bit
}
//Handle timer1 interrupt
if( pir1 & (1<<TMR1IF) )
{
clear_bit( pir1, TMR1IF ); //clear timer 1 interrupt bit
}
//Handle timer2 interrupt
if( pir1 & (1<<TMR2IF) )
{
clear_bit( pir1, TMR2IF ); //clear timer 2 interrupt bit
}
}
int convert_table[10] = {512,256,128,64,32,16,8,4,2,1};
void main( void )
{
//Configure port A
trisa = 0x10;
//Configure port B
trisb = 0x00;
//Configure port C
trisc = 0x00;
//Configure port D
trisd = 0x00;
//Configure port E
trise = 0x00;
//Configure A/D pins
adcon0 = 0x04;
adcon1 = 0x8E;
//Initialize port A
porta = 0x00;
//Initialize port B
portb = 0x00;
//Initialize port C
portc = 0x00;
//Initialize port D
portd = 0x00;
//Initialize port E
porte = 0x00;
//Set Timer0 mode
clear_bit( option_reg, T0CS ); //configure timer0 as a timer
//Set prescaler assignment
clear_bit( option_reg, PSA ); //prescaler is assigned to timer0
//Set prescaler rate
clear_bit( option_reg, PS2 ); //prescaler rate 1:2
clear_bit( option_reg, PS1 );
clear_bit( option_reg, PS0 );
//Set timer0 source edge selection
set_bit( option_reg, T0SE ); //increment on high-to-low transition on RA4/T0CKI pin
//Set timer 1 prescaler rate
clear_bit( t1con, T1CKPS1 ); //prescaler rate 1:1
clear_bit( t1con, T1CKPS0 );
//Set timer 1 mode
clear_bit( t1con, TMR1ON ); //disable timer 1
//Set timer 2 prescaler rate
clear_bit( t2con, T2CKPS1 ); //prescaler rate 1:1
clear_bit( t2con, T2CKPS0 );
//Set timer 2 postscaler rate
clear_bit( t2con, TOUTPS3 ); //postscaler rate 1:1
clear_bit( t2con, TOUTPS2 );
clear_bit( t2con, TOUTPS1 );
clear_bit( t2con, TOUTPS0 );
//Set timer 2 mode (enable or disable)
clear_bit( t2con, TMR2ON ); //enable timer 2
//Enable interrupts (Timer0)
intcon = 0xA0;
int check;
//Endless loop
while( 1 )
{
adcon0 = 11000101b; //turn on ADC
while (NOT_DONE) //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
if ((ADRESL & 0x01b)== 0x01b) check += convert_table[0]; // convert binary to digital
if ((ADRESL & 0x02b)== 0x02b) check += convert_table[1];
if ((ADRESL & 0x04b)== 0x04b) check += convert_table[2];
if ((ADRESL & 0x08b)== 0x08b) check += convert_table[3];
if ((ADRESL & 0x10b)== 0x10b) check += convert_table[4];
if ((ADRESL & 0x20b)== 0x20b) check += convert_table[5];
if ((ADRESL & 0x40b)== 0x40b) check += convert_table[6];
if ((ADRESL & 0x80b)== 0x80b) check += convert_table[7];
if ((ADRESL & 0x01b)== 0x01b) check += convert_table[8];
if ((ADRESL & 0x02b)== 0x02b) check += convert_table[9];
if (check > 511) //if equal or bigger than 2.5 Volts
{
portb = 11111111b;
}
else
{
portb = 00000000b;
}
}
}