Microcontroller › PIC › Temperature Sensor : Interfacing LM35 and LCD Display to PIC18F4550 › help me rectifying error in
March 10, 2013 at 7:12 pm
#9289
himanshu singh
Participant
help me rectifying error in this code for ADC
// program to read analog input from TEMPERATURE SENSOR AND PRODUCE EQUIVALENT OUTPUT AND PRINT IT ON LCD
#include <avr/io.h>
#include<util/delay.h>
#include “lcd.h”
#include “lcd.c”
#define F_CPU 1000000UL
void main()
{
char ch; // declaring ch as character variable of length 4, used to select 1 channel out of 7
unsigned long int adc_result; // declaring adc_result variable as unsigned integer used to to store adc result which willbe passed onto lcd
lcd_init (LCD_DISP_ON);
lcd_clrscr() ;
initADC (); // initialise adc funtion
lcd_gotoxy(0,0);
lcd_puts(“ADC TEST”);
lcd_gotoxy(0,1);
lcd_puts(“ADC= “);
while (1)
{
adc_result= ReadADC(); // store adc result in adc_result variable
sprintf(ch,”%4d”,adc_result); // to print this result in lcd, first store it in ch variable
lcd_gotoxy(4,1);
lcd_puts(ch); // display the result on lcd
_delay_ms(100);
}
}
void initADC() // ADC INITIALISATION FUNCTION
{
ADMUX=(1<<REFS0)|(1<<REFS1); // VOLTAGE REFERENCE AS 2.56
ADCSRA= (1<<ADEN)|(1<<ADPS1)|(1<<ADPS0);// ENABLING ADC AND SELECTING PRESCALING OF 8
}
unsigned long int ReadADC(void) // ADC READING FUNCTION, RETURN VALUE IS OF 16 BITS SO UINT16_T AND CHANNEL LENGTH AS INPUT ARGUMENT
{
// enabling 0to 7 channels
ADMUX|= (1<<MUX0);
ADCSRA |= (1<<ADSC); // start adc conversion
while (!(ADCSRA & (1<<ADIF))); // wait until conversion is complete
ADCSRA|= (1<<ADIF); // clear adif flag after completion, yes its 1 to clear it
return (ADC); // return the digital output
}