Teori dasar ADC bisa baca disini
// Sensor di simulasikan oleh variable resistor di hubungkan ke pin ADC chanel 0 (kaki 40 atmega8535)
//Adc : channel 0 (pin 40)
//Compiler: CodeVision
//mengambil data ADC dengan interupt ADC.
// hasil konversi resolusi 10 bit , 00 0000 0000 ~ 11 1111 1111 (3FFH)
#include <mega8535.h>
#include <delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0×15 ;PORTC
#endasm
#include <lcd.h>
// ADC interrupt service routine
interrupt [ADC_INT] void adc_isr(void)
{
unsigned int polutan; //
// 20ms delay
delay_ms(20);
// Start a new AD conversion
ADCSRA|=0×40;
//data 10 bit hasil konversi disimpan di ADCW
polutan = (unsigned int) ADCW; //rubah type data ADCW ke integer
if(polutan>500)
{ lcd_gotoxy(0,1);
lcd_putsf(“berbahaya “);
}
else
{
{ lcd_gotoxy(0,1);
lcd_putsf(“Tidak berbahaya”);
}
}
lcd_gotoxy(6,0);
lcd_putsf(“PPM”);
lcd_gotoxy(0,0);
// rubah angka integer ke char Ascii , misal angka 1 -> code ASCII =1 + 30 = 31H
lcd_putchar(polutan/1000 %10 + 0×30); //menampilkan digit ribuan
lcd_putchar(polutan/100 %10 + 0×30); //menampilkan digit ratusan
lcd_putchar(polutan/10 %10 + 0×30); //menampilkan digit puluhan
lcd_putchar(polutan %10 + 0×30); //menampilkan digit satuan
}
void main(void)
{
// ADC Interrupts: On
ADCSRA=0x8E;
// LCD module initialization
lcd_init(16);
lcd_gotoxy(6,0);
lcd_putsf(“PPM”);
// Global enable interrupts
#asm(“sei”)
// Select ADC input 0
ADMUX=0×40;
// Start the first AD conversion
ADCSRA|=0×40;
// All the job is done by ADC interrupts
while (1);
}
0 komentar:
Posting Komentar