Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
1
MIKROKONTROLLER 1 SESI 4 BASIC C
2
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] }
3
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__)
4
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.
5
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
6
Tipe data
7
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
8
Operator Logic
9
Level Operator
10
If-statement Syntax: if( condition) { statement……. } else
11
Contoh program dengan if-statement
Int a=4; Int b=5; If(a>b) fan (ON); else fan (OFF);
12
Do- while statement Syntax: Initial counter Do { statement…….
update statement } While(condition);
13
Program dengan do-while
Int a=4; Do { a++; } while(a>5);
14
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”);
15
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 }
16
Infinite looping void main() {
//code program di luar infinite looping. Hanya dijalankan sekali for( ; ; ) //infinite looping // code program dalam infinite looping }
17
I/O Ports – Configuration and usage
18
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);
19
Scanning Cek PD2 bernilai tinggi (high) atau rendah
if (PIND & (1<<PD2)) if (PIND & 0x04) if(PIND.2==1)
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.