Vous êtes sur la page 1sur 6

/*______________________________________________________

|
|
|
|
|
|
|
|
|
|
US1 -> US7 -> US2 -> US6 -> US3 -> US5 -> US4;;
|
|
|
________________________________________________________|*/
/:::::::::::::::\
|
4
|
|
3
5 |
| 2
6 |
|
US
|
| 1
7 |
|
|

#include <mega8.h>
#include <delay.h>
#include <stdio.h>
//#define __SCAN__US__COUNTER
#define manual_switch

PINB.0

//---------------------------------#define TR_US1 PORTC.5


#define EC_US1 PINC.4
//---------------------------------#define TR_US2 PORTD.2
#define EC_US2 PIND.3
//---------------------------------#define TR_US3 PORTD.4
#define EC_US3 PIND.6
//---------------------------------#define TR_US4 PORTC.1
#define EC_US4 PINC.0
//---------------------------------#define TR_US5 PORTC.3
#define EC_US5 PINC.2
//---------------------------------#define DIR_PING1 DDRB.2
#define OUT_PING1 PORTB.2
#define IN_PING1 PINB.2
#define DIR_PING2 DDRB.3
#define OUT_PING2 PORTB.3
#define IN_PING2 PINB.3
//---------------------------------#define DATA_COMPLETE
#define RESET_USART

255
0x00

#ifndef __TX__RX
#define RX
#define TX
#endif

PORTD.0
PORTD.1

//===================================
unsigned char data_US;
unsigned int jarak;
//===================================
#include "__US__ALT.h"
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCCR0=0;
TCNT0=255;
//Max Jarak
}
#define
#define
#define
#define
#define
#define
#define

__PING1
__PING2
__US1
__US5
__US2
__US4
__US3

void SCAN_SRF()
{
#ifdef __PING1
//--------------"US-satu"-------------------//===============Scan Ping -1 ==================
//--------------Ping Belakang Kiri-------------DIR_PING1 = 1;
OUT_PING1 = 0;
delay_us(10);
OUT_PING1 = 1;
delay_us(5);
OUT_PING1 = 0;
delay_us(10);
DIR_PING1 = 0;
while (IN_PING1 == 0);
#asm("sei");
TCCR0 = 0x04;
while (IN_PING1 == 1);
TCCR0 = 0x00;
data_US = TCNT0;
putchar(data_US);
delay_us(10);
TCNT0 = 0;
delay_ms(4);
//====================End of ScanPing=================
#endif
#ifdef __PING2
//===============Scan Ping -2=========================
//---------------Ping belakang Kanan ----------------DIR_PING2 = 1;
OUT_PING2 = 0;
delay_us(10);
OUT_PING2 = 1;

delay_us(5);
OUT_PING2 = 0;
delay_us(10);
DIR_PING2 = 0;
while (IN_PING2 == 0);
#asm("sei");
TCCR0 = 0x04;
while (IN_PING2 == 1);
TCCR0 = 0x00;
data_US = TCNT0;
putchar(data_US);
delay_us(10);
TCNT0 = 0;
delay_ms(4);
//====================End of ScanPing=================
#endif
//------------------------- Ping Belakang Tengah kiri-------#ifdef __US1
TR_US1 = 0;
delay_us(10);
TR_US1 = 1;
delay_us(10);
TR_US1 = 0;
while (EC_US1 == 0);
#asm("sei")
TCCR0 = 0x04;
while (EC_US1 == 1 && TCCR0 == 0x04);
TCCR0=0x00;
data_US = TCNT0;
putchar(data_US);
delay_us(10);
TCNT0 = 0;
delay_ms(4);
#endif
#ifdef __US5
//--------------"US-Lima"-------------------TR_US5 = 0;
delay_us(10);
TR_US5 = 1;
delay_us(10);
TR_US5 = 0;
while (EC_US5 == 0);
#asm("sei")
TCCR0 = 0x04;
while (EC_US5 == 1 && TCCR0 == 0x04);
TCCR0=0x00;
data_US = TCNT0;
putchar(data_US);
delay_us(10);
TCNT0 = 0;
delay_ms(4);
#endif
#ifdef __US2
//--------------"US-Dua"-------------------TR_US2 = 0;
delay_us(10);

TR_US2 = 1;
delay_us(10);
TR_US2 = 0;
while (EC_US2 == 0);
#asm("sei")
TCCR0 = 0x04;
while (EC_US2 == 1 && TCCR0 == 0x04);
TCCR0=0x00;
data_US = TCNT0;
putchar(data_US);
delay_us(10);
TCNT0 = 0;
delay_ms(4);
#endif
#ifdef __US4
//--------------"US-Empat"-------------------TR_US4 = 0;
delay_us(10);
TR_US4 = 1;
delay_us(10);
TR_US4 = 0;
while (EC_US4 == 0);
#asm("sei")
TCCR0 = 0x04;
while (EC_US4 == 1 && TCCR0 == 0x04);
TCCR0=0x00;
data_US = TCNT0;
putchar(data_US);
delay_us(10);
TCNT0 = 0;
delay_ms(4);
//-------------------------------------------#endif
#ifdef __US3
//--------------"US-Tiga"-------------------TR_US3 = 0;
delay_us(10);
TR_US3 = 1;
delay_us(10);
TR_US3 = 0;
while (EC_US3 == 0);
#asm("sei")
TCCR0 = 0x04;
while (EC_US3 == 1 && TCCR0 == 0x04);
TCCR0=0x00;
data_US = TCNT0;
putchar(data_US);
delay_us(10);
TCNT0 = 0;
delay_ms(4);
#endif
if (manual_switch == 0) putchar(1);
else putchar(0);
delay_ms(4);
}
void main(void)

{
PORTB=0x01;
DDRB=0x00;
//Trigger SRF PORTC. 5,3,1
PORTC=0x00;
DDRC=0b00101010;
//Trigger SRF PORTD. 2,4,6
PORTD=0x00;
DDRD=0b00010101; //PORTD.0 == RX
TCCR0=0x00;
TCNT0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
TIMSK=0x01;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600 (Double Speed Mode)
UCSRA=0x02;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
ACSR=0x80;
SFIOR=0x00;
ADCSRA=0x00;
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;

// Global enable interrupts


#asm("sei")
delay_ms(500); // Time for starting TImer...
while (1)
{
// SCAN_US_WITH_COUNTER();
SCAN_SRF();
putchar(RESET_USART);
}
}

Vous aimerez peut-être aussi