Interfacing HC-SR04 Ultrasonic Sensor with PIC 16F676 using Hi-Tech C – electroSome https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/feed/ Sat, 18 Mar 2023 04:56:38 +0000 https://bbpress.org/?v=2.6.9 en-US https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/#post-10434 <![CDATA[Interfacing HC-SR04 Ultrasonic Sensor with PIC 16F676 using Hi-Tech C]]> https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/#post-10434 Thu, 08 Jan 2015 08:09:45 +0000 sahu With reference to the article Interfacing HC-SR04 Ultrasonic Distance Sensor with PIC Microcontroller, I am trying to change the microcontroller to PIC 16F676.

#define _XTAL_FREQ 8000000

#define RS RC5
#define EN RC4
#define D4 RC0
#define D5 RC1
#define D6 RC2
#define D7 RC3

//#include <xc.h>
#include <htc.h>

#include "lcd.h";
#include <pic16f676.h>

// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
//#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
//#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//END CONFIG

void delayMs(int x);

void main()
{
  int a;

  TRISA = 0b00010001; //RA0 as Input PIN (ECHO)
  TRISC = 0x00; // LCD Pins as Output

  Lcd_Init();

  Lcd_Set_Cursor(1,1);
  Lcd_Write_String("Developed By");
  Lcd_Set_Cursor(2,1);
  Lcd_Write_String("electroSome");

  delayMs(3000);
  Lcd_Clear();

  T1CON = 0x10; //Initialize Timer Module

  while(1)
  {
    TMR1H = 0; //Sets the Initial Value of Timer
    TMR1L = 0; //Sets the Initial Value of Timer

    RA0 = 1; //TRIGGER HIGH
    __delay_us(10); //10uS Delay
    RA0 = 0; //TRIGGER LOW

    while(!RA1); //Waiting for Echo
    TMR1ON = 1; //Timer Starts
    while(RA1); //Waiting for Echo goes LOW
    TMR1ON = 0; //Timer Stops

    a = (TMR1L | (TMR1H<<8)); //Reads Timer Value
    a = a/58.82; //Converts Time to Distance
    a = a + 1;   //Distance Calibration
    if(a>=2 && a<=400) //Check whether the result is valid or not
    {
      Lcd_Clear();
      Lcd_Set_Cursor(1,1);
      Lcd_Write_String("Distance = ");

      Lcd_Set_Cursor(1,14);
      Lcd_Write_Char(a%10 + 48);

      a = a/10;
      Lcd_Set_Cursor(1,13);
      Lcd_Write_Char(a%10 + 48);

      a = a/10;
      Lcd_Set_Cursor(1,12);
      Lcd_Write_Char(a%10 + 48);

      Lcd_Set_Cursor(1,15);
      Lcd_Write_String("cm");
    }
    else
    {
      Lcd_Clear();
      Lcd_Set_Cursor(2,1);
      Lcd_Write_String("Out of Range");
    }
    delayMs(400);
  }
}
/************************************************************/

void delayMs(int x)
{
  int i;
  for (x ;x>0;x--)
  {
    for (i=0;i<=110;i++);
  }
}

I am using Hi-Tech C compiler and getting following errors.

Error [1347] ; 0. can’t find 0x21 words (0x21 withtotal) for psect “text142” in segment “CODE” (largest unused contiguous range 0x1E)
Error [1347] ; 0. can’t find 0x12 words (0x12 withtotal) for psect “text140” in segment “CODE” (largest unused contiguous range 0x7)
Error [1347] ; 0. can’t find 0xF words (0xf withtotal) for psect “text144” in segment “CODE” (largest unused contiguous range 0x7)

]]>
https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/#post-10437 <![CDATA[Reply To: Interfacing HC-SR04 Ultrasonic Sensor with PIC 16F676 using Hi-Tech C]]> https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/#post-10437 Thu, 08 Jan 2015 14:51:50 +0000 Ligo George You should use a PIC with higher memory or reduce the code size.

]]>
https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/#post-10438 <![CDATA[Reply To: Interfacing HC-SR04 Ultrasonic Sensor with PIC 16F676 using Hi-Tech C]]> https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/#post-10438 Thu, 08 Jan 2015 21:42:14 +0000 sahu what’s the way reduce the code size ?

]]>
https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/#post-10439 <![CDATA[Reply To: Interfacing HC-SR04 Ultrasonic Sensor with PIC 16F676 using Hi-Tech C]]> https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/#post-10439 Fri, 09 Jan 2015 03:54:28 +0000 Ligo George
  • Reduce code length
  • Remove unnecessary statemetns
  • Optimize the Lcd_Port() by using single statement, eg  : PORTC = a by making necessary changes in the circuit diagram.
  • ]]>
    https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/#post-10442 <![CDATA[Reply To: Interfacing HC-SR04 Ultrasonic Sensor with PIC 16F676 using Hi-Tech C]]> https://electrosome.com/topic/hc-sr04-ultrasonic-pic-16f676-hi-tech/#post-10442 Fri, 09 Jan 2015 07:53:58 +0000 sahu you- Remove unnecessary statements

    me- i think here every statements is necessary

    you- Optimize the Lcd_Port() by using single statement, eg  : PORTC = a by making necessary changes in the circuit diagram.

    me – if i use single statement, eg  : PORTC ,then no need Lcd_Port()

    ]]>