Thanks both of u , finally it is working ..
here is my code any fineness is required just tell me
All i have observed the o/p on led
now i have 2 work on taking this data to the LCD
CODE:
IC16F882 INTERNAL ADC USING MIKROC
unsigned int result1,temp_result;
unsigned int read_ad(char);
unsigned char acquisition_time = 3U;
void main()
{
OSCCON=0X61; // INTERNAL CLOCK FREQ 4MHZ,SCS Internal oscillator is used for system clock
TRISA=0XFF; // ADC INPUT PORTA
TRISB=0; //ADC O/P PORTB FOR LOWER BITS (0-7) USING RIGHT JUSTIFIED
TRISC=0; // ADC O/P HIGHER BITS (8-9)
ANSEL=0x10; //ANS4 Analog i/p
ANSELH=0; //Configure other AN pins as digital I/O
PORTB=0X00; // make o/p ports clear
PORTC=0X00; // make o/p ports clear
C1ON_BIT=0; // disable comparator
C2ON_BIT=0;
do
{
temp_result=read_ad(4); //Read the adc function
// send the 0-7 bits of adc result to the portb ( LSB 1st) *
PORTB=temp_result; // portb will contain the result of adc (1-8)lsb 1st
PORTC=temp_result>>8; // send the 8-9 bits of adc result to the portc(RC0,RC1) (MSB END)
}while(1);
}
/* calling function*/
unsigned int read_ad(unsigned char channel)
{
unsigned int result = 0U;
unsigned char acquisition_time = 3U;
ADCON1 = 0x80U; /* 10 bit Right justified result, Vdd as ref */
ADCON0 = 0x81U; /* Conversion clock Fosc/32 */
ADCON0 |= (unsigned char)(channel << 2U); /* Select channel */
while(acquisition_time--) /* Sample channel */
{;
}
GO_DONE_BIT = 1U;
//while((ADIF&0x01)&(GO_DONE_BIT&0x00))
//while(ADIF&0x01)/* Wait for conversion end */
while(GO_DONE_BIT) // use this for mikroC
{
;
}
result1 = ADRESH;
result1 <<= 8U;
result1 |= ADRESL;
return result1;
}