SRF05 | PIC16F18875

-2

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;
                }
            }
        }
    }
}
    
pregunta Alan

1 respuesta

0

Aquí hay un ejemplo, he solicitado cosas de lcd y retrasos, el timer0 producirá 1 tick por microsegundo, desde la hoja de datos del sensor "uS / 58 = cm o uS / 148 = pulgadas.". La variable 'distancia' ahora debe almacenar la distancia en cm. Timer0 explicó enlace

/*
 * File:   newmain.c
 *
 * Created on 22 de mayo de 2018, 14:22
 */
#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)

#include <xc.h>
#include <stdint.h>
#include <stdio.h>
#include "../include/stdlib.h"
#include "LCD.h"
#include "stdio.h"
#define _XTAL_FREQ 4000000
unsigned int readUltrasonic(void);
void TMR0_Initialize(void);
void TMR0_Reload(void);
void TMR0_WriteTimer(uint16_t timerVal);
void TMR0_StopTimer(void);
void TMR0_StartTimer(void);
uint16_t TMR0_ReadTimer(void);
void sendPulse(void);

unsigned int distance = 0;
volatile uint16_t timer0ReloadVal16bit;
float time = 0;
char lcd_distance[10];

int main(void) {
    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  

    TMR0_Initialize();

    while (1) {
        //distance = readUltrasonic() / 1.7;
        distance = readUltrasonic() / 58.0; //
        time = 0;
        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) {

    while (1) {
        if (RA3 == 0) {
            break;
        }
    }
    sendPulse();
    while (1) {
        TMR0_StopTimer();
        if (RA3 == 1) {
            //TMR0 = 0;
            TMR0_WriteTimer(0);
            TMR0_StartTimer();

            while (1) {
                if (RA3 == 0) {
                    time = TMR0_ReadTimer();
                    return time;
                }
            }
        }
    }
}

void TMR0_Initialize(void) {
    // Set TMR0 to the options selected in the User Interface
    // T0CS FOSC/4; T0CKPS 1:1; T0ASYNC not_synchronised; 
    T0CON1 = 0x50;
    // TMR0H 0; 
    TMR0H = 0x00;
    // TMR0L 36; 
    TMR0L = 0x24;
    // Load TMR0 value to the 16-bit reload variable
    timer0ReloadVal16bit = (TMR0H << 8) | TMR0L;
    // Clearing IF flag
    PIR0bits.TMR0IF = 0;
    // T0OUTPS 1:1; T0EN enabled; T016BIT 16-bit; 
    T0CON0 = 0x90;
}

uint16_t TMR0_ReadTimer(void) {
    uint16_t readVal;
    uint8_t readValLow;
    uint8_t readValHigh;

    readValLow = TMR0L;
    readValHigh = TMR0H;
    readVal = ((uint16_t) readValHigh << 8) + readValLow;

    return readVal;
}

void TMR0_WriteTimer(uint16_t timerVal) {
    // Write to the Timer0 register
    TMR0H = timerVal >> 8;
    TMR0L = (uint8_t) timerVal;
}

void TMR0_Reload(void) {
    // Write to the Timer0 register
    TMR0H = timer0ReloadVal16bit >> 8;
    TMR0L = (uint8_t) timer0ReloadVal16bit;
}

void TMR0_StopTimer(void) {
    // Stop the Timer by writing to TMR0ON bit
    T0CON0bits.T0EN = 0;
}

void TMR0_StartTimer(void) {
    // Start the Timer by writing to TMR0ON bit
    T0CON0bits.T0EN = 1;
}
    
respondido por el johnger

Lea otras preguntas en las etiquetas