Pemrograman MCS51 (AT89S51,52 dll) dengan Bahasa C

Software yang digunakan dalam ujicoba kali ini ialah MIKROC 8051 ver 2.2. pemrograman mikrokontroler AT89S51 dengan bahasa C memang lebih mudah jika dibandingkan dengan menggunakan Assembler. apalagi jika kita ingin membuat perangkat yang rumit akan sangat terasa perbedaannya.

Awal pembuatan program bisa di lihat pada video berikut :


Resolusi lebih tinggi klik disini



Berikut merupakan daftar uji coba tersebut:
1. Percobaan LED
Skema Uji Coba LED

Program LED 1 menyalakan Led dengan bilangan heksa
void main() {
P0=0x00;     // LED SEMUA ON
}


Program LED 2 menyalakan sebagian Led dengan bilangan heksa
void main() {
P0=0xF0;     // LED P0-P3 ON
}

Program LED 3 menyalakan 1 Led dengan bilangan biner
void main() {
P0=0b11111110;     // LED 1 ON
}

Program LED 4 menyalakan 2  Led dengan bilangan biner
void main() {
P0=0b01111110;     // LED 1 dan 8 ON
}

Program LED 5 menyalakan Led per bit
void main() {
P0_0_bit=0;     // LED 1  ON
P0_7_bit=0;     // LED 8 ON
}


Program LED 6 Blinking led dengan delay
void main() {
while(1){
P0=0b00000000;     //led ON
delay_ms(1000);    //tunda 1 detik
P0=0b11111111;     //led OFF
delay_ms(1000);    //tunda 1 detik
} 

}

2. Percobaan Switch
Skema Uji Coba Switch

Program Switch 1
void main() {
while(1){
if(P1_0_bit==0){P0=0x00;}  //jika tombol 1 ditekan LED ON
}
}

Program Switch 2 
void main() {
while(1){
if(P1_0_bit==0){P0=0x00;}  //jika tombol 1 ditekan LED ON
if(P1_1_bit==0){P0=0xff;}  //jika tombol 2 ditekan LED OFF
}
}

Program Switch 3
void main() {
while(1){
if(P1_0_bit==0){P0_0_bit=0;}  //jika tombol 1 ditekan LED 1 ON
if(P1_1_bit==0){P0_1_bit=0;}  //jika tombol 2 ditekan LED 2 ON
if(P1_2_bit==0){P0=0xff;}     //jika tombol 3 ditekan LED OFF
}
}

Program Switch 4
void main() {
while(1){
if(P1_0_bit==0){P0=0x00;}     //jika tombol 1 ditekan led on dilepas led off
else{P0=0xff;}
} 
}

3. Percobaan LCD
Skema Uji Coba LCD

Program LCD 1
// lcd pinout settings
sbit LCD_RS at P2_0_bit;
sbit LCD_EN at P2_1_bit;
sbit LCD_D7 at P2_7_bit;
sbit LCD_D6 at P2_6_bit;
sbit LCD_D5 at P2_5_bit;
sbit LCD_D4 at P2_4_bit;

void main() {
lcd_init();
Lcd_Out(1, 3, "tutorial-");             //tulis di baris ke 1 kolom 3
lcd_Out(2,1, "mikrokontroler");   //tulis di baris ke 2 kolom 1
}


Program LCD 2
// lcd pinout settings
sbit LCD_RS at P2_0_bit;
sbit LCD_EN at P2_1_bit;
sbit LCD_D7 at P2_7_bit;
sbit LCD_D6 at P2_6_bit;
sbit LCD_D5 at P2_5_bit;
sbit LCD_D4 at P2_4_bit;

void main() {
lcd_init();
Lcd_Out(1, 3, "Uji coba");               //tulis di baris ke 1 kolom 3
Lcd_Cmd(_LCD_CURSOR_OFF);    //matikan kursor
while(1)
{
Lcd_Cmd(_LCD_SHIFT_RIGHT);     //geser tulisan ke kanan
delay_ms(1000);                              //tunda 1 detik
}                                                          //ulangi 
}


Program LCD 3
// lcd pinout settings
sbit LCD_RS at P2_0_bit;
sbit LCD_EN at P2_1_bit;
sbit LCD_D7 at P2_7_bit;
sbit LCD_D6 at P2_6_bit;
sbit LCD_D5 at P2_5_bit;
sbit LCD_D4 at P2_4_bit;

void main() {
lcd_init();
Lcd_Out(1, 3, "Uji coba");               //tulis di baris ke 1 kolom 3
Lcd_Cmd(_LCD_CURSOR_OFF);    //matikan kursor 

}

Command LCD lainnya :


4. Percobaan I2C RTC (Jam Digital Dengan LCD)
Skema Uji Coba RTC

Program Jam Digital
char seconds, minutes, hours, date, month;  // Global date/time variables
unsigned int year;
unsigned int RTCModuleAddress, YearOffset; // RTC chip description variables


#define DS1307          // Uncomment this line if you use DS1307 RTC chip (mE RTC2 extra board)

// Software I2C connections
sbit Soft_I2C_Scl at P1_0_bit;
sbit Soft_I2C_Sda at P1_1_bit;
// End Software I2C connections

// LCD module connections
sbit LCD_RS at P2_0_bit;
sbit LCD_EN at P2_1_bit;

sbit LCD_D4 at P2_4_bit;
sbit LCD_D5 at P2_5_bit;
sbit LCD_D6 at P2_6_bit;
sbit LCD_D7 at P2_7_bit;
// End LCD module connections

//------------------ Performs project-wide init
void Init_Main() {



  #ifdef DS1307
    RTCModuleAddress   = 0xD0;
    YearOffset         = 2000;
  #endif


  Soft_I2C_Init();           // Initialize Soft I2C communication
  Lcd_Init();                // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);       // Clear LCD display
  Lcd_Cmd(_LCD_CURSOR_OFF);  // Turn cursor off

  LCD_Out(1,1,"Date:");      // Prepare and output static text on LCD
  Lcd_Chr(1,8,':');
  Lcd_Chr(1,11,':');
  LCD_Out(2,1,"Time:");
  Lcd_Chr(2,8,':');
  Lcd_Chr(2,11,':');
}


//--------------------- Reads time and date information from DS1307 RTC
void Read_Time_DS1307() {
  char byte_read;

  Soft_I2C_Start();                   // Issue start signal
  Soft_I2C_Write(RTCModuleAddress);   // RTC module address + write (R#/W = 0)
  Soft_I2C_Write(0);                  // Start from seconds byte
  Soft_I2C_Start();                   // Issue repeated start signal
  Soft_I2C_Write(RTCModuleAddress+1); // RTC module address + read  (R#/W = 1)

  byte_read = Soft_I2C_Read(1);                                // Read seconds byte
  seconds = ((byte_read & 0x70) >> 4)*10 + (byte_read & 0x0F); // Transform seconds

  byte_read = Soft_I2C_Read(1);                                // Read minutes byte
  minutes = ((byte_read & 0x70) >> 4)*10 + (byte_read & 0x0F); // Transform minutes

  byte_read = Soft_I2C_Read(1);                                // Read hours byte
  if (byte_read.B6) {                                          // 12h format
    hours = ((byte_read & 0x10) >> 4)*10 + (byte_read & 0x0F); // Transform hours
    if (byte_read.B5)                                          // PM flag
      hours = hours + 12;
  }
  else
    hours = ((byte_read & 0x30) >> 4)*10 + (byte_read & 0x0F); // Transform hours

  byte_read = Soft_I2C_Read(1);                                // Read weekday byte

  byte_read = Soft_I2C_Read(1);                                // Read date byte
  date = ((byte_read & 0x30) >> 4)*10 + (byte_read & 0x0F);    // Transform date

  byte_read = Soft_I2C_Read(1);                                // Read month byte
  month = ((byte_read & 0x10) >> 4)*10 + (byte_read & 0x0F);   // Transform month

  byte_read = Soft_I2C_Read(1);                                // Read year byte
  year = YearOffset + ((byte_read & 0xF0) >> 4)*10 + (byte_read & 0x0F); // Transform year

  Soft_I2C_Stop();                    // Issue stop signal
}

//--------------------- Reads time and date information from RTC
void Read_Time() {

  #ifdef DS1307
    Read_Time_DS1307();
  #endif
}

//-------------------- Output values to LCD
void Display_Time() {

  Lcd_Chr(1, 6, (date / 10)   + 48);       // Print tens digit of date variable
  Lcd_Chr(1, 7, (date % 10)   + 48);    // Print oness digit of date variable
  Lcd_Chr(1, 9, (month / 10)  + 48);
  Lcd_Chr(1,10, (month % 10)  + 48);
  Lcd_Chr(1,12, ((year / 1000) % 10) + 48);    // Print year
  Lcd_Chr(1,13, ((year / 100)  % 10) + 48);
  Lcd_Chr(1,14, ((year / 10)   % 10) + 48);
  Lcd_Chr(1,15, (year % 10)          + 48);

  Lcd_Chr(2, 6, (hours / 10)   + 48);
  Lcd_Chr(2, 7, (hours % 10)   + 48);
  Lcd_Chr(2, 9, (minutes / 10) + 48);
  Lcd_Chr(2,10, (minutes % 10) + 48);
  Lcd_Chr(2,12, (seconds / 10) + 48);
  Lcd_Chr(2,13, (seconds % 10) + 48);
}

//----------------- Main procedure
void main() {


  Init_Main();               // Perform initialization

  while (1) {                // Endless loop
    Read_Time();             // Read time from RTC
    Display_Time();          // Prepare and display on LCD

    Delay_ms(950);          // Wait 1 second
  }
}

5. Percobaan SOUND (mikrokontroler mengeluarkan nada)
Skema Uji Coba Sound

sbit Sound_Play_Pin at P2_0_bit;
/*
220.00 Hz A (La)
246.94 Hz B (Si)
261.63 Hz C (Do)
293.66 Hz D (Re)
329.63 Hz E (Mi)
349.23 Hz F (Fa)
392.00 Hz G (Sol)
*/

void main() {
 Sound_Init();
 while(1){

 Sound_Play(262, 250);  // Frequency = 262Hz, duration = 250ms DO
 Sound_Play(294, 250);  // Frequency = 294Hz, duration = 250ms RE
 Sound_Play(330, 250);  // Frequency = 330Hz, duration = 250ms MI
 Sound_Play(349, 250);  // Frequency = 349Hz, duration = 250ms FA
 Sound_Play(392, 250);  // Frequency = 392Hz, duration = 250ms SO
 Sound_Play(220, 250);  // Frequency = 220Hz, duration = 250ms LA
 Sound_Play(247, 250);  // Frequency = 247Hz, duration = 250ms SI
 }


}

semua file uji coba tersebut dapat di download disini