Home › Forums › Microcontrollers › PIC Microcontroller › Interfacing HC-SR04 Ultrasonic Sensor with PIC 16F676 using Hi-Tech C
Tagged: 16F676, HC-SR04, PIC, Sensor, Ultrasonic
- This topic has 4 replies, 2 voices, and was last updated 8 years, 2 months ago by sahu.
-
AuthorPosts
-
January 8, 2015 at 1:39 pm #10434sahuParticipant
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)January 8, 2015 at 8:21 pm #10437Ligo GeorgeKeymasterYou should use a PIC with higher memory or reduce the code size.
January 9, 2015 at 3:12 am #10438sahuParticipantwhat’s the way reduce the code size ?
January 9, 2015 at 9:24 am #10439Ligo GeorgeKeymaster- 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.
January 9, 2015 at 1:23 pm #10442sahuParticipantyou- 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()
-
AuthorPosts
- You must be logged in to reply to this topic.