PIC C Compiler and HC-SR04 Ultrasonic Sensor

Home Forums Microcontrollers PIC Microcontroller PIC C Compiler and HC-SR04 Ultrasonic Sensor

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #10154
    enhee enheelee
    Participant

    What is wrong. The below code. Please help me.

    #include "C:\Users\enhee\Documents\lcd16f873a.h"
    #define use_portb_lcd TRUE
    #include <LCD.C>
    #byte timer1=0x10
    #bit  trigger=0x07.2
    #bit  echo=0x07.1
    #bit on_off=0x10.0
    
    unsigned char zai=12;
    
    void main()
    {
      float a;
      set_tris_c(0x02);
      timer1=0x10;
    
      setup_adc_ports(NO_ANALOGS);
      setup_adc(ADC_OFF);
    
      setup_spi(SPI_SS_DISABLED);
    
      setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
      setup_timer_1(T1_DISABLED);
      setup_timer_2(T2_DISABLED,0,1);
    
      setup_comparator(NC_NC_NC_NC);
      setup_vref(FALSE);
    
      lcd_init();
      lcd_gotoxy(1,1);
      printf(lcd_putc,"zai medregch");
    
      for(;;)
      {
        set_timer1(0x0000);
        trigger=1;
        delay_us(10);
        trigger=0;
    
        while(!echo);       //Waiting for Echo
        on_off = 1;         //Timer Starts
        while(echo);        //Waiting for Echo goes LOW
        on_off = 0;         //Timer Stops
    
        a=get_timer1();
        a = a/58.82;
        a = a + 1;
    
        if(a>=2 && a<=400)   //Check whether the result is valid or not
        {
          lcd_gotoxy(1,2);
          printf(lcd_putc,"zai=%f cm",a-4);
        }
        else
        {
          lcd_gotoxy(1,2);
          printf(lcd_putc,"heterhii hol bna");
        }
        delay_ms(200);
      }
    }
    
    
    #10164
    Ligo George
    Keymaster

    Hello, It seems like you converted our program in the article Interfacing HC-SR04 with PIC Microcontroller to CCS C.

    In that tutorial I used prescaler : 2, so you should use :

    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);

    and you should change a-4 to a in printf().

    Please check the above program after making these changes and let me know whether it works or not. If it works, I can include it too in that article such that it will help others too.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
>