Está en la página 1de 3

Diagrama

El diagrama que presentamos será el siguiente que usaremos un sensor ultrasónico para las
mediciones que serán mostradas por la LCD, lo que veremos recurrentemente será el botón de
reset

Programación
1 #include <xc.h>
2 #include "config.h"
3 #define _XTAL_FREQ 48000000
4 #include "flex_lcd.h"
5 #include <stdio.h>
6
7 int x;
8 int a;
9 char pp[16];
10 float t,d;
11
12 void main(void) {
13 OSCFRQ=0b0111; //48MHz
14
15 //Puertos Sensor
16 ANSELBbits.ANSELB2=0;
17 ANSELBbits.ANSELB3=0;
18 TRISBbits.TRISB2=0; //Trigger salida
19 TRISBbits.TRISB3=1; //Echo entrada
20
21 //Puertos LCD
22 ANSELA=0;
23 TRISA=0;
24
25 PORTA=0;
26 PORTB=0;
27
28 //Timer 3
29 //T3CONbits.CKPS=0b11; //Prescaler de 8
30 T3CONbits.CKPS=0b11; //Prescaler de 1
31 // T3CONbits.NOT_SYNC=1; //Asincrono
32 T3CONbits.RD16=1; //Escritura y lectura de 16 bits
33 T3CLKbits.CS=0b00010; //Fosc/4
34 // T3CLKbits.CS=0b00101; //500kHz
35 // TMR3H=0b00000000;
36 // TMR3L=0b00000000;
37 // PIR5bits.TMR3IF=0; //Limpiamos bandera
38 // T3CONbits.ON=0; //Activar timer 3
39
40 //LCD
41 Lcd_Init();
42 Lcd_Cmd(LCD_CLEAR);
43 Lcd_Cmd(LCD_BLINK_CURSOR_ON);
44 __delay_ms(500);
45 Lcd_Cmd(LCD_CURSOR_OFF);
46 Lcd_Out(1,2, "Distancia de");
47 __delay_ms(1000);
48 Lcd_Out2(2, 3, "un objeto");
49 __delay_ms(2000);
50 Lcd_Cmd(LCD_CLEAR);
51
52 //Iniciamos la programacion
53 while (1){
54
55 LATBbits.LATB2=1;
56 __delay_us(10);
57 LATBbits.LATB2=0;
58 //Activar timer 3
59
60 while (PORTBbits.RB3==0);
61
62 TMR3L=0b00000000;
TMR3H=0b00000000;
63
T3CONbits.ON=1;
64
65
while (PORTBbits.RB3==1);
66
T3CONbits.ON=0;
67
68
69
x= TMR3L ;
70
x|=TMR3H<<8;
71
72
73
t=(x*0.000000166)/2;
74
d=(t*34300)-1;
75
//Activar timer 3
76
77
// t=(x*0.000000166)/2;
78
// d=(t*343000);
79
80
Lcd_Out(1,1,"Distancia");
81
82
sprintf(pp, "%0.3fcm", d);
83
Lcd_Out2 (2,1,pp);
84
__delay_ms(100);
85
86
TMR3L=0b00000000;
87
TMR3H=0b00000000;
88
t=0;
89
d=0;
90
91
92
}
93
return;
94

También podría gustarte