djc
Advanced Member level 1
- Joined
- Jan 27, 2013
- Messages
- 402
- Helped
- 3
- Reputation
- 6
- Reaction score
- 2
- Trophy points
- 1,298
- Location
- India
- Activity points
- 4,554
Hi,
Here is the code for ADC using 16f72.
Here is the code for ADC using 16f72.
Code:
/****************************************************************
1) RA3 is selected as input channel
2) Knob is connected to RA3 pin on kit, by varying it we can have ADC result which is displayed on PORTB.
3) GO/DONE bit must be set before monitoring it for going low.
******************************************************************/
void main() {
unsigned int temp,i;
//TRISA = 0xff; //Set port A direction as i/p
TRISA = 0x00; //Port A as output
PORTA = 0X00; //ALL BITS OFF
ADCON0 = 0X18; //CLOCK SELECTION AND ANALOG CHANNEL SELECTION channel RA3 as input
TRISB = 0x00; //Set port B as output
TRISC = 0x00;
ADCON1 = 0X01; //ALL CHANNELS AS ANALOG INPUT Vref is RA3
//ADCON1 = 0x00; //Vref is VDD
TRISA3_bit = 0; //Port A 0th bit as input
TRISA2_bit = 0;
TRISA1_bit = 1;
TRISA0_bit = 1;
PORTB = 0xFF;
Delay_ms(1000);
PORTB = 0x00;
Delay_ms(1000);
PORTC = 0x00;
while(1)
{
ADRES = 0x00;
ADCON0.ADON = 0x01; //Turn on the ADC
ADCON0.GO = 1;
//ADCON0 = ADCON0 & 0x04;
//while((ADCON0 & 0x04)==1); // Wait till conversion is going on
while (ADCON0 == 0x04);
//while(ADCON0.GO == 1);
ADCON0 = 0x00; // Stop the ADC
// ADIF = 0;
//temp = ADRES;
PORTB = ADRES;
//PORTA = ADRES;
//PORTC = ADRES;
Delay_us(2); //Call delay 2 microseconds;
}
}