Can't get COUNT to count


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378

    Default Can't get COUNT to count

    One part of my program won't work where I am using the COUNT command to count input pulses on an input pin. I know it isn't actually seeing any pulses and counting them because I am writing the count variable to EEPROM and then reading the EEPROM address after running the program. It continuously shows the value that was written to the EEPROM address as 0 (zero). I double checked to make sure that TRISA = $02 in my Register Settings so that RA1 is set to bit 1 and RA3 is set to bit 0.
    Can anyone tell me what is wrong with this code that keeps COUNT from counting input pulses?
    Thanks!
    Code:
    ' Start measuring output of flow meter at RA1 input pin
        i = 1           ' Preset pulse counter index to zero
        WHILE i <= 10 'Assume the value CIN is a pulse count from the sensor
            'Count the number of pulses from Hall Effect Magnetic Sensor in 10 sec
            Count PORTA.1,10000,CIN
            WRITE 5,CIN                ' Write count into EEPROM location 5
            'Slowly blink LED during water flow  
               HIGH PORTC.0             ' If we get here, Blink the LED slowly
               PAUSE 200
               LOW PORTC.0
               Sleep 2              ' Wait 2 seconds
            i = i + CIN              ' Increment pulse count for last 10 sec
        WEND        'When i = 10, required gallons have flowed by the meter

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    TRISA = $02 in my Register Settings so that RA1 is set to bit 1 and RA3 is set to bit 0.
    The above is pretty much TRISA= %10. Do you see the possible problem?

    And with out more info.. Chip and configs and ... I will have to assume for now.
    http://www.picbasic.co.uk/forum/showthread.php?t=561
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    jellis00, You know that the returned value from COUNT is a 16 bit value? I see you are only writing 1 byte of it to eeprom.. That limits the reading stored to 0 to 255 counts.. And its only the lower byte so it will roll over up to 255 times...

    Dave Purola,
    N8NTA

  4. #4
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Unhappy COUNT won't count

    Thanks for that catch, Dave. I fixed it by writing both CIN.Byte0 and CIN.Byte1 into EEPROM, but both addresses have zeros in them after running the program, so COUNT is still not counting pulses into the CIN variable.

    I will post my code in another reply, including declaratons as suggested to see if any of you can see my problem.

  5. #5
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Thumbs down COUNT won't count

    Mackrackit, my chip is a PIC16F690 as installed in a PICkit2. I am using PICBasic Pro as the programming language and MicroCode Studio as the programming IDE to the PICkit2. I will post my code at end of this posting in hopes you can help me determine whaI I am doing wrong.
    I didn't quite understand your implication about the register setting for setting RA1 to bit 1. I read the link you provided and I guess what you are saying is that I should set the register for all A/D to digital....??

    I read the data sheet for the PIC16F690 regarding setting the A/D to digital and saw a warning that connecting some analog signals to the input pin when it is set to be a digital input can damage the input. This caused me to measure the analog signal coming from the meter and it generates a pulse it is a +5.0 volt pulse. This is the same as VDD and within the specified max input voltage for the PIC16F960, so I don't think it would have damaged the chip....your opinion?? For this kind of input signal from the meter, should I set the register for all digital or all analog?? Since COUNT measures a pulse on the rising input signal, I presumed it would count each pulse input from the meter.

    I have made the changes suggested by Dave and also redid the ADC register settings as I understand them. Here is my code, some of it only stubbed out with pseudo code comments. It all works as intended except for the segment that requires COUNT to read the input pulses from PORTA.1 (RA1 pin). Can you please review and advise me where I am going wrong and why the COUNT statement is not actually counting any of the input pulses into the variable CIN??

    I greatly appreciate your help.
    Code:
    '**************************************************************************
    '*  Name    : Meter.BAS                                            *
    '*  Compiler: PICBASIC PRO Compiler microEngineering Labs                 *
    '*  Author  : John R. Ellis                                               *
    '*  Notice  : Copyright (c) 2009 LodeStar Associates, Inc.                *
    '*          : All Rights Reserved                                         *
    '*  Date    : 3/23/2009                                                   *
    '*  Version : 1.07                                                         *
    '*  This Program is for a PIC16F690 installed in a PICkit2 Starter Kit    *
    '*  Purpose: 1)  Monitor an input for an external Interrupt*
    '*          2)  On Interrupt, command latching solenoid to open valve *
    '*          3)  Measure flow meter output to determine when required no.      *
    '*                  gallons have passed                                   *
    '*          4)  On required gallons command latching solenoid to close    *
    '*           5)  Put Microcontroller back in Sleep State to await next *
    '*                  Interrupt                                       *
    '**************************************************************************
    'Device Declaration
    @device pic16F690, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off,mclr_off,protect_off
     
    'EEPROM PRESETS
    DATA @0,0
     
    'OSC DEFINE
    DEFINE OSC 4
     
    'Variables Declarations
    led  VAR PORTC.0    ' Set RA0 as output pin for LED
    valve VAR PORTC.3   ' Set RA3 as output pin for pulse to valve solenoid
    meter VAR PORTA.1   ' Set RA1 as input pin for Hall Sensor meter input pulses
     
    'Registers Settings
    TRISA = %00000010   ' Set PORTA.1 pin to input 
    TRISB = 0           ' Set all PORTB and PORTC pins to outputs
    TRISC = 0
    ADCON1 = 15         ' Set all ADC pins to digital....SHOULD THIS BE SO????
    PORTC = 0   ' Preset LEDs off
    PORTA.1 =0          ' Pre-set RA1 at Low value
     
    'Variables Initialization
     switchstate    VAR Bit ' Stores value of last measured switchstate
     k             CON 30  ' Calibration factor for flow meter...# pulses per gal
     i              VAR Byte    ' Index used in Gallon counter While loop
     CIN           VAR Word    ' Storage variable for count from COUNTER
    
    'Set INT Handler
    'ON INTERRUPT GOTO in_sig   'Provision for future interrupt input..commet out
     
    'Start...normal code here
    MAIN:
        switchstate = 1  ' Preset interrupt simulation Switch State
        'SLEEP = 65,535  ' Place in sleep state at startup..commented out in test 
        ' Microcontroller is in Sleep State waiting for external Interrupt
     
        'This code segment simulates external interrupt switch
        '*** Read State of Switch with 100 msec Debounce
        loop: 
     if PORTA.3 = 0 then    'Interrupt Switch is pressed on RA3 input
      pause 100     'Delay 100 msec
      if PORTA.3 = 0 then   'Test switch again for switch bounce
      switchstate = ~switchstate 'Toggle Switch State Flag
       hold
       if PORTA.3 = 0 then hold 'Wait for switch to be released
       goto in_sig       'Switch pressed...simulates external interrupt
      endif
     endif
        Goto loop       'If notpressed, monitor switch input
     
    in_sig:
        'Interrupt handler code here when actual external interrupts implemented
        'Disable                 ' No interrupts past this point
     
        'Put code here to Wakeup the Microcontroller from SLEEP on interrupt
        
        ' Put code here to start a timer to run for 50 secs as a fail safe to prevent
        ' overflow of tank in case of sensor failure...goes to Stop label on timeoout
        
        'Send PWM pulse to latching solenoid to open valve
            'Generates 10 millisec pulse on RC3 output pin
             'LOW PORTC.3            ' Initialize output pulse polarity
             PULSOUT PORTC.3,1000   ' Generate 10 msec pulse to RC3               
      
        ' Valve should be open at this point and water flowing
                
        ' Start measuring output of flow meter at RA1 input pin
        i = 0           ' Preset pulse counter index to zero
        WHILE i <= k 'Assume the value CIN is a pulse count from the sensor
            'Count the number of pulses from Hall Effect Magnetic Sensor in 10 sec
            COUNT PORTA.1,10000,CIN
            WRITE 5,CIN.Byte0                ' Write 10 sec count into EEPROM
            WRITE 6,CIN.Byte1
            'Slowly blink LED during water flow  
               HIGH PORTC.0             ' If we get here, Blink the LED slowly
               PAUSE 200
               LOW PORTC.0
               Sleep 1              ' Wait 1 seconds
            i = i + CIN.Byte0         ' Increment pulse count for last 10 sec
        WEND        'When CIN = k, required gallons have flowed by the meter
    Stop:
        ' Put code here to command solenoid to shut valve
    END

  6. #6
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Default COUNT won't count

    Found my problem after an email from melLabs tech support.

    I needed to set all inputs as digital by configuring all pins digital on the 16F690 by setting ANSEL = 0 and ANSELH = 0 in my Register Settings. Once I did this, everything works!
    This also answers my question about whether the input from the meter sensor needs to be set as an analog or digital input. Evidently, even though the input is an analog pulse, since it is within the max limits for digital input, having the input pin as digital is OK and precludes having to use code to sample the signal input with the A/D to determine the pulse presence that way. Don't quite understand why a digital input works with an analog signal, but if it works I won't argue with it.

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jellis00 View Post
    Found my problem after an email from melLabs tech support.

    I needed to set all inputs as digital by configuring all pins digital on the 16F690 by setting ANSEL = 0 and ANSELH = 0 in my Register Settings. Once I did this, everything works!
    This also answers my question about whether the input from the meter sensor needs to be set as an analog or digital input. Evidently, even though the input is an analog pulse, since it is within the max limits for digital input, having the input pin as digital is OK and precludes having to use code to sample the signal input with the A/D to determine the pulse presence that way. Don't quite understand why a digital input works with an analog signal, but if it works I won't argue with it.
    Hi jellis00, I gotta disagree about the signal being analog inasmuch as you are counting the tops of the waveforms of the hall effect switch. Your post stands as a wonderful example of why you want to read everyone else's posts even if their problem seems unrelated to your own problem, here is why: I just addressed this problem (ANSEL & ANSELH) here: http://www.picbasic.co.uk/forum/showthread.php?t=10788 Using the same development board and PIC.
    It might have saved you some hair Speaking only for myself, I do not have any extra to lose ! I am happy you found the problem.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. Doubling the pusle count
    By malc-c in forum mel PIC BASIC Pro
    Replies: 43
    Last Post: - 17th May 2012, 13:17
  2. COUNT is not counting again
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 19th June 2009, 04:52
  3. Count pulses between VARIABLE TIME
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th October 2007, 12:44
  4. Multiplex Display with count comand
    By mdaweb in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th October 2005, 04:09
  5. Count command
    By hawk72501 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 6th September 2005, 19:04

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts