Estoy haciendo un proyecto para mi clase de electrónica. Estoy tratando de calcular una distancia usando un sensor ultrasónico SRF-05; pero el resultado en la pantalla es 0. Estoy usando una placa de desarrollo HPC Curiosity con PIC16F18875. código xc8. ¡Necesito tu ayuda! Gracias de antemano!
#include <xc.h>
#define _XTAL_FREQ 4000000
#include "../include/stdlib.h"
#include "LCD.h"
#include "stdio.h"
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Reset Selection bits (BOR enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = ON // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
unsigned int distance = 0;
char lcd_distance[10];
void sendPulse(void);
unsigned int readUltrasonic(void);
int main(int argc, char** argv) {
ANSELA=0; //Digital and Analogic
TRISAbits.TRISA3=1; //RA3 como INPUT (Echo)
TRISAbits.TRISA0=0; //RA0 como OUTPUT (Trigger)
LATAbits.LATA0=0;
//Configuracion para LCD Display
ANSELB=0; //Digital and Analogic
TRISB=0; //Declara puertos B como Output
PORTB=0; //Valor inicial del puerto B
Lcd_Init();
Lcd_Cmd(LCD_CLEAR);
Lcd_Cmd(LCD_CURSOR_OFF);
while(1) {
distance = readUltrasonic()/1.7;
sprintf(lcd_distance,"%d",distance);
Lcd_Out2(2,0, lcd_distance);
__delay_ms(300);
}
return (EXIT_SUCCESS);
}
void sendPulse(void) {
LATAbits.LATA0 = 1;
__delay_us(10);
LATAbits.LATA0 = 0;
}
unsigned int readUltrasonic(void){
unsigned int time = 0;
while(1) { if(RA3==0) { break; } }
sendPulse();
while(1) {
if(RA3==1)
{
TMR0 = 0;
while(1)
{
if(RA3==0){
time = TMR0;
return time;
}
}
}
}
}