lampiran a source code program mikrokontroler atmega 8535...
TRANSCRIPT
45
LAMPIRAN A
Source Code Program Mikrokontroler ATMega 8535
dengan bahasa C
Chip type : ATmega8535
Program type : Application
Clock frequency : 11.059200 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 128
*****************************************************
/
#include <mega8535.h>
#include <delay.h>
#include <stdio.h>
#include <math.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
#define ADC_VREF_TYPE 0x40
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Start the AD conversion
ADCSRA|=0x40;
46
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
// Declare your global variables here
char baca[33],baca1[33],i,j;//,g,h,k,l;
//milik adc+kalibrasi
float a,b,c,d,e,f,g,h,k,l;//x,y,z;
eeprom float *memori;//,*suhu1, *keruh1;//, *angka3, *angka4;
void tombol1()
{
if (PIND.0==0){i++;delay_ms(300);};
if (PIND.1==0){i--;delay_ms(300);};
if (i>=4){i=0;};
j=1+i;
}
void tombol2()
{
if (PIND.0==0){i++;delay_ms(300);};
if (PIND.1==0){i--;delay_ms(300);};
if (i>=2){i=0;};
j=1+i;
}
void tampilan()
{
menu:
lcd_clear();
while(1)
{
47
tombol1();
switch(j)
{
case 1 : lcd_gotoxy(0,0);
lcd_putsf(">Suhu Kekeruhan");
lcd_gotoxy(0,1);
lcd_putsf(" S+K Lain ");
if (PIND.2==0){delay_ms(300);goto suhu;};
break;
case 2 : lcd_gotoxy(0,0);
lcd_putsf(" Suhu >Kekeruhan");
lcd_gotoxy(0,1);
lcd_putsf(" S+K Lain ");
if (PIND.2==0){delay_ms(300);goto
kekeruhan;};
break;
case 3 : lcd_gotoxy(0,0);
lcd_putsf(" Suhu Kekeruhan");
lcd_gotoxy(0,1);
lcd_putsf(">S+K Lain ");
if (PIND.2==0){delay_ms(300);goto sk;};
break;
case 4 : lcd_gotoxy(0,0);
lcd_putsf(" Suhu Kekeruhan");
lcd_gotoxy(0,1);
lcd_putsf(" S+K >Lain ");
if (PIND.2==0){delay_ms(300);goto lain;};
break;
}
}
suhu:
lcd_clear();
while(1)
{
48
a=read_adc(0);
a=read_adc(0);
b=((float)5/1024);
c=((float)(a*b));
d=((float)112.38*c-3.506);
lcd_gotoxy(0,0);
lcd_putsf(" besar suhu ");
lcd_gotoxy(0,1);
sprintf(baca," %5.3f\xdf C ",d);
lcd_puts(baca);
delay_ms(300000000000);
}
kekeruhan:
lcd_clear();
while(1)
{
tombol2();
switch(j)
{
case 1 : lcd_gotoxy(0,0);
lcd_putsf(">Kalibrasi ");
lcd_gotoxy(0,1);
lcd_putsf(" Mulai ukur ");
if (PIND.2==0){delay_ms(300);goto
otomatis;};
if (PIND.3==0){delay_ms(300);goto menu;};
break;
case 2 : lcd_gotoxy(0,0);
lcd_putsf(" Kalibrasi ");
lcd_gotoxy(0,1);
lcd_putsf(">Mulai ukur ");
if (PIND.2==0){delay_ms(300);goto ukur;};
if (PIND.3==0){delay_ms(300);goto menu;};
49
break;
}
}
sk:
lcd_clear();
while(1)
{
//suhu
a=read_adc(0);
a=read_adc(0);
b=((float)5/1024);
c=((float)(a*b));
d=((float)112.38*c-3.506);
//kekeruhan
e=((float)*memori);
f=read_adc(1);
f=read_adc(1);
g=((float)5/1024);
h=((float)(f*g));
k=((float)187.42*h-606.24);
l=((float)k-e);
lcd_gotoxy(0,0);
sprintf(baca," %5.3f\xdf C ",d);
lcd_puts(baca);
lcd_gotoxy(0,1);
sprintf(baca1," %5.3f NTU ",l);
lcd_puts(baca1);
delay_ms(3000);
}
50
lain:
lcd_clear();
while(1)
{
tombol1();
switch(j)
{
case 1 : lcd_gotoxy(0,0);
lcd_putsf(">ADC Off Zero");
lcd_gotoxy(0,1);
lcd_putsf(" V.in NTU.asli");
if (PIND.2==0){delay_ms(300);goto adc;};
if (PIND.3==0){delay_ms(300);goto
kekeruhan;};
break;
case 2 : lcd_gotoxy(0,0);
lcd_putsf(" ADC >Off Zero");
lcd_gotoxy(0,1);
lcd_putsf(" V.in NTU.asli");
if (PIND.2==0){delay_ms(300);goto zero;};
if (PIND.3==0){delay_ms(300);goto
kekeruhan;};
break;
case 3 : lcd_gotoxy(0,0);
lcd_putsf(" ADC Off Zero");
lcd_gotoxy(0,1);
lcd_putsf(">V.in NTU.asli");
if (PIND.2==0){delay_ms(300);goto vin;};
if (PIND.3==0){delay_ms(300);goto
kekeruhan;};
break;
case 4 : lcd_gotoxy(0,0);
lcd_putsf(" ADC Off Zero");
lcd_gotoxy(0,1);
lcd_putsf(" V.in >NTU.asli");
51
if (PIND.2==0){delay_ms(300);goto
ntuasli;};
if (PIND.3==0){delay_ms(300);goto
kekeruhan;};
break;
}
}
//================== milik kekeruhan
================== //
otomatis:
lcd_clear();
while(1)
{
//a=read_adc(1);
//a=read_adc(1);
lcd_gotoxy(0,0);
lcd_putsf(" kalibrasi nol ");
lcd_gotoxy(0,1);
lcd_putsf(" tunggu ");
delay_ms(1000);
lcd_gotoxy(0,0);
lcd_putsf(" proses ");
delay_ms(500);
lcd_gotoxy(0,1);
lcd_putsf(" 33% ");
delay_ms(500);
lcd_gotoxy(0,1);
lcd_putsf(" 66% ");
delay_ms(500);
lcd_gotoxy(0,1);
delay_ms(500);
lcd_putsf(" 99% ");
delay_ms(500);
52
lcd_gotoxy(0,1);
lcd_putsf(" 100% ");
a=read_adc(1);
a=read_adc(1);
b=((float)5/1024);
c=((float)(a*b));
d=((float)187.42*c-606.24);
*memori=(float)d;
lcd_gotoxy(0,1);
lcd_putsf(" Ok ");
delay_ms(500);
goto kekeruhan;
}
ukur:
lcd_clear();
while(1)
{
a=read_adc(1);
e=((float)*memori);
a=read_adc(1);
b=((float)5/1024);
c=((float)(a*b));
d=((float)187.42*c-606.24);
f=((float)d-e);
lcd_gotoxy(0,0);
lcd_putsf(" Nilai NTU ");
lcd_gotoxy(0,1);
sprintf(baca," %5.3f NTU ",f);
lcd_puts(baca);
delay_ms(5000);
}
//==============================================
=================//
53
//================== milik lain ================== //
adc:
lcd_clear();
while(1)
{
a=read_adc(1);
a=read_adc(1);
lcd_gotoxy(0,0);
lcd_putsf(" Nilai ADC ");
lcd_gotoxy(0,1);
sprintf(baca,"ADC= %d ",read_adc(1));
lcd_puts(baca);
delay_ms(1000);
}
zero:
lcd_clear();
while(1)
{
c=(float)*memori;
lcd_gotoxy(0,0);
lcd_putsf(" Nilai memori ");
lcd_gotoxy(0,1);
sprintf(baca," %5.3f ",c);
lcd_puts(baca);
delay_ms(5000);
}
vin:
lcd_clear();
while(1)
{
a=read_adc(1);
a=read_adc(1);
54
b=((float)5/1024);
c=((float)a*b);
lcd_gotoxy(0,0);
lcd_putsf(" Besar tegangan ");
lcd_gotoxy(0,1);
sprintf(baca," %2.3f Volt ",c);
lcd_puts(baca);
delay_ms(5000);
}
ntuasli:
lcd_clear();
while(1)
{
a=read_adc(1);
a=read_adc(1);
b=((float)5/1024);
c=((float)(a*b));
d=((float)187.42*c-606.24);
lcd_gotoxy(0,0);
lcd_putsf(" Nilai NTU ");
lcd_gotoxy(0,1);
sprintf(baca," %5.3f ",d);
lcd_puts(baca);
delay_ms(5000);
}
//==============================================
=================//
55
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T
State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T
State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
56
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In
Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=P State2=P
State1=P State0=P
PORTD=0x0F;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
57
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 691.200 kHz
// ADC Voltage Reference: AVCC pin
// ADC High Speed Mode: Off
// ADC Auto Trigger Source: None
58
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;
SFIOR&=0xEF;
// LCD module initialization
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf(" Alat Ukur ");
delay_ms(500);
lcd_gotoxy(0,1);
lcd_putsf("Suhu ");
delay_ms(500);
lcd_gotoxy(0,1);
lcd_putsf("Suhu + ");
delay_ms(500);
lcd_gotoxy(0,1);
lcd_putsf("Suhu + kekeruhan");
delay_ms(500);
lcd_clear();
delay_ms(300);
while (1)
{
// Place your code here
tampilan();
};
}