Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help with mikroc program that reads water flow meter.

Status
Not open for further replies.

moad

Newbie level 5
Joined
Jan 30, 2015
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
113
hello,

for the past week iv just started my first ever project to make a c program on the software called mikroc where i would get pulses in from the flow meter and calculate the flow rate of how much milliters flows every second which then outputs 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. please if any one knows how to make this work and whats my mistake i would really appreciate it. and sorry i am a beginner to this...

pic16f877a datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf
water flow meter datasheet: https://www.hobbytronics.co.uk/datasheets/sensors/YF-S201.pdf

PROGRAM:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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++;
      }
 
     }
 
        
}

 
Last edited by a moderator:

It's useless to connect a pulse output to an ADC input. Instead connect it to a counter input or interrupt capacble GPIO and make a frequency counter.
 

so which pin would be the best to connect to? sorry im just a beginner
 

Awesome, never knew that!! i changed it and now im getting values but there are lots of zeros...:bang:
link to image : https://obrazki.elektroda.pl/4171348600_1422625419.jpg
is this better? :D


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void main() { 
     volatile unsigned int value;
     char str [20];
     double calc;
     lcd_init();        //initiallise lcd
     ADC_Init();        // and to read in values
     ADCON1=6;
     TRISB=0;
     PORTB=0x00;
     TRISA.f4=1;        //setting the signal input
     PORTA.f4=0;
     while (1)
     {
      if (PORTA.F0= 1)   //if water flows through the meter
      {
       value = ADC_read(PORTA.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++;
      }
 
     }
 
        
}

 

1. T0CKI or T1CKI
2. INT
3. RB4 to RB7 unsing input change interrupt

- - - Updated - - -

I said "make a frequency counter", not use the ADC on a different pin.
 

IS THIS BETTER?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char array[17];
void main() {
 
ADCON1 = 6; // Changes PORTA to digital
CMCON = 7; // Disable analog comparators
PORTA = 0; // Reset port A
TRISA = 0xFF; // All portA pins are configured as inputs
TRISB = 0; // All port B pins are configured as outputs
PORTB = 0x0; // Reset port B
OPTION_REG.F5 = 1;     // Counter TMR0 receives pulses through the RA4 pin
//OPTION_REG.F3 = 1;      // Prescaler rate is 1:1
Lcd_Init(); // LCD display initialization
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD command (cursor off)
Lcd_Cmd(_LCD_CLEAR); // LCD command (clear LCD)
TMR0 = 0; // Reset timer/counter TMR0
 
while(1) { 
if (PORTA.F4=1){
TMR0++;
longinttostrwithzeros(tmr0,array);
Lcd_Out(2,1,array);
}
} }

 
Last edited by a moderator:

hello,

at firts ,Read some documents about Timer...

Code:
while(1) { 
if (PORTA.F4=1){
TMR0++;
longinttostrwithzeros(tmr0,array);
Lcd_Out(2,1,array);
}


let's the counter TMR0 do his job..
this TMR0 , used as a counter , will count every edge on his RA4 input.. ( don't forget to add a Pull up resistor , if needed)
and use another timer (Timer1) to count elpased time.. ( 1sec or 1mn or ???)
when timer1 fire on ..
get the TMR0 value and diplay it
and RAZ TmR0
restart timer1 for next measurment.
 

If 1 ltr flows you want to show 1000 ml flowed ? What if 1 ml flows ? Whether you place the flow sensor horizontally or vertically then quantity of water like 10 ml or 100 ml will not even rotate the wheel inside the sensor. If you are measuring flow of only small quantity fluid then I think it is not possible to do with the this sensor but if you want to measure like 3 liter per minute and show it as x ltr/hr then it can be done.
 

hello,


if frequency of signal is very low, you can use diredt counting without timer

Code:
unsigned long Compteur ;


..inits for hardware and LCD

Compteur=0;
while(1) { 
if (PORTA.F4==1)     // test needs == !
{
while(PORTA.F4==1);  // wait RA4 returns to zero
Compteur++; // yes you get a pulse
longinttostrwithzeros(Compteur,array);
Lcd_Out(2,1,array);
}
}


The frequency must be low enough ,because of the duration of convertion +LCD display !

so you will get a quantity of pulses, not a flow rate.
until you do a reset to restart from 0..
or go overcount of compter long value..
 

can you update the code. just now i try to build this project
 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top