ss_blog_claim=726e70d7c87c20ae33aa7a61f06eb8aa

Wednesday, October 22, 2008

Code snippet for working with an ADC

We have already seen the need for an analog to digital converter and also how the conversion takes place, the below code is used to work with an ADC in my PIC development board where the PIC16F877A senses the input from an ADC through RA0 pin and displays the digital value in the LCD which is connected to PORTD and also send the converted data to PORTB.

#include<16f877a.h>
#use delay(clock=4000000)
#include

#byte portb=0x06
#byte porta=0x05
#byte portd=0x08
int16 temp;

main( )
{
lcd_init( ); //INITIALISE THE LCD ;
set_tris_b(0x00); //PORTB IS CONFIGURED AS A O/P;
set_tris_a(0x01); //PORTA IS CONFIGURED AS A I/P;
set_tris_d(0x00); //PORTD IS CONFIGURED AS A O/P;
setup_adc_ports(ra0_analog);
setup_adc(adc_clock_internal);
set_adc_channel(0);
while(1)
{
temp=read_adc(); //OUTPUT IS STORED IN TEMP;
portb=temp;
lcd_gotoxy(1,1);
printf(lcd_putc,"ADC:%4lx",temp); //DISPLAY ON LCD;
delay_ms(500);
}
}
Here you can see in the code that while configuring the pins of PORTA since we are going to sense the analog input from RA0 we hace configured it as an input (set_tris_a(0x01)).

No comments: