COUNT is not counting again


Closed Thread
Results 1 to 34 of 34

Hybrid View

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

    Unhappy COUNT is not counting again

    My excerpted code as listed below all works until it gets to the point where it is suppose to count the input pulses to pin RA1 during each loop of a REPEAT...UNTIL epoch and write the count valuable to a variable Cin. Then the accumulated count for all epochs is incremented for each epoch by adding Cin to i. The accumulated count is then suppose to be written to EEPROM after the accumulated count reaches its limit and exits the loop.
    I know the loop is working because I see the LED blinking on each loop and because the WRITE statement is actually writing over the FF value in EEPROM. However, the Cin value that is written is always zero (0), indicating that COUNT is actually not counting. Therefore, the loop is never exiting, because i=i+Cin is always zero and the WRITE 7,i statement is therefore never executed. For the life of me I can't figure out why this is happening.
    I had a similar problem when I first wrote this code and solved the problem by adding the missing ANSEL =0 and ANSELH = 0 statements, which are in this code now, so that can't be the problem. I checked my register settings and I can see no reason why COUNT is not counting. Can anyone see what is wrong?
    Code:
    ' -----[ Title ]-----------------------------------------------------------*
    '                                                                          *
    ' File...... Toilet_Meter.BAS                                              *
    ' Compiler.. PICBASIC PRO Compiler from microEngineering Labs              *
    ' Purpose... PIC16F690 microcontroller for Toilet Meter Project            *
    ' Author.... John R. Ellis,                         *
    ' Started... 2/20/2009                                                     *
    '                                           *
    ' Updated... Version 1.0.6 on 5/2/2009                                    *
    ' Program Size:  255 Words                                                 *                                                            *
     ' -----[ Program Description ]---------------------------------------------*
    '  This Program is for a PIC16F690 installed in a PICkit2 Starter Kit      *
    '  Purpose:   
    '  1)  Monitor an input for an external Interrupt from flush      *
    '  2)  On Interrupt, command latching solenoid to open valve  *
    '  3)  Measure flow meter output to determine when 1.6        *
    '       gallons have passed                                                *
    '  4)  On 1.6 gallons, command latching solenoid to close       *
    '  5)  Put Microcontroller back in Sleep State to await next    *
    '       Flush Interrupt                                                      *
    ' 
    ' This Program uses the PIC16F690 to control a solenoid operated water valve
    ' and to measure the volume of water flowing through a flow meter.
    ' This program places the MCU in sleep mode most of the time until it receives 
    ' an interrupt from the activation of the toilet flush handle that closes a 
    ' contact limit switch that grounds an input pin of the microcontroller as an 
    ' interrupt.  I/O pin connections to the PIC16F690 are as follows:
    '
    ' RA1...is connected as an input from the meter to count pulse inputs, which 
    '       are simulated by multiple manual depressions of a switch on the PICKIT2 
    '       demo board during program testing.
    ' RA3...is connected as an interrupt input from the FLUSH contact limit Switch.     
    ' RC0...is connected as an output to an LED and is slowly flashed when the valve 
    '       is open.  It is also flashed even more slowly when the battery is low.
    ' RC3...is connected as an output to the latching solenoid that opens or 
    '       closes the valve.  During program testing with the PICkit2 demo
    '       board it is connected to an LED to show when the pulse is sent.      
     
    ' -----[ Device Declaration ]----------------------------------------------
    '
    '@device pic16F690, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off,mclr_off,
    ' protect_off
     
    ' -----[ Revision History ]------------------------------------------------
    '
    ' -----[ Variables Initialization ]----------------------------------------
    '
     k             CON 15  ' Calibration factor for flow meter...# pulses per gal
     i             VAR Byte   ' Index used in Gallon counter Repeat...Until loop
     Cin           VAR Byte   ' Storage variable for count from COUNTER
     
    ' -----[ Initialization ]--------------------------------------------------
    '
     
    'Registers Settings
    Init:
    ADCON1 = %01110000  ' Set PORTA to digital I/O & FRC (clock derived from a 
    '                                 dedicated internal oscillator)
    TRISA = %00000010   ' Set all PORTA pins except RA1 to outputs
    TRISB = %00000000   ' Set all PORTB and PORTC pins to outputs
    TRISC = %00000000
    PORTC = %00000000 ' Pre-set PORTB pins low, turning LEDs off
    ANSEL = 0           ' Set PortA to digital I/O; Unselect any of the A/D channels
    ANSELH = 0
    TRISA.1 = 1         ' Set RA1 as input port for meter pulse inputs
    PortA.1 = 0         ' Pre-set RA1 at Low value
    INTCON = %10010000  ' Set INTCON register to enable interrupts
     
    ' -----[ Main Code ]-------------------------------------------------------
     MAIN:
        '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
        Cin = 0           ' Initialize pulse count variable to zero
        i = 0            ' Initialize total pulse count index to zero
        REPEAT ' Assume it takes k accumulated pulses for required gallons
            ' Count the number of pulses from Hall Effect Magnetic Sensor in 5 sec
            ' epochs and add count from each epoch to total accumulation and then 
            ' shutoff the valve when count meets i>k criteria.
            Count PORTA.1,5000,Cin
            WRITE 5,Cin    ' Write count into EEPROM location 5 during
                          ' pre-production testing
             'Slowly blink LED during water flow  
               HIGH PORTC.0         ' If we get here, Blink the LED once
               PAUSE 100
               LOW PORTC.0
            i = i + Cin              ' Add count to total pulse accumulation indexer
         'When i >= k, required gallons have flowed by the meter
         UNTIL i = k
         WRITE 7,i  'Store result as test of code..not in production
         'Write 8,i.LowByte
         GoTo ShutValve  
     
    ShutValve: 'Send PWM pulse to latching solenoid to close valve
                PULSOUT PORTC.3,1000   ' Generate required 10 msec pulse to RC3
      
        ' Valve should be closed at this point and no water flowing
    END

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    John,

    You've Enabled Interrupts, but there aren't any handlers defined.
    That usually causes everything to Lock-Up from "Continuous Interruptus".

    Interrupts have nothing to do with the COUNT statement.
    If it was to exit sleep mode? Don't enable the GIE bit.

    But the way you're doing it, you're going to miss a lot of pulses.
    It really should use interrupts to count the pulses.
    <br>
    DT

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


    Did you find this post helpful? Yes | No

    Default COUNT isn't counting again

    Darrell,
    Thanks for the reply. I commented out the INTCOM statement and the program still runs as I was describing above, but still doesn't count input pulses.
    As far as missing pulses, the rate at which the incoming pulses occur is much, much slower than the loop rate, so I don't think it will miss that many pulses.
    At any rate, I will look into how to do this with interrupts, but I still would like to figure out why the COUNT statement is not counting. Any ideas??

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


    Did you find this post helpful? Yes | No

    Default

    Hello jellis00,
    Have you verified the presence and level of the pulses you are counting?
    What happens if i = i + cin >15 then will it not overshoot the until and never make the statement true ? Maybe UNTIL i >= k would be better ?
    Looked at the data sheet, no schmitt triggers to worry about, porta on change interrupts are available. I notice your code does not loop, one shot as is. I really did not write any code <font color=red>AND I DID NOT TEST THIS,</font color> but here is what I was thinking you might do differently. I would actually make the int handler a bit smaller and tidier but you get the jist.
    Code:
    ' -----[ Title ]---------------------------------------------------------- -*
    '                                                                           *
    ' File...... Toilet_Meter.BAS                                               *
    ' Compiler.. PICBASIC PRO Compiler from microEngineering Labs               *
    ' Purpose... PIC16F690 microcontroller for Toilet Meter Project             *
    ' Author.... John R. Ellis,                                                 *
    ' Started... 2/20/2009                                                      *
    '                                                                           *
    ' Updated... Version 1.0.6 on 5/2/2009                                      *
    ' Program Size:  255 Words                                                  *                                                            *
     ' -----[ Program Description ]---------------------------------------------*
    '  This Program is for a PIC16F690 installed in a PICkit2 Starter Kit       *
    '  Purpose:   
    '  1)  Monitor an input for an external Interrupt from flush                *
    '  2)  On Interrupt, command latching solenoid to open valve                *
    '  3)  Measure flow meter output to determine when 1.6                      *
    '       gallons have passed                                                 *
    '  4)  On 1.6 gallons, command latching solenoid to close                   *
    '  5)  Put Microcontroller back in Sleep State to await next                *
    '       Flush Interrupt                                                     *
    ' 
    ' This Program uses the PIC16F690 to control a solenoid operated water valve
    ' and to measure the volume of water flowing through a flow meter.
    ' This program places the MCU in sleep mode most of the time until it receives 
    ' an interrupt from the activation of the toilet flush handle that closes a 
    ' contact limit switch that grounds an input pin of the microcontroller as an 
    ' interrupt.  I/O pin connections to the PIC16F690 are as follows:
    '
    ' RA1...is connected as an input from the meter to count pulse inputs, which 
    '       are simulated by multiple manual depressions of a switch on the PICKIT2 
    '       demo board during program testing.
    ' RA3...is connected as an interrupt input from the FLUSH contact limit Switch.     
    ' RC0...is connected as an output to an LED and is slowly flashed when the valve 
    '       is open.  It is also flashed even more slowly when the battery is low.
    ' RC3...is connected as an output to the latching solenoid that opens or 
    '       closes the valve.  During program testing with the PICkit2 demo
    '       board it is connected to an LED to show when the pulse is sent.      
     
    ' -----[ Device Declaration ]----------------------------------------------
    '
    '@device pic16F690, intrc_osc_noclkout, BOD_OFF, PWRT_OFF, wdt_off,mclr_off,
    ' protect_off
     
    ' -----[ Revision History ]------------------------------------------------
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
    ' -----[ Variables Initialization ]----------------------------------------
    '
     k             CON 15     ' Calibration factor for flow meter...# pulses per gal
     i             VAR Byte   ' Index used in Gallon counter Repeat...Until loop
     Cin           VAR Byte   ' Storage variable for count from COUNTER
     
    ' -----[ Initialization ]--------------------------------------------------
    '
     
    'Registers Settings
    Init:
    ADCON1 = %01110000  ' Set PORTA to digital I/O & FRC (clock derived from a dedicated internal oscillator)
                        ' 
    TRISA = %00000010   ' Set all PORTA pins except RA1 to outputs
    TRISB = %00000000   ' Set all PORTB and PORTC pins to outputs
    TRISC = %00000000
    PORTC = %00000000   ' Pre-set PORTB pins low, turning LEDs off
    ANSEL   = 0         ' Set PortA to digital I/O; Unselect any of the A/D channels
    ANSELH  = 0
    TRISA.1 = 1         ' Set RA1 as input port for meter pulse inputs
    PortA.1 = 0         ' Pre-set RA1 at Low value
    INTCON  = %10011000 ' Set INTCON register to enable interrupts
     
    ' -----[ Main Code ]-------------------------------------------------------
     on interrupt goto Int_Handler
     
     MAIN:
        '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
                
    Log:
         WRITE 7,i  'Store result as test of code..not in production
         'Write 8,i.LowByte
         GoTo ShutValve  
     
    ShutValve:                         ' Send PWM pulse to latching solenoid to close valve
                PULSOUT PORTC.3,1000   ' Generate required 10 msec pulse to RC3
    @ sleep
    Disable                                   ' Valve should be closed at this point and no water flowing
    Int_Handler:
        ' Start measuring output of flow meter at RA1 input pin
        Cin = 0           ' Initialize pulse count variable to zero
        i   = 0           ' Initialize total pulse count index to zero
        REPEAT ' Assume it takes k accumulated pulses for required gallons
            ' Count the number of pulses from Hall Effect Magnetic Sensor in 5 sec
            ' epochs and add count from each epoch to total accumulation and then 
            ' shutoff the valve when count meets i>k criteria.
            Count PORTA.1,5000,Cin
            WRITE 5,Cin    ' Write count into EEPROM location 5 during
                           ' pre-production testing
                           ' Slowly blink LED during water flow  
               HIGH PORTC.0         ' If we get here, Blink the LED once
               PAUSE 100
               LOW PORTC.0
            i = i + Cin              ' Add count to total pulse accumulation indexer
         'When i >= k, required gallons have flowed by the meter
         UNTIL i >= k
         goto Log
    return
    enable
    END
    Last edited by Archangel; - 5th May 2009 at 08:27.
    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.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    This could be really easy with Timer0 configured to count external pulses.

    Here's one version;
    Code:
    @  device  pic16F690, intrc_osc_noclkout, wdt_off, mclr_off, protect_off
    
      T0_CLK VAR PORTA.0 ' for testing, RA0 provides external clock input to RA2
      Time CON 100       ' pause delay time
      
    ' Setup Timer0 as an 8-bit counter with the clock input on RA2.
    ' 1:1 TMR0 prescaler
    ' TMR0 counts on high-to-low transitions
      OPTION_REG = %00111000
      
    ' A/D & Comparators disabled
      ANSEL=0      ' all digital
      ANSELH=0     ' analog module disabled
      CM1CON0=0
      CM2CON0=0
      
    ' Port setup
      PORTC = 0
      PORTA = 0
      TRISC = %11110000 ' lower 4 pins output TMR0 count to LEDs
      TRISA = %11111110 ' RA0 = pulse output to RA2 TMR0 clock input
             
    Main:
      TMR0 = 0          ' clear TMR0 count
      REPEAT
        HIGH T0_CLK     ' rising edge on T0CKI
        PAUSE Time
        LOW T0_CLK      ' trigger TMR0 counter on falling edge on T0CKI
        PAUSE Time
        PORTC = TMR0    ' show Timer0 external clock count on LEDs
      UNTIL TMR0 = 15
      PAUSE 1000
      GOTO Main
    If you prefer interrupts, just preload Timer0 to over-flow after 15 pulses, and do whatever
    you need in the interrupt.

    You don't need to worry about missing any pulses, and you can do lots of other stuff while
    waiting for the counter to trigger.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

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


    Did you find this post helpful? Yes | No

    Angry COUNT is not counting again

    Joe and Bruce, thanks for the excellent inputs! I didn't totally grasp Joe's suggested use of the TMR0 as a counter, mainly because the wiring on microcontroller board doesn't enable me to use RA0 to generate a clock input to RA2 as Bruce's approach suggests.
    However, I have gone back to the drawing board to try to count the input pulses from the external sensor by counting them as interrupts. I am enclosing my new code below for reference. I tried to set up the Int-Handler to test for either a RA2/INT or a RA1 on change interrupt by means of two IF_Then blocks. Unfortunately it doesn't appear to recognize any interrupts. I know this because I inserted a LED blink at the very start of the Int_handler to tell me when an interrupt has been recognized and it never blinks for either a RA2/INT or a RA1 on change interrupt. If the RA1 interrupt is received it would increment an index counter, i, for each pulse. I also put the write statement in the If_Then block for RA1 on change interrupt so I could use the PICkit2 to read the EEPROM memory address for the value and it never changes from the FF value. These two tests tell me that my interrupt handler isn't working. Can anyone tell me what I have done wrong from this code??
    Code:
    ' -----[ Device Declaration ]--------------------------------------------------*
    '
    @device  pic16F690, intrc_osc_noclkout, wdt_off, mclr_off, protect_off 
    
    ' -----[ Variables Initialization ]--------------------------------------------*
    '
     k             CON 15  ' Calibration factor for flow meter = # pulses per gal
     i             VAR Byte   ' Index used in Gallon counter Repeat...Until loop
     
    ' -----[ Initialization of Registers ]-----------------------------------------*
    '
    
    'Register Settings
    TRISA = %00001010   ' Set PORTA pins RA1 and RA3 to inputs, others as outputs
    TRISB = %00000000   ' Set PORTB pins as outputs
    TRISC = %01000000   ' Set PORTC pin RC6 to input, all others as outputs
    PORTC = %0000000    ' Pre-set PORTC LED pins low, turning LEDs off
    'A/D & Comparators disabled
        ADCON1 = %01110000  ' Set PORTA to digital I/O & FRC (clock derived from a 
                          ' dedicated internal oscillator)
        ANSEL=0             ' Set PortA to digital I/O
        ANSELH=0            ' Analog module disabled
        CM1CON0=0
        CM2CON0=0
    TRISA.1 = 1         ' Set RA1 as input port for simulated meter pulse inputs
    TRISA.3 = 1         ' Set RA3 as Master Reset input port
    PORTA.1 = 1         ' Pre-set RA1 at High value
    
    ' Setup  Interrupts
    INTCON = %10011000  ' Enable RA2/Int external interrupt and PORTA interrupts on
                        ' change
    IOCA = %00000010    ' Enable RA1 for interrupt on change for pulses received
                        ' from momentary switch, activation which simulates external HS1 sensor pulses
    on interrupt goto Int_Handler
    i=0                 ' Initialize counter index to zero
    
    ' -----[ Main Code ]-----------------------------------------------------------*
    MAIN:
        INTCON = %10011000         ' Clear interrupt flags
        IF i < k Then         'k is 15 pulse threshold
            ' Keep the valve open...do nothing..let water flow
        Else
            'i >= 15....1.6 gallons reached...Close the valve 
            PULSOUT PORTC.3,1000  ' Generate required 10 msec pulse to RC3
        ENDIF
        GOTO Main   'Main program loops while waiting for RA1 on change interrupt to count pulses
    
    Int_Handler:
        Disable                    ' No interrupts past this point
        'Blink LED on receipt of this interrupt  
            HIGH PORTC.0         ' Blink the LED once
            PAUSE 100
            LOW PORTC.0
        If INTCON = %10011010 Then  'RA2/INT external interrupt received
            PULSOUT PORTC.3,1000      ' Generate 10 msec pulse to RC3 to open valve
        Endif
        If INTCON = %10011001 Then  ' Flow meter interrupt pulse received on PORTA
            i = i + 1    'Increment pulse count value
            WRITE 7,i  'Store updated count result
        ENDIF
        Resume                     ' Return to main program
        Enable
    END
    Last edited by jellis00; - 7th May 2009 at 05:11.

Similar Threads

  1. Can't get COUNT to count
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 23rd March 2009, 23:14
  2. Remain counting while sending out a pulse
    By ultiblade in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 10th January 2007, 15:51
  3. hardware counting while software runs
    By Archangel in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 2nd October 2006, 03:26
  4. continious counting process (capture)
    By asynch in forum General
    Replies: 1
    Last Post: - 17th February 2006, 07:42
  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