COUNT is not counting again


Closed Thread
Results 1 to 34 of 34

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Hello, here is some spaghetti, you can remove the extra junk but it counts.
    Code:
    '*******************************************************************************
    '
    'jellis00
    ' 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                                    *
    '*******************************************************************************
    '
    '
    '
    ' -----[ Device Declaration ]--------------------------------------------------*
    ' __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF &_CP_OFF
    INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
    ' -----[ 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
    GIE VAR INTCON.1
    OPTION_REG = %01110000    ' CLOCK ON RISING EDGE  'enable timer 0 count mode on RA.2
        
    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
        INT_Handler INT_INT, _Interrupt_Handler, PBP, yes
      endm
      INT_CREATE ; Creates the interrupt processor
      INT_ENABLE  TMR0_INT     ; enable Timer 0 interrupts
      INT_ENABLE   INT_INT     ; enable external (INT) interrupts
    ENDASM
    ' 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
     GIE = 0         ' Clear interrupt flags
    ' -----[ Main Code ]-----------------------------------------------------------*
    
    flush:if portA.1 = 1 then
           
          goto main
          else
          endif  
          goto flush
    MAIN:
        IF i < k Then
        goto main        'k is 15 pulse threshold
    Else
            if i >= k then
            PULSOUT PORTC.3,1000  ' Generate required 10 msec pulse to RC3
            WRITE 7,i  'Store updated count result
            I=0 ' CLEAR COUNTER FOR NEXT FLUSH
        ENDIF
        endif
        GOTO flush   'Main program loops while waiting for RA1 on change interrupt to count pulses
    Disable 
    Interrupt_Handler:   ' only get here if interrupt occured
            
            PULSOUT PORTC.0,500         ' Blink the LED once
            i = i + 1    'Increment pulse count value
    GIE = 0 
    @ INT_RETURN ;    Resume                     ' Return to main program
    
    
    Enable
    
    END
    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.

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


    Did you find this post helpful? Yes | No

    Default

    Non interrupt version;
    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
        TMR0 = 0        ' clear TMR0 count
        PULSOUT PORTC.3,1000  ' Generate required 10 msec pulse to RC3
       ' WRITE 7, TMR0   ' write count if you need to?
        HIGH PORTC.0    ' LED toggle stuff
        PAUSE 500
        LOW PORTC.0 
      ENDIF
      GOTO Main
      
      END
    With DT interrupts; Note that now you need to use the MPASM
    assembler & config settings for MPASM.
    Code:
     @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
      
      INCLUDE "DT_INTS-14.bas"     ; Base Interrupt System
      INCLUDE "ReEnterPBP.bas"     ; Include if using PBP interrupts
    
    ASM
    INT_LIST  macro    ; IntSource,   Label,     Type, ResetFlag?
          INT_Handler    TMR0_INT,  _FlusO_Matic, PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
      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 = 241        ' preload TMR0 to overflow after 15 counts
        
      @ INT_ENABLE TMR0_INT ; enable TMR0 counter interrupt
      
    Main:
      PAUSE 1
      GOTO Main
      
    FlusO_Matic:
      TMR0 = 241       ' reload TMR0 to overflow after 15 counts
      PULSOUT PORTC.3,1000  ' Generate required 10 msec pulse to RC3
     ' WRITE 7, TMR0   ' write count if you need to?
      @ INT_RETURN
      
      END
    Then of course, you can do pretty much the same by just watching the Timer0
    interrupt flag;
    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 = 241        ' preload TMR0 to overflow after 15 counts
      
      TMRO_INT_FLAG VAR INTCON.2 ' Timer0 overflow flag bit   
      INTCON = 0        ' not using interrupts + clear Timer0 int flag
      
    Main:
      WHILE TMRO_INT_FLAG = 0
      WEND
      PULSOUT PORTC.3,1000  ' Generate required 10 msec pulse to RC3
     ' WRITE 7, TMR0   ' write count if you need to?
      TMRO_INT_FLAG = 0 ' clear overflow flag
      TMR0 = 241        ' reload TMR0 to overflow after 15 counts
      GOTO Main
      
      END
    And, last, but not least, there's the old stand-by ON INTERRUPT;
    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 = 241        ' preload TMR0 to overflow after 15 counts
      
      TMRO_INT_FLAG VAR INTCON.2 ' Timer0 overflow flag bit   
      INTCON = %10100000 ' enable global & Timer0 interrupt
      
      ON INTERRUPT GOTO FlushOMatic
      
    Main:
      PAUSE 1
      GOTO Main
      
      DISABLE  
    FlushOMatic:
      PULSOUT PORTC.3,1000  ' Generate required 10 msec pulse to RC3
     ' WRITE 7, TMR0   ' write count if you need to?
      TMRO_INT_FLAG = 0 ' clear overflow flag
      TMR0 = 241        ' reload TMR0 to overflow after 15 counts
      RESUME
      ENABLE
        
      END
    All kept really simple & fairly easy to follow/modify...;o}
    Regards,

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

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


    Did you find this post helpful? Yes | No

    Default COUNT is not counting again

    Bruce, I really liked the code you posted for a Non-interrupt version to do my counting. I prefer it over the other options you posted, since I won't get into the problems I have seen in trying to handle interrupts in noisy signal envoirnments. It is concise with minimal executable statements, low memory requirement and should execute efficiently.
    It looked to me like it should run with my PIC16F690 almost as is. So I set up a pinout so that my momentary push button switch that I have been using to simulate the external sensor pusles will connect the RA2 pin to ground each time I depress the switch. I presumed this would be enough of a "clock" transition to increment the TMR0 counter. However, I haven't yet been able to see it run since I am having a compile problem with assembler errors that I can't understand or trace. I am attaching a screen snapshot (.jpg image) that shows the errors in lower left. Can you clarify what these errors are telling me?
    Attached Images Attached Images  

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


    Did you find this post helpful? Yes | No

    Default

    Make sure you're using the PM assembler, and not MPASM. Or change to the MPASM
    config line I've already shown in the DT interrupts version.
    Regards,

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

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


    Did you find this post helpful? Yes | No

    Unhappy COUNT is not counting again

    Bruce,
    OK, I am using PBP compiler via MicroStudio that uses MSPM assembler. So I made the config change. Now I get a different, but single assembler error: " Error[118]: Overwriting previous address contents[2007]" When I click on it, it points to the @__config statement.
    Again I don't know what this is telling me...can you advise.
    BTW, is there anywhere that you can get error listings for the MSPM assembler so you know what they mean?

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


    Did you find this post helpful? Yes | No

    Default

    Steve

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

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


    Did you find this post helpful? Yes | No

    Default Count

    Thanks, Mister e, for the link. The article by Melanie on how to set configuration fuses answered my error question and also a lot of others I have had about the subject. I edited my .incl file per her instructions to eliminate overwrite by the @__config statement and it now compiles without errors.
    Now that it finally runs, I have another question for Bruce. The Non-interrupt code runs fine, but I am not seening any pulse counts show up in TMR0 value when I write it during execute and then use READ in PICKkit2 to read the memory address....it is continuously zero. I thought maybe it was because my simulation mode for pulses wasn't working so I put a voltmeter on the RA2 pin while running the program and depressing the switch button to simulate pulses. What I saw was that RA2 was at 1.8 volts when the switch was open and of course it went to ground when I depress it. Isn't the 1.8 volt transition enough to trigger the TMR0 to count the pulses?

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


    Did you find this post helpful? Yes | No

    Default COUNT is not counting again

    Bruce, I see now in 16F690 data sheet that for TTL the minimum Vih threshold is 2.0 volts and for Schmidt trigger it is 0.8 volts. Would my switch grounding interface appear to RA2 as TTL or Schmidt trigger? Since the RA2 input is 1.8 v when open and I am grounding it to create a simulated pulse, I don't know whether this is enough downward transition or not. If not, what can I do about it. I thought the open circuit voltage level of RA2 would be over 2.0 volts.

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


    Did you find this post helpful? Yes | No

    Default

    Do you see where it states TMR0 = 0 ' clear TMR0 count

    This clears the timer0 count. If you need to write the timer0 count value to EEPROM first,
    then simply move the TMR0 = 0 below where you have WRITE 7, TMR0. Then you will clear
    the TMR0 count after you write it to EEPROM.

    It's not rocket science...;o}

    A TTL input requires 2V to VDD. As long as power is 4.5V ≤ VDD ≤ 5.5V, which just means
    VDD is greater than or equal to 4.5V and less than or equal to 5.5V. Then it meets the Vih
    logic 1 threshold.

    A Schmitt Trigger tytpe input requires 0.8V * VDD to VDD.

    If you were operating from a 5V supply, then 0.8 * 5V = 4V, which is what you'll need to
    apply as a minimum to the input for it to recognize a logic 1.

    Ground is ground. Your switch is for sure taking the input to ground.

    With 4.5V ≤ VDD ≤ 5.5V, Vil is met when the input is at Vss, up to 0.8V. But check the
    column under Conditions to be sure. It varies a bit with different VDD levels.

    Look at PINOUT DESCRIPTION – PIC16F690. Now look at RA2 for what type of input T0CKI
    is under the Input Type column.

    Now look at RA5. When you use RA5 as a general purpose I/O-pin, it only needs TTL level
    voltages. When you flip the switch to make it the Timer1 clock input, now it's a Schmitt
    Trigger input.

    Check out the BLOCK DIAGRAM OF RA5 to find out why.

    It's all right there. Just takes time to learn how to find & decipher it all...;o}

    And, when that doesn't work, download the latest Errata sheet for the PIC you're using.
    Microchip screws up once in a while, and makes changes to datasheets....;o}
    Last edited by Bruce; - 8th May 2009 at 04:55. Reason: And, when that doesn't work...!
    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