Counter not counting !!!


Closed Thread
Results 1 to 25 of 25

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi

    well I am only getting about 3volt, so 0.8 * 5v = 4v, one volt away from triggering. I will a simple amplifier or inverter to the output of my sensor and feed that to the chip

    ken

  2. #2
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I think I am getting there but just not quite.
    your program works with the loop for incrementing. I removed the loop , because I have a sensor output, The output is going through an inverter, so now I get either 0 or 5v going into RA4.
    What I now see on the LCD is the incrementation going up high without any look at the input. it goes up to a 1000 in about 3 seconds with the sensor laying there not moving:

    Here is the code I have, mainly Bruce,s code with the addition of the LCD and external oscillation.


    @ DEVICE pic16F88, CCPMX_ON, MCLR_OFF

    DEFINE OSC 20 'use external 20mhz crystal
    ANSEL = 0 ' disable A/D so RA4 is digital
    CMCON = 7 ' disable comparators

    TRISA.4=1 ' RA4/T0CKI = input (Timer0 clock input)

    ANSEL = 0 ' disable A/D so RA4 is digital
    CMCON = 7 ' disable comparators
    OPTION_REG = %10110001 ' clk on RA4/T0CKI, falling edge, 1:4 prescale

    Revs var BYTE
    ORevs var word
    Counter var word

    '/////////////////////////
    '// LCD configuration //
    '/////////////////////////

    DEFINE LCD_DREG PORTB ' Set LCD Data port
    DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
    DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
    DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit
    DEFINE LCD_EREG PORTB ' Set LCD Enable port
    DEFINE LCD_EBIT 0 ' Set LCD Enable bit
    DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD
    DEFINE LCD_LINES 2 ' Set number of lines on LCD
    DEFINE LCD_COMMANDUS 2500
    DEFINE LCD_DATAUS 250
    DEFINE CHAR_PACING 2000
    pause 1000

    Counter =0

    Mainloop:
    TMR0 = 0 ' clear count before each pass

    ' output 8 clocks on RB0 to RA4/T0CKI
    'instead, input from sensor


    Revs = TMR0 ' get TMR0 count
    Counter = Revs + 1
    IF OPTION_REG.0 THEN
    LCDOUT $FE,1,"Counter1: ", DEC Counter
    ELSE
    lcdout $FE,$C0, "Counter2: ", DEC Counter
    ENDIF
    OPTION_REG.0 = OPTION_REG.0 ^ 1 ' toggle 1:4 or 1:2 prescale
    GOTO Mainloop

    end
    Last edited by lerameur; - 20th February 2009 at 22:25.

  3. #3
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi,

    also, I used your program andn it seemed to work but I realized that even if I remove the Loop: (or simply remove the connection between RA4 and RB3)
    FOR Loops = 0 TO 7
    PORTB.3=1 ' RB0 connects to RA4/T0CKI
    PAUSE 5
    PORTB.3=0 ' counter increments on falling edge
    PAUSE 5
    NEXT Loops

    the incrementation stays the same change, so I guess there si not effect on TMR0.

    K

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


    Did you find this post helpful? Yes | No

    Default

    Try this;
    Code:
    Mainloop:
    TMR0 = 0 ' clear count before each pass
    PAUSE 5000 ' give it some time to count pulses
    Revs = TMR0 ' get TMR0 count
    Counter = Counter + 1
    LCDOUT $FE,1,"Counter1: ", DEC Counter
    LCDOUT $FE,$C0, "Revs: ", DEC Revs
    GOTO Mainloop
    Counter of course is always going to change every 5 seconds or so, but Revs shouldn't until you have pulses in on RA4.

    Timer0 will automatically count pulses. You don't need to check any regs, bits, etc, just read it & clear it as required.
    Regards,

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

  5. #5
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I did output the Revs and it increment without any input:
    not as fast, takes about 2 seconds to get to ten.
    The odd thing is that it increment without changes to RA4.
    I am going to try your code you posted and be back later
    thanks for your time

    Mainloop:
    TMR0 = 0 ' clear count before each pass

    ' output 8 clocks on RB0 to RA4/T0CKI
    'instead, input from sensor


    Revs = TMR0 ' get TMR0 count
    Counter = Revs + 1
    IF OPTION_REG.0 THEN
    LCDOUT $FE,1,"Revs: ", DEC Revs
    ELSE
    lcdout $FE,$C0, "Counter2: ", DEC Counter
    ENDIF
    OPTION_REG.0 = OPTION_REG.0 ^ 1 ' toggle 1:4 or 1:2 prescale

    GOTO Mainloop

    end

  6. #6
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi again

    I set my function generator to 100Hz and the Revs outputs 127, when I set it to 1000Hz, I get Revs 225 on my LCD.
    I get a specific number to from different frequency input.

    Ken

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


    Did you find this post helpful? Yes | No

    Default

    That's a good sign. Higher frequency should = a higher count in a given time period.

    FYI: You really don't need to keep flipping OPTION_REG.0. That was just a part of my
    previous example to show the count difference with different prescaler values. I would
    leve it set at one prescale if you want valid counts.
    Regards,

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

Similar Threads

  1. Conway's Game Of Life
    By wellyboot in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 28th May 2020, 07:14
  2. Replies: 42
    Last Post: - 14th January 2008, 12:38
  3. 20 Digit Virtual LED Counter
    By T.Jackson in forum Code Examples
    Replies: 9
    Last Post: - 19th November 2007, 06:02
  4. Replies: 4
    Last Post: - 18th June 2007, 14:38
  5. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 18:27

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