MIKROKONTROLLER 1 SESI 4 BASIC C
Struktur penulisan program #include < [library1.h] > // library eksternal #include < [library2.h] > #define [nama1] [nilai] ; // variabel tetap #define [nama2] [nilai] ; [global variables] // Opsional [functions] // Opsional int main(void) // Program Utama { [Deklarasi local variable/constant] [Isi Program Utama] }
Preprocessor Mendefinisikan macro yang digunakan yang akan digunakan dalam proses kompilasi Commands dengan ‘#’. Abbreviated list: #define : defines a macro #undef : removes a macro definition #include : insert text from file #if : conditional based on value of expression #ifdef : conditional based on whether macro defined #ifndef : conditional based on whether macro is not defined #else : alternative #elif : conditional alternative defined() : preprocessor function: 1 if name defined, else 0 #if defined(__NetBSD__)
Headers # include <header.h> # include “header.h” #include akan mengarahkan preprosessor untuk menggunakan fungsi-fungsi yang ada di dalamnya Header merupakan file yang berisi fungsi-fungsi tertentu yang bisa langsung digunakan.
Headers Standard header yang sering digunakan: <stdio.h>: Standard IO facilities <avr/interrupt.h>: Interrupts <avr/io.h>: AVR device-specific IO definitions <avr/wdt.h>: Watchdog timer handling <util/delay.h>: Convenience functions for busy-wait delay loops <avr/pgmspace.h>: Program Space Utilities
Tipe data
Deklarasi variabel dan konstanta [tipe data] [nama] = [nilai] ; int a = 4; int i; unsigned char c; const [nama] = [nilai] ; const x = 5; #define #define satu 2 #define LED PORTB
Operator Logic
Level Operator
If-statement Syntax: if( condition) { statement……. } else
Contoh program dengan if-statement Int a=4; Int b=5; If(a>b) fan (ON); else fan (OFF);
Do- while statement Syntax: Initial counter Do { statement……. update statement } While(condition);
Program dengan do-while Int a=4; Do { a++; } while(a>5);
For- statement Syntax: Program: For( initial counter; test condition; update stmt) { statement…….. statement……... } Program: for(int i=0;i<5;i++) sprintf(str, “Hello Robosapiens”);
Infinite Looping Infinite looping merupakan perulangan yang dijalankan secara terus menerus Syntax void main() { ...................//code program di luar infinite looping. Hanya dijalankan sekali while(1) //infinite looping ..................................// code program dalam infinite looping }
Infinite looping void main() { ...................//code program di luar infinite looping. Hanya dijalankan sekali for( ; ; ) //infinite looping ..................................// code program dalam infinite looping }
I/O Ports – Configuration and usage
C-Example Konfigurasi Pin B3 output, set output level ke VCC DDRB |= (1<<3); PORTB |= (1<<3); Pin D2 sebagai input dengan pullup DDRD &= ~(1<<2); PORTD |= (1<<2); uint8_t x = PIND & (1<<2); PC.5 sebagai output DDRC |= (1<<5); DDRC &= ~(1<<PC5);
Scanning Cek PD2 bernilai tinggi (high) atau rendah if (PIND & (1<<PD2)) if (PIND & 0x04) if(PIND.2==1)