|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#if defined(__XC)
|
|
#include <xc.h> /* XC8 General Include File */
|
|
#elif defined(HI_TECH_C)
|
|
#include <htc.h> /* HiTech General Include File */
|
|
#elif defined(__18CXX)
|
|
#include <p18cxxx.h> /* C18 General Include File */
|
|
#endif
|
|
|
|
#if defined(__XC) || defined(HI_TECH_C)
|
|
#include <stdint.h> /* For uint8_t definition */
|
|
#include <stdbool.h> /* For true/false definition */
|
|
#endif
|
|
|
|
// Definition de l'oscillateur comme oscillateur externe
|
|
#pragma config FOSC = INTOSCIO
|
|
|
|
// Desactivation du WATCHDOG
|
|
#pragma config WDTEN = OFF
|
|
#pragma config MCLRE = OFF
|
|
#pragma config LVP = OFF
|
|
|
|
// determination de la frequence
|
|
#define _XTAL_FREQ 16000000L
|
|
|
|
///////////////////////////////// ULTRASON /////////////////////////////////////
|
|
|
|
int calcul_distance()
|
|
{
|
|
// Declaration des variables
|
|
|
|
int distance = 0;
|
|
int timer = 0;
|
|
TMR1H = 0;
|
|
TMR1L = 0;
|
|
|
|
LATBbits.LATB0 = 1; // Impulsion de 10 us sur le trigger
|
|
__delay_us(10);
|
|
LATBbits.LATB0 = 0;
|
|
|
|
while(PORTBbits.RB1==0); // Mesure de la dur?e entre l'envoi et la r?ception du signal
|
|
T1CONbits.TMR1ON = 1;
|
|
while(PORTBbits.RB1==1);
|
|
timer=TMR1;
|
|
T1CONbits.TMR1ON = 0;
|
|
|
|
distance=timer/17; // Calcul de la distance en cm
|
|
return distance;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
//////////////////////////////// FONCTION MAIN /////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
{
|
|
// Declaration et configuration des broches
|
|
|
|
TRISDbits.TRISD7 = 0; // Broche D7 = LED t?moin, ultrason
|
|
ANSELDbits.ANSD7 = 0;
|
|
LATDbits.LATD7 = 0;
|
|
|
|
PORTD = 0; // PORTD initialis? ? 0
|
|
|
|
TRISBbits.TRISB0 = 0; // Broche B0 = trigger ultrason
|
|
ANSELBbits.ANSB0 = 0;
|
|
PORTBbits.RB0 = 0;
|
|
|
|
TRISBbits.TRISB1 = 1; // Broche B1 = echo ultrason
|
|
ANSELBbits.ANSB1 = 0;
|
|
PORTBbits.RB1 = 0;
|
|
|
|
// Configuration du timer
|
|
T1CKPS0 = 0;
|
|
T1CKPS1 = 0;
|
|
T1CONbits.TMR1CS = 0; // L'horloge locale est l'horloge source
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
//////////////////////////* PROGRAMME PRINCIPAL *///////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**/
|
|
while(1)
|
|
{
|
|
|
|
__delay_ms(10);
|
|
|
|
int dist = calcul_distance();
|
|
|
|
|
|
|
|
if (dist<10){ // valeur de distance ? d?finir en cm
|
|
LATD7=1;
|
|
}
|
|
else {
|
|
LATD7=0;
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|