Anda di halaman 1dari 20

Dasar Pemrograman C

Hendawan Soebhakti
Oktober 2009

Tujuan
Mampu

membuat program mikrokontroler dengan bahasa C Mampu menjelaskan struktur bahasa C

Sistem Mikrokontroler

Hendawan Soebhakti

Sub Pokok Bahasan


Library Fungsi Tipe

Data Konstanta Variable Konversi Tipe Data

Sistem Mikrokontroler

Hendawan Soebhakti

Struktur Program Bahasa C


/***************************************************** 2. Project : Tampilan LED 3. Version : 1 4. Date : 9/29/2009 Komentar 5. Author : Hendawan Soebhakti 6. Company : Politeknik Batam 7. Comments: 8. Menampilkan variasi nyala LED pada PORTA 9. *****************************************************/ 10. #include <mega8535.h> Preprocessor directive : Memasukkan file header 11. #include <delay.h> 12. #define on 1 Preprocessor directive : Mendefinisikan string 13. #define off 0 14. // Declare your global variables here Global Variable 15. unsigned char data; // Global Variable 16. void Geser(void) // Function 17. { 18. int i; // Local Variable 19. data=0x01; 20. for (i=0;i<8;i++) Fungsi 21. { data=data<<1; 22. PORTA=data; 23. delay_ms(1000); 24. Mikrokontroler }; Sistem Hendawan Soebhakti 4 25. }
1.

Struktur Program Bahasa C


26. 27. 28. 29. 30. 31.
32.

void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port A initialization // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0

33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47.

PORTA=0x00; DDRA=0xFF; . . . while (1) { // Place your code here Geser(); PORTA.0=on; delay_ms(1000); PORTA.0=off; delay_ms(1000); }; }
Hendawan Soebhakti

Fungsi main

Sistem Mikrokontroler

Library

Library adalah paket instruksi/program yang sudah jadi untuk memudahkan programmer. Library dapat digunakan oleh program setelah kita memasukkan header file dengan menggunakan preprocessor #include, misalnya : /* Header files are included before using the functions */ #include <math.h> // for abs #include <stdio.h> // for putsf void main(void) { int a,b; a=-99; /* Here you actually use the functions */ b=abs(a); // Function in math.h putsf(Hello world); // Function in stdio.h }

Sistem Mikrokontroler

Hendawan Soebhakti

Library
File library dapat dilihat pada C:\cvavr2\lib. Library yang disediakan oleh CodeVisionAVR adalah : Character Type Functions Standard C Input/Output Functions Standard Library Functions Mathematical Functions String Functions Variable Length Argument Lists Macros Non-local Jump Functions BCD Conversion Functions Gray Code Conversion Functions Memory Access Functions Delay Functions LCD Functions LCD Functions for displays with 4x40 characters LCD Functions for displays connected in 8 bit memory mapped mode

Sistem Mikrokontroler

Hendawan Soebhakti

Library

I2C Bus Functions National Semiconductor LM75 Temperature Sensor Functions Dallas Semiconductor DS1621 Thermometer/Thermostat Functions Philips PCF8563 Real Time Clock Functions Philips PCF8583 Real Time Clock Functions Dallas Semiconductor DS1302 Real Time Clock Functions Dallas Semiconductor DS1307 Real Time Clock Functions 1 Wire Protocol Functions Dallas Semiconductor DS1820/DS18S20 Temperature Sensors Functions Dallas Semiconductor DS2430 EEPROM Functions Dallas Semiconductor DS2433 EEPROM Functions SPI Functions Power Management Functions

Sistem Mikrokontroler

Hendawan Soebhakti

Tipe Data

Berikut ini adalah tabel semua tipe data yang disupport CodeVisionAVR C compiler :
Type bit char unsigned char signed char int short int unsigned int signed int long int unsigned long int signed long int float double Size (Bits) 1 8 8 8 16 16 16 16 32 32 32 32 32 Range 0,1 -128 to 127 0 to 255 -128 to 127 -32768 to 32767 -32768 to 32767 0 to 65535 -32768 to 32767 -2147483648 to 2147483647 0 to 4294967295 -2147483648 to 2147483647 1.175e-38 to 3.402e38 1.175e-38 to 3.402e38
9

Sistem Mikrokontroler

Hendawan Soebhakti

Variable

Variable dapat berupa global variable, yaitu variable yang dapat diakses oleh semua fungsi dalam program atau local variable yaitu variable yang hanya dapat diakses didalam fungsi yang mendeklarasikan variable tersebut. Jika tidak diinisialisasi, global variable secara otomatis akan bernila 0 saat program mulai dijalankan/startup. Local variable tidak secara otomatis diinisialisasi saat fungsi dipanggil. Syntaksnya adalah sbb : [<storage modifier>] <type definition> <identifier>;

Sistem Mikrokontroler

Hendawan Soebhakti

10

Variable
Contoh : /* Global variables declaration */ char a; int b; /* and initialization */ long c=1111111; void main(void) { /* Local variables declaration */ char d; int e; /* and initialization */ long f=22222222; }

Sistem Mikrokontroler

Hendawan Soebhakti

11

Konstanta

Konstanta adalah variable yang diisi sekali dan tidak dapat diubah nilainya. Konstanta akan tersimpan didalam Flash Memori mikrokontroler, oleh karena itu harus didahului dengan preprocessor flash atau const, misalnya : flash int integer_constant=1234+5; flash char char_constant=a; flash long long_int_constant1=99L; flash long long_int_constant2=0x10000000; flash int integer_array1[]={1,2,3}; /* The first two elements will be 1 and 2,the rest will be 0 */ flash int integer_array2[10]={1,2}; flash int multidim_array[2,3]={{1,2,3},{4,5,6}}; flash char string_constant1[]=This is a string constant; const char string_constant2[]=This is also a string constant; const float phi=3.14; const char k=32; Kontanta tidak dapat didefinisikan didalam fungsi.

Sistem Mikrokontroler

Hendawan Soebhakti

12

Konversi Tipe Data


Dalam operasi data, maka data tersebut harus memiliki tipe yang sama, misalnya sebuah operand bertipe char tidak dapat langsung dijumlahkan dengan operand yang bertipe int. Untuk itu perlu konversi data agar keduanya memiliki tipe data yang sama. Dalam bahasa c terdapat fasilitas untuk mengkonversi tipe data. Contoh : void main(void) { int a, c; long b; /* The long integer variable b akan dikonversi menjadi integer */ c=a+(int) b; }

Sistem Mikrokontroler

Hendawan Soebhakti

13

Fungsi
Sebuah program dapat dipecah menjadi beberapa fungsi (subprogram) untuk memudahkan penyusunan program. Sintaks Fungsi :

Type_fungsi Nama_fungsi(argumen_fungsi) { blok statement fungsi }

Type fungsi maksudnya sekumpulan eksekusi yang dipisah mempunyai hasil akhir berupa sebuah nilai dengan tipe data tertentu, misal : integer, karakter, float,void (tak bertipe) contoh:
void tambah(int x,int y) { int hasil; hasil=x+y; return hasil; }

Sistem Mikrokontroler

Hendawan Soebhakti

14

Fungsi
Contoh 1 : #include <stdio.h> int Tambah (int x, int y) { int z; z = x + y; return (z); } main () { int i, j, k; i = 10; j = 20; k = Tambah(i,j); printf ("The value of k is%d\n", k); }

Sistem Mikrokontroler

Hendawan Soebhakti

15

Fungsi
Penjelasan : Fungsi ini menambahkan dua nilai integer dan mengembalikan/return (menghasilkan) hasil penjumlahannya. Formal parameter :
int Tambah (int x, int y) int adalah tipe data yang akan di-return Tambah adalah nama fungsi intx, int y adalah parameter dengan tipe data int.

Body of the function :


{ int z; z = x + y; return (z); }

Didalam body of the function kita dapat mendeklarasikan variable atau statement. Variable yang berada didalam fungsi disebut local variable. Fungsi dapat dipanggil dengan menggunakan argument yang sesuai dengan formal parameter. Argument ini disebut actual parameter. k = Tambah(i,j); Selama dipanggil, nilai actual parameter dicopy ke dalam formal parameter dan statement dididalam fungsi dijalankan. Setelah menjalankan statement return, tidak ada lagi statement didalam fungsi yang dijalankan.

Sistem Mikrokontroler Hendawan Soebhakti 16

Prototipe Fungsi
Jika sebuah fungsi ditulis sebelum main, maka fungsi tersebut dapat dipanggil didalam body main. Jika fungsi tersebut ditulis setelah main, maka didalam deklarasi main harus ditulis prototype fungsi.

Contoh 1: #include <stdio.h> main ( ) { int i; void f1(int k); i = 0; printf (" The value f1 (&i); // printf (" The value } void f1(int k) { k = k + 10; }

// D <- Prototype of i before call %d \n", i); A of i after call %d \n", i); // B // C Fungsi

Sistem Mikrokontroler

Hendawan Soebhakti

17

Prototipe Fungsi

Contoh 2: #include <stdio.h> void f1(int k) // B { Fungsi k = k + 10; // C } main ( ) { int i; i = 0; printf (" The value of i before call %d \n", i); f1 (&i); // A printf (" The value of i after call %d \n", i); }

Sistem Mikrokontroler

Hendawan Soebhakti

18

Prototipe Fungsi
Penjelasan Pada Contoh 1, fungsi ditulis setelah main, maka kita harus menulis prototype didalam main seperti terlihat pada statement D. Pada Contoh 2, fungsi ditulis diatas main, maka selama proses compile fungsi f1 sudah ikut di-compile, maka kita tidak perlu menulis prototype didalam fungsi.

Sistem Mikrokontroler

Hendawan Soebhakti

19

Wassalamualaikum wr.wb.

Sistem Mikrokontroler

Hendawan Soebhakti

20

Anda mungkin juga menyukai