[Moved] code for mikroc water flow sensor

Status
Not open for further replies.

Allif Izzat

Newbie level 4
Joined
May 25, 2015
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
51
hey there i need some example code for pic16f877a. i try search but all in adruino. my water flow sensor are G1/2 Water Flow sensor. pls help me...
 

re: code for mikroc water flow sensor

I think it is like a frequency counter. You find out the pulse per second and calculate liters/minute from that. How many pulses per liter does your sensor give ?
 

re: code for mikroc water flow sensor

the flow meters I have used gave a number of pules per per litre - by feeding this signal into a interrupt on change digital input (I used a PIC24) one can count pulses over a time period and determine flow rate etc
 
re: code for mikroc water flow sensor

@horace1

How do you calculate liters/minute and liters/hour ?

Do you count pulses for say 1 sec or 10 sec or 1 min and then calculate liters/min and liters/hr ?
 
Last edited:

re: code for mikroc water flow sensor

@horace1

How do you calculate liters/minute and liters/hour ?

Do you count pulses for say 1 sec or 10 sec or 1 min and then calculate liters/min and liters/hr ?
it depends on the flow rate and the number of pules/litre
we measured pulses over a tenth of a second to get flow rate (and maintained a running average) and also a total of pulses to give the volume of liquid transferred
 
re: code for mikroc water flow sensor

@horace1

This is my flow sensor datasheet. How can I write code for it. I need to calculate ltr/min and ltr/hr.

https://www.futurlec.com/FLOW40L0.shtml

from the data sheet for a meter mounted horizontaly the resolution appears to average 300 pulses/litre (you can do a more accurate correction later when you know the actual flow rate)
using interrupt on change you could maintain an unsigned long int variable (to avoid overflow) as a counter
measure the count over a minute and the calculation would be
ltr/min = count/300
 

re: code for mikroc water flow sensor

You mean I have to wait 1 minute to get the reading ?
 

re: code for mikroc water flow sensor

You mean I have to wait 1 minute to get the reading ?
depends on your flow rate - do you have sufficent pulses to give you an accurate reading over a shorter period
you could have two counters
1. measure over a second and maintain a running average?
ltr/min= 60*secondCounter/300
2. and over a minute
readings should be approximatly the same
 

re: code for mikroc water flow sensor

My flow is arounf 25 liter per minute and 330 pulses per liter.
 

re: code for mikroc water flow sensor

My flow is arounf 25 liter per minute and 330 pulses per liter.

this should give you 137pulses/second which should be sufficent to give you accurate readings
you will probably need floating point numbers for the final calculation of 0.416litres/second
 
re: code for mikroc water flow sensor

hello,



i did a similar application**broken link removed** on a Culligan
count of nb of liters and alarm ...
my flowmeter is the same as on my Culligan. (125 pulses per liter)
with PIC12F1840 , Bluetooth link to PC or Android tablett.
 
re: code for mikroc water flow sensor

the flow meters I have used gave a number of pules per per litre - by feeding this signal into a interrupt on change digital input (I used a PIC24) one can count pulses over a time period and determine flow rate etc

how to calculate the pulse. means i try but at output only display 2 value and not stable
 

re: code for mikroc water flow sensor

Use INT0 pin. Configure INT0 pin to detect low to high transition using INTEDG bit. Now in interrupt routine check like

Code:
if(INTF_bit) {

     INTF_bit = 0;
     counter++;
}

counter holds the value of pulses. Once every minute append the counter value to another unsigned long variable and clear the counter variable.
 

re: code for mikroc water flow sensor




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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;
 
// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD4_bit;
 
volatile int NbTopsFan;
 
 void rpm(){
 NbTopsFan++;  //This function measures the rising and falling edge of the
          }           //hall effect sensors signal
 
unsigned int temp_res;
char txt1[15];
float calc;
 
 
void main() {
TRISB=0;
 
LCD_Init();
 
TRISA=0xFF;
PORTA=1 ;
 
//Spec :Fosc/64, Use ADC Ch.0 with Ref Vdd&Vss, Right justified
ADCON0=0b10001001;  //0b10_000_0_x_1
ADCON1=0b11000100;  //0b1_1_xx_1110
ADC_Init();
do {
temp_res = ADC_Read(0); // Get 10-bit results of AD conversion
NbTopsFan = 0;
delay_ms(2000);
calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate      //calculation to show flow per minute
floattostr(calc,txt1);
lcd_out(1,1,"FLOWRATE: ");
lcd_out_cp(txt1);
 
 
delay_ms(500);
lcd_cmd(_lcd_cursor_off);
 
  } while(1);
}



this my code where to put interrupt or my code wrong?
 
Last edited by a moderator:

re: code for mikroc water flow sensor

Please zip and post the complete mikroC project file. I will make changes to the code. Is there ADC measurement also ? What sensor have you interfaced to ADC ?
 
request code for water flow sensor pic 16f1877a

Code:
// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD4_bit;

volatile int NbTopsFan;

 void rpm(){
 NbTopsFan++;  //This function measures the rising and falling edge of the
          }           //hall effect sensors signal

unsigned int temp_res;
char txt1[15];
float calc;


void main() {
TRISB=0;

LCD_Init();

TRISA=0xFF;
PORTA=1 ;

//Spec :Fosc/64, Use ADC Ch.0 with Ref Vdd&Vss, Right justified
ADCON0=0b10001001;  //0b10_000_0_x_1
ADCON1=0b11000100;  //0b1_1_xx_1110
ADC_Init();
do {
temp_res = ADC_Read(0); // Get 10-bit results of AD conversion
NbTopsFan = 0;
delay_ms(2000);
calc = (NbTopsFan * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate      //calculation to show flow per minute
floattostr(calc,txt1);
lcd_out(1,1,"FLOWRATE: ");
lcd_out_cp(txt1);


delay_ms(500);
lcd_cmd(_lcd_cursor_off);

  } while(1);
}

this my program but the output not have value can someone edit my code?
 

Re: request code for water flow sensor pic 16f1877a

I did something similar to this with a PIC16F917 on a Microchip Mechatronics board
https://www.microchip.com/Developmenttools/ProductDetails.aspx?PartNO=DM163029

the pulse from the motor opto speed sensor was fed into timer 1 clock external input and I calculated the RPM from the timer1 counter
timer0 was used to interrupt every 10mSeconds

these days I would tend to use interrupt on change input to count RPM pulses
Code:
#define _LEGACY_HEADERS
#include <htc.h>

// Setup configuration bits
__CONFIG (EC & WDTDIS & PWRTDIS  & UNPROTECT &  MCLREN & BORDIS & FCMEN & IESOEN);

volatile int tenMillSecCounter=0;               // incremented every 10mSec
static volatile int tenMillSecCounter2=0;       // incremented every 10mSec - used for delays
static volatile int revolutionTimer=0;          // used for counting revs/min or motor
volatile int tenthsSecond=0;                    // incremented every tenth of a second
volatile int second=0;                          // incremeneted every second

// Timer 0 is programmed to interrupt every 10mSeconds
//  T0 clock is FOSC/4 = 200000
//  with 256 prescaler decrements 2000000/256 times / second
//  to get an interrupt every T mSec the calculation for TMR0 overflow from 0xFF to 0 is
//    count = 255 - T * 2000/256
//    i.e. for 10mSec counter is
#define tenMillSec 177
static void interrupt isr(void)
{
    if (T0IF)                           // A TMR0 interupt occurred
    {
        TMR0=tenMillSec;                // increment counters every 10 mSec
        tenMillSecCounter2++;
        revolutionTimer++;
        if((++tenMillSecCounter % 10)==0) tenthsSecond++;
        if((tenMillSecCounter % 100)==0) { second++; tenMillSecCounter=0; }
    }
    T0IF=0;                             // clear TMR0 interrupt flag
}

// delay for count tenths of a second
void tenthsSecondDelay(int count)
{
    tenMillSecCounter2=0;                         // zero counter
    while( tenMillSecCounter2  < (count*10)) ;    // wait
}
        
// delay for count seconds
void secondDelay(int count)
{
    tenMillSecCounter2=0;                         // zero counter
    while( tenMillSecCounter2  < (count*100)) ;   // wait
}
        
// initalise clock, set port A for digital input, set up timer 0, etc
void systemInitialise(void)
{
        OSCCON=0x70;             // Fosc use oscillator 8MHz
        ANSEL=0;                 // set digital inputs
        // PORTB PULL-UP DISABLED, TMR0 Timer mode Clock Source Select bit FOSC/4,  
        // Prescaler is assigned to the Timer0 module & TMR0 RATE 1:256
        OPTION=0x87;             
        TMR0=tenMillSec;         // initialise timer 0 counter
        T0IE = 1;                // enable interrupt on TMR0 overflow from 0xFF to 0
        GIE = 1;                 // Global interrupt enable
 }


// initialise Timer 1 to count revolutions of the motor
void REVcounterInitialise(void)
{
        TRISC5=1;                  // RC5 is optical interrupter input
        T1CON=0x07;                // set Timer 1 prescaler to 1:2, Select external clock source, Timer 1 on
        TMR1L=0;                   // clear timer 1 counter
        TMR1H=0;
        revolutionTimer=0;         // clear rev timer
}

// read timer 1 to get revs/min of motor
int REVperMinute(void)
{
        int rpm = (TMR1H << 8) + TMR1L;                // read timer 1 counter
        // calculate RPM - revolutionTimer is in 1/100's of a second
        rpm = (int) (rpm * 6000L / revolutionTimer);   // calculate RPM
        revolutionTimer=0;                             // clear REV timer
        TMR1L=0;                                       // clear timer 1 counter
        TMR1H=0;
        return rpm;
}
 

Re: request code for water flow sensor pic 16f1877a

Here is the project. I made it using PIC18F26K22 for your sensor. It works fine.
 

Attachments

  • Flow Meter PIC18F26K22.rar
    126.3 KB · Views: 115

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…