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


    Did you find this post helpful? Yes | No

    Default COUNT is not counting again

    Joe, I enabled a WPUA.2 = 1 on RA2. However, with this change the voltage on RA2 when open circuit output is still at 3.2 vdc rather than Vih or close to Vdd. My code now looks like below. Interestingly the WRITE 7, TMR0 in the IF block never shows a value other than zero. However, just a couple of momentary switch activations will enter into the ELSE block where the WRITE 9, TRM0 statement shows a value of greater than K each time. So it looks like a lot of switch bounce going on, but the thing I don't understand is why the WRITE 7, TMR0 statement isn't showing any TMR0 values< K.
    I also notice that if I comment out the LED blink statements in the IF block, the WRITE 7 statement doesn't write anything because the memory address shows a continuous FF value. Strange things that I don't understand. But at least it looks like the TMR0 is counting whether the value shows up correctly or not.
    Tomorrow I am going to try to generate another method of simulating the external sensor input pulses to eliminate the switch bounce problem. I thought this might be possible by using one of the I/O pin outputs in a similar blink sequence loop as I am using for the LED, and then route that output into RA2. Do you think this would work? I will try to use one of the I/O pins that has a CMOS output.

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


    Did you find this post helpful? Yes | No

    Default COUNT is not counting again

    This time with the code:
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
      K CON 15    ' Calibration factor for flow meter = # pulses per gal
      
    ' 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         ' LEDs off, PULSOUT RC3 provides a high-going pulse
      PORTA = 0
      TRISC = %11110000 ' lower 4 pins outputs
      TRISA = %11111111 ' RA2 = 2TMR0 clock input for simulated meter pulse inputs
      TMR0 = 0          ' clear TMR0 count
      WPUA.2 = 1        ' enable weak pull-up on RA2       
      
    Main:
      IF TMR0 < K  THEN
        WRITE 7, TMR0   ' write count during testing
        ' Keep the valve open...do nothing..let water flow
        HIGH PORTC.0    ' Blink Light LED while water is flowing
        Pause 500
        LOW PORTC.0
        PAUSE 500
      ELSE
        WRITE 9, TMR0   ' write count during testing before clearing
        TMR0 = 0        ' clear TMR0 count
        PULSOUT PORTC.3,1000  ' Generate required 10 msec pulse to RC3 
      ENDIF
      GOTO Main
      END

  3. #3
    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
    This time with the code:
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
      K CON 15    ' Calibration factor for flow meter = # pulses per gal
      
    ' 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         ' LEDs off, PULSOUT RC3 provides a high-going pulse
      PORTA = 0
      TRISC = %11110000 ' lower 4 pins outputs
      TRISA = %11111111 ' RA2 = 2TMR0 clock input for simulated meter pulse inputs
      TMR0 = 0          ' clear TMR0 count
      WPUA.2 = 1        ' enable weak pull-up on RA2       
      
    Main:
      IF TMR0 < K  THEN
        WRITE 7, TMR0   ' write count during testing
        ' Keep the valve open...do nothing..let water flow
        HIGH PORTC.0    ' Blink Light LED while water is flowing
        Pause 500
        LOW PORTC.0
        PAUSE 500
      ELSE
        WRITE 9, TMR0   ' write count during testing before clearing
        TMR0 = 0        ' clear TMR0 count
        PULSOUT PORTC.3,1000  ' Generate required 10 msec pulse to RC3 
      ENDIF
      GOTO Main
      END
    try masking the interrupt just before write and reenable right after.
    Code:
    eecon1 = 6 ' enables eeprom write
    intcon.7 = 1 
    write  address, data
    intcon.7 =0
    Last edited by Archangel; - 8th May 2009 at 10:35.
    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.

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    FYI, there's a new DEFINE for that
    Code:
    DEFINE WRITE_INT 1
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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


    Did you find this post helpful? Yes | No

    Default

    This code;
    Code:
     @ device  pic16F690, intrc_osc_noclkout, wdt_off, mclr_off, protect_off 
      K CON 15     ' Calibration factor for flow meter = # pulses per gal
      
    ' 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         ' LEDs off, PULSOUT RC3 provides a high-going pulse
      PORTA = 0
      TRISC = %11110000 ' lower 4 pins outputs
      TRISA = %11111111 ' RA2 = TMR0 clock input for simulated meter pulse inputs
      TMR0 = 0          ' clear TMR0 count       
      
    Main:
      IF TMR0 < K  THEN
                        ' Keep the valve open...do nothing..let water flow
      ELSE
        PULSOUT PORTC.3,1000  ' Generate required 10 msec pulse to RC3
        WRITE 7, TMR0   ' write count if you need to?
        TMR0 = 0        ' clear TMR0 count
        HIGH PORTC.0    ' LED toggle stuff
        PAUSE 500
        LOW PORTC.0 
      ENDIF
      GOTO Main
      
      END
    With the schematic attached - works. It writes 15 to EEPROM after precisely
    15 press/release actions on the switch.

    Tested on the PICkit 2 low pin count demo board.

    The cap on RA3 is 0.01uF, which helps eliminate contact bounce from the switch.

    The switch on the PICkit 2 demo board connects to RA3, so a wire runs directly from
    RA3 to RA2. Remove the cap, and switch bounce causes problems. It's a really simple
    fix.

    If your circuit is powered from 5V, and you measure 5V between the PIC VDD/VSS pins,
    and you only see 3.2V on RA2 with a pull-up to VDD, then check your circuit. There's a
    problem somewhere with your wiring or connections.
    Attached Images Attached Images  
    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

    Default COUNT is not counting again

    Quote Originally Posted by mister_e View Post
    FYI, there's a new DEFINE for that
    Code:
    DEFINE WRITE_INT 1
    I don't see this DEFINE listed in Appendix B of the PICBASIC PRO manual, which supposedly lists all DEFINEs.
    Can you explain how you would use this to replace the code statements Joe lists to mask interrupts while writing? Do you just precede the WRITE statement with DEFINE WRITE_INT1? After the WRITE, do you still have to re-enable the interrupt? I guess if you have to do all this, I don't see how it is any more efficient than using Joe's 4 statements.

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


    Did you find this post helpful? Yes | No

    Default

    If DEFINE WRITE_INT isn't in your manual, then you probably have an older version of PBP
    or an old manual before this new define option was added.

    Newer versions of PBP include a check for this in the WRITE library. If you have DEFINE
    WRITE_INT 1 declared, it automatically disables interrupts before the write, and re-enables
    interrupts immediately after the write is complete.

    It just saves you from having to remember to do it yourself.
    Regards,

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

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


    Did you find this post helpful? Yes | No

    Thumbs up COUNT is not counting again

    PROBLEM SOLVED ON THIS THREAD.

    Thanks, Bruce, for clarification on DEFINE. I will use this in all future programs where I have write statements.

    Joe S., the 0.01 uF capacitor definitely solved my switch bounce problem. The code now works perfectly and always counts exactly 15 pulses when it activates the external pulse to the valve solenoid.

    Your guys are stupendous! I learned more in the interchanges during this thread about programming PIC MCUs than I have learned in weeks reading different sources.

    Thanks again and keep up the good work!

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    FYI, there's a new DEFINE for that
    Code:
    DEFINE WRITE_INT 1
    That should save someone some brain damage. Where did you learn that? Das Book ?
    Joe S., the 0.01 uF capacitor definitely solved my switch bounce problem. The code now works perfectly and always counts exactly 15 pulses when it activates the external pulse to the valve solenoid.
    Credit to where credit is due . . . BRUCE . . . always on target.
    After the WRITE, do you still have to re-enable the interrupt? I guess if you have to do all this, I don't see how it is any more efficient than using Joe's 4 statements.
    Maybe not more efficient, but way easier.
    Last edited by Archangel; - 8th May 2009 at 20:17.
    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.

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


    Did you find this post helpful? Yes | No

    Angry Now same working code won't work with PCB Pro??

    I thought I had this problem licked. Then I purchased PCB PRO and tried to run the same code that ran in PCB. I get all an "Overwriting prrevious address contents" assembly error and many "Symbol not previously defined erros" as related to the ANSEL, ANSELH, CM1CON and CM2CON statements as shown in the attached image. These errors sort of sound like a problem with the CONFIG statement not being commented out in the .inc file. I did make sure I commented out the @ __device statement in the 16F690.incl file that came with PCB PRO so it isn't overwritten by the !__config statement in the code.
    Can't figure thwhy this code now won't assemble. Can any of you advise me why my code would run in PCB and won't assemble in PCB PRO??

    Here is the current code as adapted from Bruce's "Non-Interrupt version" in his above post.
    Code:
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF 
      i var Byte     ' Indexer for use in For..Next loop
      K CON 15     ' Calibration factor for flow meter = # pulses per gal
      
    ' 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
      PORTA = 0         ' Clear the port register latches
      PORTB = 0
      PORTC = 0         ' LEDs off, PULSOUT RC3 provides a high-going pulse
    ' Set or clear the data direction registers
      TRISA = %11111111 ' RA2 = TMR0 clock input for simulated meter pulse inputs
      TRISB = %11111111 
      TRISC = %11110000 ' lower 4 pins outputs
    
      'VRCON = %00000000 ' turns the Vref Module OFF by clearing bit 7, 6, 4
      TMR0 = 0          ' clear TMR0 count       
      
    Main:
      IF TMR0 < K  THEN
                        ' Keep the valve open...do nothing..let water flow
      ELSE
        PULSOUT PORTC.3,1000  ' Generate required 10 msec pulse to RC3..close valve
        DEFINE WRITE_INT 1
        WRITE 7, TMR0  ' write count if you need to during testing
        TMR0 = 0        ' Clear timer count
        For i = 1 to 3 ' Blink LED 3x to indicate valve turned off
            HIGH PORTC.0    
            PAUSE 500
            LOW PORTC.0
        Next   
      ENDIF
      GOTO Main        ' Loop to Main to wait for next meter flow input
    Attached Images Attached Images  

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


    Did you find this post helpful? Yes | No

    Default

    The first error indicates you have not commented out the __config line if the 16F690.INC
    file that's used with the MPASM assembler. Make sure you do before compiling if you insert
    the MPASM @ __config directive in your code.

    The remaining errors indicate it's not finding the Microchip P16F690.INC file, which defines
    register locations/names.

    Make sure your path statement includes information on where you have MPLAB installed, or
    modify the 16F690.INC file to point directly to it.

    INCLUDE "C:\Program Files\Microchip\MPASM Suite\P16F690.INC"
    Regards,

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

Similar Threads

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