- Dapatkan link
- X
- Aplikasi Lainnya
Pengetahuan Dasar Timer Untuk Pengukuran Jarak Dengan Ultrasonic
NOV 30
under construction.
Pada artikel ini sensor ultrasonik yang digunakan adalah aktif high , maksudnya pemicunya/start pulse berupa sinyal pendek high spt pada gambar dibawah ini . jika anda membeli sensor ultrasonic perhatikan datasheet /manual nya apakah start pulse dan sinyal output sensor nya aktif low atau aktif high.
Pada gambar diatas sinyal satrt pulse warna merah dikirim dari micon AVR ke sensor ultrasonic , sedangkan warna biru menggambarkan sinyal output dari sensor ultrasonic. keduanya berjalan pada jalur yg sama tapi secara bergantian.
Cara kerja (lihat gambar diatas):
– AVR mentriger sensor (sinyal warna merah) agar mengirim sinyal pendek (burst) dgn frekwensi 40khz
– Ketika Sensor ultrasonik mengirim burst , output sensor menjadi high (sinyal warna biru), menandakan awal burst dipancarkan
– Burst menabrak pahlawan bertopeng dan kembali dipantukan ke sensor – Ketika sinyal pantulan terdeteksi, output sensor menjadi low
– Panjang pulsa high (sinyal warna biru) dari output sensor sebanding dgn jarak yg ditempuh burst.
Panjang sinyal output sensor yg berwarna biru mencerminkan waktu tempuh sinyal burst 40khz yg dipancarkan sampai pantulan diterima. waktu tempuh tsb juga bisa digunakan untuk mengetahui jarak tempuh sinyal . Karena di ketahui kecepatan Burst adalah 344 M/Detik maka jarak yg ditempuh oleh burst adalah
Jarak sensor dgn pahlawan bertopeng = (T/2)*340 meter
Menghitung Panjang Pulsa dgn Timer Seperti kita ketahui Timer mempunyai mode kerja antara lain
- Mode Normal
- Mode PWM
- Mode CTC
Untuk menghitung panjang pulsa kita bisa menggunakan mode normal dan dengan mengaktifkan interupsi input capture (int melalui pin ICP) .
urutan satu siklus program kira kira begini :
1. PORTD.6 diset sbg output
2. buat start pulse low-high-low di PORTD.6 , burst dipancarkan
3. PORTD.6 diset sebagai input
4, interupt input capture di aktifkan , set pemicu interupt : rising edge.
5. , sensor mengeluarkan pulsa high , terjadi interupsi ICP dipicu rising edge . low->high
6. service rutin inteupsi ICP dijalankan ,timer1 di aktifkan , TCNT1 mulai counting , triger interupsi ICP diset Falling edge.
7. terjadi interupsi kedua dipicu perubahan falling edge high->low, sinyal keluaran sensor .
8. service rutin ICP kembali di laksanakan , matikan timer , ambil nilai conter timer1 di reg TCNT1.
9. selesai
Pemrograman perhitungan panjang pulsa output sensor dengan interupsi ICP
http://www.limaportal.blogspot.com
http://www.limaportal.blogspot.com
/*****************************************************
This program was produced by the
CodeWizardAVR V2.04.4a Advance
Chip type : ATmega16
AVR Core Clock frequency: 11.059200 MHz
*****************************************************/
This program was produced by the
CodeWizardAVR V2.04.4a Advance
Chip type : ATmega16
AVR Core Clock frequency: 11.059200 MHz
*****************************************************/
#include <mega16.h>
#include <delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#include <stdio.h>
#define awal 1
#define counting 2
#define finish 3
unsigned char status=3;
float counter;
char lcd_buffer[16];
#include <delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#include <stdio.h>
#define awal 1
#define counting 2
#define finish 3
unsigned char status=3;
float counter;
char lcd_buffer[16];
// Timer1 input capture interrupt service routine
interrupt [TIM1_CAPT] void timer1_capt_isr(void)
{
// Place your code here
if(status==awal)
{
interrupt [TIM1_CAPT] void timer1_capt_isr(void)
{
// Place your code here
if(status==awal)
{
TCCR1B |= 0x05; //Start Timer 1 , set clock timer 10800 Hz
TCCR1B &= 0xBF; //bit ICSE=0 set triger ICP falling edge
status = 2;
TCCR1B &= 0xBF; //bit ICSE=0 set triger ICP falling edge
status = 2;
return ;
}
}
if(status==counting)
{
TCCR1B &= 0xF8; // stop Timer 1
counter =TCNT1;
{
TCCR1B &= 0xF8; // stop Timer 1
counter =TCNT1;
TCNT1 = 0x0000; //Clear Timer 1
TIMSK &= 0xDF; // matikan int ICP , PIND6 sebagai I/O lagi
DDRD |= 0x40; //Set PIND.6 as out
TCCR1B |= 0x40; //Rising edge
status = finish;
lcd_gotoxy(0,0);
lcd_clear();
lcd_gotoxy(0,0);
lcd_clear();
//diketahui kecepatan burst = 344 m/detik
sprintf(lcd_buffer,”jarak=%f”,(float)(counter/2*(1/10800.0) *344.0);
lcd_puts(lcd_buffer);
delay_ms(500);
lcd_gotoxy(5,1);
lcd_putsf(“satuan Meter”);
return;
lcd_puts(lcd_buffer);
delay_ms(500);
lcd_gotoxy(5,1);
lcd_putsf(“satuan Meter”);
return;
}
}
// Declare your global variables here
// Declare your global variables here
void main(void)
{
// Declare your local variables here
{
// Declare your local variables here
// 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=0x00;
DDRD=0x40;
// 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=0x00;
DDRD=0x40;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 10.800 kHz
// Mode: Normal top=FFFFh
// Clock source: System Clock
// Clock value: 10.800 kHz
// Mode: Normal top=FFFFh
// Input Capture on Rising Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: On
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x40; //TIMER1 STOP
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x20;
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: On
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x40; //TIMER1 STOP
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x20;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
lcd_init(16);
// Global enable interrupts
#asm(“sei”)
#asm(“sei”)
lcd_gotoxy(0,1);
lcd_putsf(“distance meter”);
delay_ms(1500);
lcd_clear();
while (1)
{
delay_ms(1000);
//Bit 5 TIMSK – TICIE1=0, Timer1 Input Capture Interrupt(ICP) DISABLE
TIMSK &= 0xDF;
lcd_putsf(“distance meter”);
delay_ms(1500);
lcd_clear();
while (1)
{
delay_ms(1000);
//Bit 5 TIMSK – TICIE1=0, Timer1 Input Capture Interrupt(ICP) DISABLE
TIMSK &= 0xDF;
//======================SEND TRIGER TO PING SENSOR
DDRD |= 0x40; //Set PIND.6 /ICP as out
PORTD |= 0x40; // PIND.6 SET HIGH
delay_us(20);
PORTD.6 &= 0xBF; // PIND.6 SET LOW
DDRD &= 0xBF; // Set PORTD.6 as in
//===========================================
DDRD |= 0x40; //Set PIND.6 /ICP as out
PORTD |= 0x40; // PIND.6 SET HIGH
delay_us(20);
PORTD.6 &= 0xBF; // PIND.6 SET LOW
DDRD &= 0xBF; // Set PORTD.6 as in
//===========================================
PORTD &= 0xBF; // PORTD.6 Tri-state tdk terkonek dgn resistor pull up
status =awal;
TIMSK |= 0x20; // aktifkan int ICP TICIE1=1
};
}
}
Pemrograman perhitungan panjang pulsa output sensor tanpa interupsi ICP
*****************************************************/
#include <mega16.h>
#include <delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#include <stdio.h>
#include <delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#include <stdio.h>
float counter;
char lcd_buffer[16];
char lcd_buffer[16];
void main(void)
http://www.limaportal.blogspot.com
http://www.limaportal.blogspot.com
{
PORTD=0x00;
DDRD=0x40;
DDRD=0x40;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 10.800 kHz
// Mode: Normal top=FFFFh
// Clock source: System Clock
// Clock value: 10.800 kHz
// Mode: Normal top=FFFFh
// Input Capture on Rising Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: On
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x40; //TIMER1 STOP
TCNT1H=0x00;
TCNT1L=0x00;
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: On
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x40; //TIMER1 STOP
TCNT1H=0x00;
TCNT1L=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x20;
TIMSK=0x20;
// LCD module initialization
lcd_init(16);
lcd_init(16);
// Global enable interrupts
#asm(“sei”)
#asm(“sei”)
while (1)
{
delay_ms(1000);
DDRD |= 0x40; //Set PIND.6 /ICP as out
PORTD |= 0x40; // PIND.6 SET HIGH
{
delay_ms(1000);
DDRD |= 0x40; //Set PIND.6 /ICP as out
PORTD |= 0x40; // PIND.6 SET HIGH
delay_us(20);
PORTD.6 &= 0xBF; // PIND.6 SET LOW
DDRD &= 0xBF; // Set PORTD.6 as in
PORTD.6 &= 0xBF; // PIND.6 SET LOW
DDRD &= 0xBF; // Set PORTD.6 as in
TCNT1=0;
while (!PINB.6); // tunggu pulsa output sensor
while (!PINB.6); // tunggu pulsa output sensor
TCCR1B=0x05; // start timer
while ((PINB.6) && !(TIFR & 0x80)); //tunggu akhir sinyal output sensor
TCCR1B=0x00; // stop timer
counter = TCNT1; // simpan counting TCNT1
while ((PINB.6) && !(TIFR & 0x80)); //tunggu akhir sinyal output sensor
TCCR1B=0x00; // stop timer
counter = TCNT1; // simpan counting TCNT1
lcd_gotoxy(0,0);
lcd_clear();
//diketahui kecepatan burst = 344 m/detik
sprintf(lcd_buffer,”jarak=%f”,(float)(counter/2*(1/10800.0) *344.0);
lcd_puts(lcd_buffer);
delay_ms(500);
lcd_gotoxy(5,1);
lcd_putsf(“satuan Meter”);
lcd_puts(lcd_buffer);
delay_ms(500);
lcd_gotoxy(5,1);
lcd_putsf(“satuan Meter”);


Komentar
Posting Komentar