Dasar
Dasar
Pemrograman
Pemrograman
C
C
Hendawan
Hendawan
Soebhakti
Soebhakti
Oktober
Tujuan
Tujuan
►
►
Mampu membuat program mikrokontroler
Mampu membuat program mikrokontroler
dengan bahasa C
dengan bahasa C
►
Sub
Sub
Pokok
Pokok
Bahasan
Bahasan
►
►
Library
Library
►
►
Fungsi
Fungsi
►
►
Tipe Data
Tipe Data
►
►
Konstanta
Konstanta
►
►
Variable
Variable
►
Struktur
Struktur
Program
Program
Bahasa
Bahasa
C
C
1. /***************************************************** 2. Project : Tampilan LED
3. Version : 1
4. Date : 9/29/2009
5. Author : Hendawan Soebhakti 6. Company : Politeknik Batam
7. Comments:
8. Menampilkan variasi nyala LED pada PORTA
9. *****************************************************/ 10. #include <mega8535.h>
11. #include <delay.h> 12. #define on 1
13. #define off 0
14. // Declare your global variables here
15. unsigned char data; // Global Variable
16. void Geser(void) // Function 17. {
Komentar
Preprocessor directive : Memasukkan file header
Preprocessor directive : Mendefinisikan string
Struktur
Struktur
Program
Program
Bahasa
Bahasa
C
C
26. void main(void) 27. {
28. // Declare your local variables here 29. // Input/Output Ports initialization 30. // Port A initialization
31. // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
32. // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
33. PORTA=0x00; 34. DDRA=0xFF; 35. . 36. . 37. . 38. while (1) 39. {
40. // Place your code here
41. Geser(); 42. PORTA.0=on; 43. delay_ms(1000); 44. PORTA.0=off; 45. delay_ms(1000); 46. }; 47. }
Fungsi main
Library
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 }
Library
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
Library
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
Tipe
Tipe
Data
Data
► Berikut ini adalah tabel semua tipe data yang disupport CodeVisionAVR C compiler :
±1.175e-38 to ±3.402e38 32 double ±1.175e-38 to ±3.402e38 32 float -2147483648 to 2147483647 32
signed long int
0 to 4294967295 32
unsigned long int
-2147483648 to 2147483647 32 long int -32768 to 32767 16 signed int 0 to 65535 16 unsigned int -32768 to 32767 16 short int -32768 to 32767 16 int -128 to 127 8 signed char 0 to 255 8 unsigned char -128 to 127 8 char 0 , 1 1 bit Range Size (Bits) Type
Variable
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 :
Variable
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;
Konstanta
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}};
Konversi
Konversi
Tipe
Tipe
Data
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; }
Fungsi
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)
Fungsi
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); }
Fungsi
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
Prototipe Fungsi
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); // D <- Prototype
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); }
void f1(int k) // B {
Prototipe Fungsi
Prototipe Fungsi
► Contoh 2: #include <stdio.h> void f1(int k) // B { k = k + 10; // C } main ( ) { int i; i = 0;printf (" The value of i before call %d \n", i); f1 (&i); // A
Prototipe Fungsi
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.