-1
\$\begingroup\$

I´m doing a project for my electronics class. I´m trying to calculate a distance using a SRF- 05 Ultrasonic Sensor; but the result in the display is 0 . I´m using a Curiosity HPC Development Board w/ PIC16F18875. xc8 code. I need your help! Thanks in advance!

#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;
                }
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ You're dividing by 1.7 and putting that value in an integer variable ? It's probably not the cause of your problem but shouldn't you use float ? And I didn't see you configure timer0 or include it at the top \$\endgroup\$ – Simeon R May 22 '18 at 6:58
  • \$\begingroup\$ your code is badly indented .... please correct it ..... the column of braces at the end of your code should be at different indent levels when done correctly \$\endgroup\$ – jsotola May 22 '18 at 6:59
  • \$\begingroup\$ I changed distance to float; the result is 0 yet. How I can configure TMR0 at the top? Thanks! \$\endgroup\$ – Alan May 22 '18 at 7:18
  • 2
    \$\begingroup\$ A note for others who might try to help: an identical question was posted at microchip.com/forums/m1052646.aspx, helpful suggestions were ignored, and the original post was deleted. \$\endgroup\$ – mlp May 25 '18 at 15:55
  • 1
    \$\begingroup\$ if this is for an electronics class, you should be able to show the scope traces for the pins on the US sensor. This is an important debugging step, and once you see the info yourself, you'll probably be better off. \$\endgroup\$ – Scott Seidman Nov 29 '18 at 14:02
0
\$\begingroup\$

Here is an example, i've ommited lcd stuff and delays, timer0 will produce 1 tick per microsecond, from sensor datasheet "uS/58=cm or uS/148=inches.". 'distance' variable should now store distance in cm. Timer0 explained http://ww1.microchip.com/downloads/en/DeviceDoc/51682A.pdf

/*
 * 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;
}
\$\endgroup\$
  • \$\begingroup\$ I made your example; but now the display dont show anything. \$\endgroup\$ – Alan May 23 '18 at 1:21
  • \$\begingroup\$ @Alan updated, check \$\endgroup\$ – johnger May 23 '18 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.