|


Gambar Portable Water Meter
Hal yang paling penting dalam membuat program adalah proses pengulangan.
Banyak sekali perintah pengulangan yang bisa dipakai pada pemrograman C berbasis AVR, antara lain while dan for.
berikut adalah cuplikan sebuah function yang menggunakan perintah yang teramat mudah untuk dipahami yakni menggunakan goto
char* key_value(unsigned char x,unsigned char y,unsigned char sz) //nyoba function { unsigned long int a1=0; unsigned char a,b,d; char e; char* c="12345678901234567890"; lcd_gotoxy(x,y); lcd_putsf(" "); lcd_gotoxy(x,y); lcd_putchar('-'); a=0; d=0; xx: a1++; if(a1==1) { lcd_gotoxy(0,3); lcd_putsf("Key D to Save "); } if(a1==40000) { lcd_gotoxy(0,3); lcd_putsf(" Key C to Cansel"); } if(a1==80000) { a1=0; } keybord();
b=keyb; if(b!=0) { if((b<='9')&(d<sz)&(b>='0')) { d++; lcd_gotoxy(x+a,y); lcd_putchar(b); lcd_gotoxy(x+d,y); lcd_putchar('-'); c[d]=b; lstr=d; } a++; if(b=='C') { //no ok c=''; lstr=0; goto ok; } if(b!='D') { goto xx; } else { // simpan data goto ok; } } goto xx; ok: return c; }
Sedangkan Procedure berikut adalah menampilkan data angka kedalam LCD yang telah diurai antara satuan,puluhan,ratusan untuk detailnya silahkan simak programnya sebagai berikut
void run_test(void) { unsigned long int b,a; unsigned int sat,pul,rat,rib,prib,rrib,jut,c; unsigned char* s1; unsigned long int lta; unsigned int msat,mpul; y: lcd_clear(); lcd_gotoxy(0,0); lcd_putsf("1. OPEN VALVE"); lcd_gotoxy(0,1); lcd_putsf("2. CLOSE VALVE"); lcd_gotoxy(0,2); lcd_putsf("3. EXIT"); lcd_gotoxy(0,3); lcd_putsf("Option = 0"); x: keybord(); if(keyb==0) { goto x; } lcd_gotoxy(9,3); lcd_putchar(keyb); if(keyb=='3') { lcd_clear(); main_lcd(); goto xx; } if(keyb=='2') { goto y; }
if(keyb=='1') { a=0; c=0; lcd_clear(); lcd_gotoxy(0,0); lcd_putsf("Pulse :"); lcd_gotoxy(0,1); lcd_putsf("WATER VOLUME"); lcd_gotoxy(0,2); lcd_putsf("=> Ltr"); keyb=0; while((a<1000000)&(keyb==0)) { a++; b=a; sat=b%10; //sisa pembagian 1234/10=123.4 sisa 0.4 b/=10; pul=b%10; b/=10; rat=b%10; b/=10; rib=b%10; b/=10; rrib=b%10; b/=10; prib=b%10; b/=10; jut=b%10; s1[1]=jut+0x30; s1[2]=prib+0x30; s1[3]=rrib+0x30; s1[4]=rib+0x30; s1[5]=rat+0x30; s1[6]=pul+0x30; s1[7]=sat+0x30; lcd_gotoxy(9,0);lcd_putchar(s1[1]); lcd_gotoxy(10,0);lcd_putchar(s1[2]); lcd_gotoxy(11,0);lcd_putchar(s1[3]); lcd_gotoxy(12,0);lcd_putchar(s1[4]); lcd_gotoxy(13,0);lcd_putchar(s1[5]); lcd_gotoxy(14,0);lcd_putchar(s1[6]); lcd_gotoxy(15,0);lcd_putchar(s1[7]); c++;
//liter b=a*100; b=b/pulsa; msat=b%10; //sisa pembagian 1234/10=123.4 sisa 0.4 b/=10; mpul=b%10; b/=10; s1[3]=mpul+0x30; s1[4]=msat+0x30; lcd_gotoxy(10,2);lcd_putchar(s1[3]); lcd_gotoxy(11,2);lcd_putchar(s1[4]);
b=a/pulsa; sat=b%10; //sisa pembagian 1234/10=123.4 sisa 0.4 b/=10; pul=b%10; b/=10; rat=b%10; b/=10; rib=b%10; b/=10; rrib=b%10; b/=10; prib=b%10; s1[1]=prib+0x30; s1[2]=rrib+0x30; s1[3]=rib+0x30; s1[4]=rat+0x30; s1[5]=pul+0x30; s1[6]=sat+0x30; lcd_gotoxy(3,2);lcd_putchar(s1[1]); lcd_gotoxy(4,2);lcd_putchar(s1[2]); lcd_gotoxy(5,2);lcd_putchar(s1[3]); lcd_gotoxy(6,2);lcd_putchar(s1[4]); lcd_gotoxy(7,2);lcd_putchar(s1[5]); lcd_gotoxy(8,2);lcd_putchar(s1[6]); lcd_gotoxy(9,2);lcd_putchar('.');
delay_ms(10); keybord(); } goto y; } goto x; xx: }
Berikut ini cuplikan procedure keypad matrik 4X4| TUGAS AKHIR
void keybord(void) { keyb=0; PORTB.0=1; //2k2 ke + out PORTB.1=0; PORTB.2=0; PORTB.3=0; if(PIND.3==1){keyb='D';while(PIND.3!=0);} //10k ke ground in if(PIND.4==1){keyb='C';while(PIND.4!=0);} if(PIND.5==1){keyb='B';while(PIND.5!=0);} if(PIND.6==1){keyb='A';while(PIND.6!=0);} PORTB.0=0; PORTB.1=1; PORTB.2=0; PORTB.3=0; if(PIND.3==1){keyb='#';while(PIND.3!=0);} if(PIND.4==1){keyb='9';while(PIND.4!=0);} if(PIND.5==1){keyb='6';while(PIND.5!=0);} if(PIND.6==1){keyb='3';while(PIND.6!=0);} PORTB.2=1; PORTB.1=0; PORTB.0=0; PORTB.3=0; if(PIND.3==1){keyb='0';while(PIND.3!=0);} if(PIND.4==1){keyb='8';while(PIND.4!=0);} if(PIND.5==1){keyb='5';while(PIND.5!=0);} if(PIND.6==1){keyb='2';while(PIND.6!=0);} PORTB.3=1; PORTB.1=0; PORTB.2=0; PORTB.0=0; if(PIND.3==1){keyb='*';while(PIND.3!=0);} if(PIND.4==1){keyb='7';while(PIND.4!=0);} if(PIND.5==1){keyb='4';while(PIND.5!=0);} if(PIND.6==1){keyb='1';while(PIND.6!=0);} if(keyb!=0) { delay_ms(10); } }
UNTUK APLIKASI PENGGERAK MOTOR DC PWM ADALAH CARA AMPUH UNTUK MENGATASINYA.
berikut contoh program sederhana dari PWM

/***************************************************** This program was produced by the CodeWizardAVR V1.25.3 Standard Automatic Program Generator © Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com
Project : Version : Date : 10/13/2009 Author : F4CG Company : F4CG Comments:
Chip type : ATmega32 Program type : Application Clock frequency : 11.000000 MHz Memory model : Small External SRAM size : 0 Data Stack size : 512 *****************************************************/
#include <mega32.h> #define pwmKi PORTC.0 #define pwmKa PORTC.1 #define mKi_plus PORTC.2 #define mKi_min PORTC.3
#define mKa_plus PORTC.4 #define mKa_min PORTC.5
unsigned char pwmKi_val,pwmKa_val,x = 0;
// Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) { // Place your code here TCNT0 = 0xC0;
x++;
if (x>=pwmKa_val) { pwmKa=0; } else { pwmKa=1; } if (x>=pwmKi_val) { pwmKi=0; } else { pwmKi=1; }
}
// Declare your global variables here
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 // 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 PORTD=0x00; DDRD=0x00;
// Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 42.969 kHz // Mode: Phase correct PWM top=FFh // OC0 output: Disconnected TCCR0=0x44; 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; 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=0x01;
// Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00;
// Global enable interrupts #asm("sei")
while (1) { // Place your code here pwmKa_val = 0xff; pwmKi_val = 0x20; delay_ms(1500); pwmKa_val = 0x20; pwmKi_val = 0xff;
}; }
|