Need help making a c program that reads flow meter

cmicrochipmicrocontrollerpicprogramming

For the past week, i've just started my first ever project to make a C program for a PIC16F877A under MikroC, where I would get pulses in from the flow meter and calculate the flow rate, then output it to the lcd.

However, when i connected it to PORTD of the PIC16f877A RD0, it gave random values everytime that go up and down on the lcd. What's wrong?

Water flow meter datasheet and program:

void main() { 
     volatile unsigned int value;
     char str [20];
     double calc;
     lcd_init();        //initiallise lcd
     ADC_Init();         // and to read in values
     TRISD.f0=1;        //setting the signal input
     PORTD.F0=0;
     while (1)
     {
      if (PORTD.F0= 1)   //if water flows through the meter
      {
       value = ADC_read(PORTD.F0);  //read from RD0 and assign to value read
       delay_ms(100);                 //delay so i could see value on screen
       calc = (value * 60 / 7.5);     //calculation to show flow per minute

       byteToStr(calc,str);
       lcd_out (1,1, str);      //output to screen
       value++;
      }

     }


}

Best Answer

I have no idea why it seems to you that you should be doing ADC reads to get your value.

What you need to be doing is counting the pulses from the meter per unit of time. So maybe you should be setting up an interrupt that comes once per second. Also setup a timer/counter channel in counter mode to increment once each time a pulse comes in from the meter. At each second you capture the counter value and re-zero for the counter for the next interval. The count of pulses per second is a direct reading of Hz and can be related to flow rate via the data sheet table.