Counter not counting !!!


Closed Thread
Results 1 to 25 of 25

Hybrid View

  1. #1
    Join Date
    Sep 2006
    Posts
    747

    Default Counter not counting !!!

    Hello,

    I made a small disk counter with (wheel encoder with 2 part black and 2 part white) about 2 inches in diameter. I use a light to voltage sensor. It is working pretty well at low speed, but when I flick the disk, o Maybe a few turns per seconds the counter only counts one out of the 5 turns it made. My guess is that the clocking should be made higher.. any ideas?, The whole circuit is already soldered, I hope i do not need to get it in a 20Mhz crystal.
    here is the code:

    ' Internal Oscillation program
    ' A/D testing -RA.1
    ' LCD

    '/////////////////////////
    '// Define section //
    '/////////////////////////

    INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
    @ DEVICE pic16F88, INTRC_OSC, CCPMX_ON, MCLR_ON
    OSCCON=$60 ' use internal 4MHZ osc

    CMCON = 7 ' Turn OFF the comparators so pins can be used as digital I/O-pins


    '/////////////////////////
    '// 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


    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////

    TRISA = %11100111 ' Set PORTA to all input
    TRISB = %00001100 ' Set PORTB to all output
    PortA = 0

    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////
    Output_Pot var byte
    OldOutput_Pot var byte
    Revolution var word
    counter var byte

    revolution = 0
    Output_Pot = 0
    OldOutput_Pot = 0
    counter = 0

    OldOutput_Pot = PortB.3

    Mainloop:

    'Calculate the distance
    Output_Pot = PortB.3

    if OldOutput_Pot == Output_Pot then
    goto Mainloop
    endif
    if OldOutput_Pot != Output_Pot then
    counter = counter +1 'If oldOutput_pot is different then Output_pot
    OldOutput_Pot = Output_Pot
    if counter =4 then
    Revolution = Revolution + 1
    counter =0
    endif
    endif

    lcdout $FE,1, "Counter:",dec counter
    lcdout $FE,$C0, "Revolution:",dec Revolution
    pause 150


    GOTO Mainloop
    end

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    Time to consider using interrupts for this. Increment within the interrupt. Keep it small and fast!! I don't recall if the 88 has interrupts on portB.3

  3. #3
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    interrupt ... ok Guess I have to read up now.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    I would suspect the "light-to-voltage sensor". Most of these have response times in the millisecond range, maybe too slow.

  5. #5
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    I am not sure how interrupt can help in this case.
    At slow speed the counter works, but at higher speeds it does not pick half the time or more.
    I reduced the pause time to 20 after the LCDout, that did not help.

    K

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


    Did you find this post helpful? Yes | No

    Default

    Hi lerameur,
    I would use a photo transistor and not a photocell, a Crystal or resonator running at 20 mhz rather than intosc @ 4mhz. those things might speed things up a bit. You might use interrupts to capture counts and display on an as time permits basis instead of watching every count.
    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.

  7. #7
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi,

    I am using this little baby for a sensor
    http://www.taosinc.com/productdetail.aspx?product=63

    I think i will user TMR0 in my program

    K

Similar Threads

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