Hi,
I am presently trying to build up a project on a temperature sensor with the help of LM35DZ using an atmega16 uC and trying to display the result on a 16x2 LCD...I have compiled a code but its not working(since I'm a complete noob)...I am posting the code in this thread..Please correct me,any help will be highly appreciated..
put the circuit also........
**broken link removed**
Which compiler did you try?
It will not work in AVR-GCC (winavr) , your code is written for codevisionAVR
Alex
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } // for example unsigned int my_adc_result; // variable to store the result my_adc_result = read_adc(0); // make the conversion for channel 0 and read the result
In this code i cant find the integer which the ADC has converted and display it on the LCD..
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } // for example unsigned int my_adc_result; // variable to store the result my_adc_result = read_adc(0); // make the conversion for channel 0 and read the result
unsigned int my_adc_result; // variable to store the result
my_adc_result = read_adc(0); // make the conversion for channel 0 and read the result
/*
About Program:
Program LM35 AVR ADC 10bit :)
Program ini akan menampilkan temperature dengan satuan CELCIUS yang didapat
dari sensor temperature LM35. LM35 dihubungkan ke PINA.0 atau ADC 0
dalam aplikasi real, mungkin diperlukan buffer dan bisa dibuat dari
opamp single supply misal LM358. Jangan lupa kalo mo dibuat beneran, PIN AVCC
ADC dihubungkan ke 5V yg stabil, terus PIN AREF dikasih BYPASS CAPASITOR ke GND
100nF sudah cukup.
perhitungan pake 1024 (jumlah total data 10bit) dan bukan 1023 (jumlah maksimal
data dari 10bit). Dibaca aja di keterangan dalam program..
Happy Programming^^
Herlambang Aribowo
#####################################################################
Compiler: CodeVisionAVR V2.03.9
Chip : ATmega16 X-tall 4Mhz
FUSE: H:C9 L:2F Brownout@4.5V,WDT ON,BOD ON,SPI EN,X-tall High Freq.
FUSE: H:C9 L:AF Brownout@2.7V,WDT ON,BOD ON,SPI EN,X-tall High Freq.
#####################################################################
*/
#include <mega16.h>
#include <delay.h>
#include <stdio.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#define ADC_VREF_TYPE 0xC0 //Internal
unsigned char buffer[16]; //Untuk buffer sprintf
float temperature; //Untuk data perhitungan temperature (float)
unsigned int temporary; //Untuk temporary data ADC (komparasi)
void updatetemp(void);//Fungsi untuk pengolahan data ADC ke temperature
void sett_regs(void); //Fungsi untuk setting register di awal proses
unsigned int read_adc(unsigned char adc_input);//Fungsi baca ADC
// Main function
void main(void){
//--------------- Setting IO and peripheral at startup!
sett_regs();
_lcd_ready();
//--------------- Time for going CRAZY (looping forever)..
for(;;){ //mainloop looping forever until ERROR/POWER SHUTDOWN/RST/WDT ACTIVE
#asm("wdr") //kick your DOG before DEAD! hwahwa... :D
updatetemp();
#asm("nop")
};
}
void updatetemp(void){
if (temporary != read_adc(0)){//bila ada data baru, tampilkan
//ADC = ( Vin * 1024 / Vref )
//misal, temperature LM35 = 30°C, Vin = 300mV = 0.3V,
//jadi nilai ADC = (.3*1024/2.56)=120, 120/30 = 4
//Persamaan bisa disederhanakan dengan output data ADC/4
temperature = read_adc(0)/4; //hanya untuk Vref 2.56
lcd_gotoxy(0,0);
lcd_putsf("WWW.NUBIELAB.COM");
lcd_gotoxy(0,1);
sprintf(buffer,"SUHU: %0.1f%cC ",temperature,0xDF);
lcd_puts(buffer);
temporary = read_adc(0);
delay_ms(100);
}
}
// Function for setting register
void sett_regs(void){
// Port a initialization
PORTA=0x00;DDRA=0x00;
// Port b initialization
PORTB=0x00;DDRB=0x00;
// Port c initialization
PORTC=0x00;DDRC=0x00;
// Port D initialization
PORTD=0x00;DDRD=0x00;
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: Free Running
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA2;
SFIOR&=0x1F;
// LCD module initialization
lcd_init(16);
ACSR=0x80;
SFIOR=0x00;
// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/2048k
#pragma optsize-
WDTCR=0x1F;
WDTCR=0x0F;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
}
// Routine baca ADC
// Jangan lupa AVCC ke +5V biar ADCnya bekerja :D
// Connected a 0.1uF bypass capacitor at AREF pin as per datasheet,
// “Internal 2.56V Voltage Reference with external capacitor at AREF pin”.
unsigned int read_adc(unsigned char adc_input){
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
if (temporary != read_adc(1))
{
//ADC = ( Vin * 1024 / Vref )
//example, temperature LM35 = 30°C, Vin = 300mV = 0.3V,
//so the value ADC = (.3*1024/5)=61.44, 61.44/30 = 2.048
//The equation can be simplified by output data ADC/2.048
[B]temperature = read_adc(1)/2.048; //only for Vref 5[/B] // [B][COLOR="#FF0000"]NOTE1[/COLOR][/B]
lcd_gotoxy(2,0);
lcd_putsf("Temperature=");
lcd_gotoxy(6,1);
sprintf(buffer,"%d%cC",temperature,0xDF);
lcd_puts(buffer);
temporary = read_adc(1);
delay_ms(100);
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?