0% found this document useful (0 votes)
11 views4 pages

Listing Program

This document contains C code for an embedded system using an ATmega16 microcontroller to read temperature data from an LM35 sensor via ADC and display it on an alphanumeric LCD. It includes functions for initializing registers, reading ADC values, and updating the temperature display. The main loop continuously updates the temperature reading and displays it on the LCD if there is a change in the ADC value.

Uploaded by

smithjhonny111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Listing Program

This document contains C code for an embedded system using an ATmega16 microcontroller to read temperature data from an LM35 sensor via ADC and display it on an alphanumeric LCD. It includes functions for initializing registers, reading ADC values, and updating the temperature display. The main loop continuously updates the temperature reading and displays it on the LCD if there is a change in the ADC value.

Uploaded by

smithjhonny111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#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(" KELOMPOK 4");
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;
}

You might also like