ArmandoCircuitos
Inicio
Electronica
Programacion
Diagrama
Codigo
#define Triger PORTA.F1 #define echo PORTA.F0 //Pines de salida para el LCD sbit LCD_RS at RD2_bit; sbit LCD_EN at RD3_bit; sbit LCD_D7 at RD7_bit; sbit LCD_D6 at RD6_bit; sbit LCD_D5 at RD5_bit; sbit LCD_D4 at RD4_bit; //Bits de configuración TRIS sbit LCD_RS_Direction at TRISD2_bit; sbit LCD_EN_Direction at TRISD3_bit; sbit LCD_D7_Direction at TRISD7_bit; sbit LCD_D6_Direction at TRISD6_bit; sbit LCD_D5_Direction at TRISD5_bit; sbit LCD_D4_Direction at TRISD4_bit; void main() { char Text[7]; unsigned int cm, tiempo; ADCON1 = 0x86; TRISA.F1 = 0; //Triger TRISA.F0 = 1; //Echo Lcd_Init(); //Inicio del LCD. Lcd_Cmd(_LCD_CURSOR_OFF); //Borrado del cursor. Lcd_Out( 1, 1, "Sensor Distancia"); //Impresión de texto. while(1){ //Bucle infinito. TMR1H = 0xB1; TMR1L = 0xDF; Triger = 1; //activa el triger Delay_us(10); //espera 10 milisegundos Triger = 0; //desactiva el triger while(echo == 0); T1CON.TMR1ON = 1; //activa el Timer1 while((echo == 1)&&(PIR1.TMR1IF == 0)); //mientra Echo este activo T1CON.TMR1ON = 0; //desactiva el Timer1 PIR1.TMR1IF = 0; //desactiva la bandera del Timer1 tiempo = (TMR1H << 8) | TMR1L; //Almacena el valor del Timer1 en tiempo cm = (tiempo - 45535) / 58; //conversion del tiempo del HCSR04 en distancia IntToStr( cm, Text ); //Se convierte el número entero a una cadena de caracteres. Lcd_Out( 2, 1, Text); //Se imprime el resultado. Lcd_Out( 2, 7, "cm"); delay_ms(500); //Retardo de 500m segundos. } }
Descarga del archivo
Descargar archivo