Version:0.9 StartHTML:0000000105 EndHTML:0000032674 StartFragment:0000001460 EndFragment:0000032658
////////////////////////////////////////////////////////////////////////////////
//****************** http://www.technologuepro.com **************************//
// ISET Nabeul Le 01/06/2011 --------+------- //
////////////////////////////////////////////////////////////////////////////////
program ultrason;
///Variables globales
// distance
var distance_H : byte;
var distance_L : byte;
/// flag obstacle
var obstacle: byte;
Var distance : longint;
/// RS232
var dat : array[3] of char; // buffer for receving/sending messages
////////////////////////////////////////////////////////////////////////////////
//////////////////////////// RS485 //// /////////////////////////
////////////////////////////////////////////////////////////////////////////////
procedure RS232;
begin
if dat[0] = 'D' then
begin
dat[0] := '=' ;
dat[1]:=distance_H;
dat[2]:=distance_L;
UART1_Write_Text(dat);
end;
end;
////////////////////////////////////////////////////////////////////////////////
//////////////////////////// Interruption //// /////////////////////////
////////////////////////////////////////////////////////////////////////////////
procedure interrupt;
begin
if PIR1.CCP1IF = 1 then
begin
obstacle:=1;
end;
if PIR1.RCIF =1 then
begin
dat[0]:=UART1_Read();
RS232;
end;
PIR1:=0;
end;
////////////////////////////////////////////////////////////////////////////////
//////////////////////////// Initialisation //// /////////////////////////
////////////////////////////////////////////////////////////////////////////////
procedure Initialisation;
begin
obstacle:=0;
trisb.7:=0;
portb.7:=0;
trisa:=0;
sound_init(porta,7);
UART1_Init(9600); // initialize UART1 module
delay_ms(100);
porta:=0;
CMCON:=$07;
CCP1CON:=$05;
CCPR1L:=0;
CCPR1H:=0;
TMR1L:=0;
TMR1H:=0;
T1CON:=%00010100; // division par 2 horloge interne/2
// 1 ----> 4us
INTCON:=$C0;
PIE1:=$24;
end;
////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Envoi Signal ultrason ///////////////////////////
////////////////////////////////////////////////////////////////////////////////
procedure envoi_ultrason;
begin
T1CON.TMR1ON:=0;// desactivation timer
TMR1L:=0;
TMR1H:=0;
CCP1CON:=$05;
T1CON.TMR1ON:=1;// demarrage timer
portb.7:=1;delay_us(12);
portb.7:=0;delay_us(12);
portb.7:=1;delay_us(12);
portb.7:=0;delay_us(12);
portb.7:=1;delay_us(12);
portb.7:=0;delay_us(12);
portb.7:=1;delay_us(12);
portb.7:=0;delay_us(12);
portb.7:=1;delay_us(12);
portb.7:=0;delay_us(12);
portb.7:=1;delay_us(12);
portb.7:=0;delay_us(12);
portb.7:=1;delay_us(12);
portb.7:=0;delay_us(12);
portb.7:=1;delay_us(12);
portb.7:=0;delay_us(12);
end;
////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Reception echo ///////////////////////////
////////////////////////////////////////////////////////////////////////////////
Var temp : longint;
procedure lecture_echo;
begin
if obstacle=1 then
begin
distance_H := CCPR1H;
distance_L := CCPR1L;
porta:=$FF;
distance:= 50*distance_H;
if distance < 100 then temp := 50;
if distance > 100 then temp := 150;
if distance > 150 then temp := 400;
if distance > 200 then temp := 600;
if distance > 300 then temp := 900;
if distance > 400 then temp := 1200;
if distance > 2000 then distance := 2000;
sound_play(1000,15);
vdelay_ms(temp);
porta:=$00;
end;
end;
////////////////////////////////////////////////////////////////////////////////
/////////////////////////// enclenchement ///////////////////////////
////////////////////////////////////////////////////////////////////////////////
procedure Enclenchement;
begin
obstacle:=0;
distance_H := 0;
distance_L := 0;
PIE1:=$24;
end;
////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Declenchement ///////////////////////////
////////////////////////////////////////////////////////////////////////////////
procedure Declenchement;
begin
PIE1:=$20;
T1CON.TMR1ON:=0;// desactivation timer
end;
////////////////////////////////////////////////////////////////////////////////
/////////////////////////// Process ///////////////////////////
////////////////////////////////////////////////////////////////////////////////
procedure Process;
var i : byte ;
begin
for i:=0 to 3 do
begin
porta.i:=1;
Enclenchement;
envoi_ultrason;
delay_ms(20);
Declenchement;
lecture_echo;
delay_ms(20);
end;
for i:=0 to 3 do
begin
porta.i:=0;
Enclenchement;
envoi_ultrason;
delay_ms(20);
Declenchement;
lecture_echo;
delay_ms(20);
end;
end;
////////////////////////////////////////////////////////////////////////////////
//////////////////////////// Programme principal ////////////////////////////
////////////////////////////////////////////////////////////////////////////////
begin
Initialisation;
while true do
begin
Process;
end;
end.