Terima Kasih Telah Mengunjungi Blog Ini

Thursday, May 10, 2012

Alogaritma Dan Program akses Sensor Jarak



Program  akses Sensor Jarak [ PING))) ]dengan Bascom AVR
roga
$regfile = “m8535.dat”
$crystal = 8000000
Config Lcd = 16 * 2
Config Lcdbus = 4
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , E = Portc.1 , Rs = Portc.0
Config Timer1 = Timer , Prescale = 64
Sigout Alias Portb.0
Sigin Alias Pinb.0
Dirsig Alias Ddrb.0
Dim Data_timer As Integer
‘==================================================
Inisialisasi:
Waitms 100
Initlcd
Locate 1 , 1
Lcd “demo ping”
Wait 1
Cls
Utama:
Gosub Ambil_datatimer
Locate 1 , 1
Lcd Data_timer
Wait 1
Cls
Goto Utama
‘==================================================
Ambil_datatimer:
Dirsig = 1
Set Sigout                                               ‘bankitkan pulsa
Waitus 10
Reset Sigout
‘terima pulsa ubah dulu jadi input
Dirsig = 0                                               ‘jadi input
Set Sigout                                              ‘pull up internal diaktifkan
Bitwait Sigin , Set
Data_timer = 0
Timer1 = 0
Start Timer1
Do
If Sigin = 0 Then
Data_timer = Timer1
Stop Timer1
Exit Do
End If
If Tifr.2 = 1 Then                                ‘cek register timer1 overflow?
Stop Timer1
Tifr.2 = 1
Data_timer = 0
Exit Do
End If
Loop
Stop Timer1
Return
V


Sensor Jarak (sensor ultrasonic)

Sensor jarak dapat menggunakan sensor ultasonic, sensor ultrasonic cara kerjanya sama seperti kelalawar saat mengetahui halangan di depannya. Sensor ultrasonic bekerja dengan memancarkan gelombang ultrasonic berupa pulse melalui TX(transifer) dari sensor dan akan terpantul kembali gelomnbang ultrasonic dan di tangkap oleh RX(resiver) sensordari sensor. Sensor ultrasonic mempunyai jara 2cm hingga 300cm.
/*****************************************************
Project :
Version :
Date    : 4/2/2008
Comments:


Chip type           : ATmega8535
Program type        : Application
Clock frequency     : 4.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 128
*****************************************************/
#include
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include
#include
#include
//deklarasi PIN I/O PING
#define PULSE PORTB.0
#define ECHO  PINB.0
#define ARAH  DDRB.0
#define OUT 1
#define INP 0
unsigned int ultrasonic()
{
unsigned int count=0;                               //deklarasi variabel local count: penghitung lebar ECHO
unsigned int jarak;                                      //dekarasi jarak: hasil pembagian lebar ECHO dengan suatu konstanta
ARAH=OUT;                                                 //mengatur PIN I/O sebagai output
PULSE=1;                                     //memberikan tanda ke PING untuk memancarkan ultrasonic burst
delay_us(5);                               //waktu tunggu sebelum pengukuran min. 2us biasanya 5us
PULSE=0;                                    //menberikan sinyal low ke PING
ARAH=INP;                               //arah PIN I/O diatur sebagai input
PULSE=1;                                    //mengatur PIN I/O sebagai pill-up
while (ECHO==0) {};              //menunggu sinyal ECHO high
while (ECHO==1)
{
count++;                                  //menghitung lebar sinyal ECHO high
}
jarak=(unsigned int)(((float)count)/25); // nilai pembagi dikalibrasi sampai sesuai dengan satuan yang diinginkan
return(jarak);                                                      //mengembalikan jarak ke fungsi ultrasonic dengan tipe data unsigned int
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
unsigned char kata[16];//variabel lokal untuk menyusun karakter dengan lebar array 16 dan mengirim ke LCD
// Input/Output Ports initialization
// Port A 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
PORTA=0×00;
DDRA=0×00;
// 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=0×00;
DDRB=0×00;
// Port C 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
PORTC=0×00;
DDRC=0×00;
// 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=0×00;
DDRD=0×00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0×00;
TCNT0=0×00;
OCR0=0×00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0×00;
TCCR1B=0×00;
TCNT1H=0×00;
TCNT1L=0×00;
ICR1H=0×00;
ICR1L=0×00;
OCR1AH=0×00;
OCR1AL=0×00;
OCR1BH=0×00;
OCR1BL=0×00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0×00;
TCCR2=0×00;
TCNT2=0×00;
OCR2=0×00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0×00;
MCUCSR=0×00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0×00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0×80;
SFIOR=0×00;
// LCD module initialization
lcd_init(16);
while (1)
{
sprintf(kata, “J=%3d cm”, ultrasonic()); //menyusun karakter ke dalam satu baris dengan menggunakan fungsi sprintf
lcd_clear();                                                        //membersihkan layar LCD
lcd_gotoxy(0,0);                                            //menempatkan kursor pada koordinat 0,0 LCD
lcd_puts(kata);                                                //mengirim data ke LCD
delay_ms(300);                                              //delay memberi waktu sebelum lcd_clear();
}
}