Kamis, 17 Maret 2016

ROBOT PENGHINDAR HALANGAN

Deskripsi dan gambar rangkaianya menyusul......
Tapi bisa d amati dari code programnya, pin mana yang terhubung ke sensor ultrasonik, servo dan motor dc.
Bagian- bagian pada Robot ini:
  1. Sisteminimum mikrokontroler.
  2. Sensor Ultrasonik
  3. Servo Tower Pro MG90
Code Programnya:

#include(mega8.h)
#include(delay.h)
#include(stdio.h)
// Alphanumeric LCD Module functions
// Alphanumeric LCD functions
#include(alcd.h)

#define servox     PORTC.4
#define TRIGGER   PORTD.4
#define ECHO      PIND.5
unsigned char baris_1[16], baris_2[16];
long timer = 0, xTimer = 0;
float pulsa, jarak,jarak_kanan, jarak_kiri;

int a=0,i;
unsigned char servo;
unsigned char y;

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here

a++;
if(a==941)    
 { a=0;
   PORTC.4 =1;   
 }

if (24 { y=(char)a;  
   if(y==servo)    servox = 0;    
 }

}

// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
xTimer++;
}

// Declare your global variables here


void bacaJarak() {
   TRIGGER = 1;         // Memberikan pulsa high selama 10 microseconds
   delay_us(10);
   TRIGGER = 0;
   while(ECHO == 0);    // Tunggu pulsa high dari sensor
   TCNT1 = 0;           // Nolkan timer
   xTimer = 0;          // Nolkan variabel xTimer
   while(ECHO == 1);    // Tunggu pulsa low dari sensor
   timer = TCNT1;       // Ambil data dari timer
   pulsa = (float)xTimer * 65535 * 0.5 + (float)timer * 0.5;   // Menghitung pulsa yang masuk dalam satuan microseconds
   jarak = pulsa / 29.034 / 2;   // Menghitung jarak dalam satuan centimeter
   lcd_clear();
   //sprintf(baris_1, "Pulsa = %5.2f ", pulsa);
   sprintf(baris_2, "Jarak = %3.2f ", jarak);
   lcd_gotoxy(0, 0);
   lcd_puts(baris_1);
   lcd_gotoxy(0, 1);
   lcd_puts(baris_2);
   delay_us(50);
}


void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x10;
DDRC=0x1F;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x20;
DDRD=0x10;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 16000.000 kHz
TCCR0=0x01;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 2000.000 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x02;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x05;

// USART initialization
// USART disabled
UCSRB=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTB Bit 0
// RD - PORTB Bit 1
// EN - PORTB Bit 2
// D4 - PORTB Bit 3
// D5 - PORTB Bit 4
// D6 - PORTB Bit 5
// D7 - PORTC Bit 0
// Characters/line: 16
lcd_init(16);
servo=64;          //90 der
delay_ms(5000);
// Global enable interrupts
#asm("sei")

while (1)
 {
 // Place your code here
  for(i=0;i<5 br="" i="">   {
    bacaJarak();
    //delay_ms(200);
   } 
  
   if(jarak>20)
   {
    while(jarak>20)
     {
      PORTC.0=1;PORTC.2=1;//maju
      delay_ms(100);
      PORTC.0=PORTC.1=PORTC.2=PORTC.3=0;
      for(i=0;i<5 br="" i="">       {
        bacaJarak();
       } 
     }
   }
  
  
   servo=25;          //180 der
   delay_ms(500);  
     
    for(i=0;i<5 br="" i="">      {
      bacaJarak();
      jarak_kanan=jarak;
      //delay_ms(200);
      }
    
    servo=108;         //0 der
    delay_ms(500);
    for(i=0;i<5 br="" i="">      {
      bacaJarak();
      jarak_kiri=jarak;
      //delay_ms(200);
      }
    
    servo=64;          //90 der
    delay_ms(500);
   
   
    if (jarak_kanan>jarak_kiri){PORTC.1=1;PORTC.2=1;delay_ms(250);PORTC.1=0;PORTC.2=0;delay_ms(1000);}
    else {PORTC.0=1;PORTC.3=1;delay_ms(250);PORTC.0=0;PORTC.3=0;delay_ms(1000);}
    
     
    }
}


vidionya


Rabu, 16 Maret 2016

APLIKASI VB UNTUK KONTROL 2 LAMPU (LED) ATAULEBIH

Tampilan Visual Basicnya
Tool Box yang digunakan:
  1. Frame
  2. Shape
  3. Check Box
  4. Combo Box
  5. Command Button
  6. MSComm
 Code Program:
Private Sub Check1_Click()
If Check1.Value = 1 Then
Shape1.FillColor = vbRed
MSComm1.Output = "a"
Else
Shape1.FillColor = &H40&
MSComm1.Output = "1"
End If
End Sub

Private Sub Check2_Click()
If Check2.Value = 1 Then
Shape2.FillColor = vbRed
MSComm1.Output = "b"
Else
Shape2.FillColor = &H40&
MSComm1.Output = "2"
End If
End Sub

Private Sub cmdconnect_Click()
MSComm1.CommPort = Val(Combo1.Text)
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
MsgBox "Koneksi Berhasil !", vbInformation, "Sukses"
Frame1.Enabled = True
Combo1.Enabled = False
cmdconnect.Enabled = False
End Sub

Private Sub cmdexit_Click()
End
End Sub

Private Sub Form_Load()
For Port = 1 To 20
Combo1.AddItem Port
Next Port
Frame1.Enabled = False
MSComm1.Settings = "9600,n,8,1"
End Sub

Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub


Vidionya