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,

    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

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


    Did you find this post helpful? Yes | No

    Default

    I was thinking . . . make a subroutine called display and do this:
    if OldOutput_Pot == Output_Pot then
    gosub display
    then do your lcd routines in there, it would display any time the counter stops incrementing.
    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.

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


    Did you find this post helpful? Yes | No

    Default

    Well, the reason for suggesting an interrupt is simple. When you poll, you spend a finite time in checking the state of the input. If you miss this(fast revolutions), you will miss counts. However, if you use interrupts, you can be sure to catch a significantly higher number of pulses than you could using the poll technique. example - you may be able to read upto 20pulses per second using the poll techniqe, but upto 200 pulses per second or even more using interrupts.

    Simple analogy to understand the difference between interrupts and polling.
    Think of yourself seated at the PC doing some work. The time you take to respond to a person at the door is your 'poll time' and depends on what you are doing at the moment and how soon it will finish. This would be similar to checking every now and then to see if there is someone at the door.

    On the contrary, if the person rings a doorbell (interrupts). You know that the door needs to be opened because of this signal. The way the processor handles it is to finish the current 'instruction' that it is executing and respond to the bell. This is much much faster than the earlier technique of checking the door now and then.

    Please don't mind the over-simplification. It might help someone else understand.

    I also second the idea that the opto interrupter may not be fast enough. But, from my experience, they work well into the KHz range.

    I suggest your Main loop should just display the counts and the interrupt routine should just count.

  4. #4
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    Hi,

    Ok I added an interrupt and wrote the program from what you said. Althouth I get zero on the LCD, seems that the interrupt is not working, can you help me debug it? Do I need to initialize some parameter . I am using the pic16f88
    Ken


    ' Internal Oscillation program
    ' LCD
    ' Main loop display the counts and the interrupt routine counts

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

    '///////////////////////////
    '// Interrupt section //
    '///////////////////////////
    OPTION_REG = %00111000
    'Transition on TOCKI (RA4),
    'Increment on falling edge
    'Prescalar assigned to WDT for a 1:1 TMR0 ratio.
    INTCON.2 = 0 ' Clear Timer0 int flag
    INTCON.5 = 1 ' Enable Timer0 int
    TMR0 = $FF ' Set TMR0 to 255. So one more tick and TMROIF will be set.

    '/////////////////////////
    '// 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 = %11110111 ' Set PORTA to all input
    ' Set PORTA.4 (TOCKI) to input for timer0
    TRISB = %00001111 ' Set PORTB to all output
    PortA = 0

    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////

    Revolution var word
    Counter var word
    oldCounter var word
    feet var word
    feet_left var word
    feet_right var word

    revolution = 0
    Counter = 0
    feet = 0
    PortB.2 = 1 'Put to 5v via a resistor and a pushbutton to ground for reset

    ON INTERRUPT GOTO LapCount

    Mainloop:
    if PortB.2 = 0 then
    goto Reset_variables
    endif

    lcdout $FE,1, "Cter:",dec Counter, " Rev:",dec Revolution
    lcdout $FE,$C0, "Feet:",dec feet/12, ".", dec2 feet//12 'This will save feet, and the // will print the decimal to two decimal point
    pause 20
    GOTO Mainloop

    Reset_variables:
    revolution = 0
    Counter = 0
    feet= 0
    lcdout $FE,1, "Counter:",dec Counter , " Rev:",dec Revolution
    lcdout $FE,$C0, "Feet:",dec feet/12, ".", dec2 feet//12
    pause 20

    GOTO Mainloop

    disable
    LapCount:
    If INTCON.2 = 1 then
    Counter = Counter + 1
    endif
    toggle PORTB.3
    TMR0 = $FF ' Indicate we're in interrupt handler
    INTCON.2 = 0 ' Clear TMR0 Interrupt Flag (Bit 2)

    'Calculate the distance

    if Counter =4 then
    Revolution = Revolution + 1
    Counter =0
    endif

    feet_left = Revolution *3
    feet_right = Revolution *25
    feet_right = feet_right / 100
    feet= feet_left +feet_right

    Resume
    Enable
    end

  5. #5
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    by the way I connected the sensor to portA.4
    ken

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by lerameur View Post
    Hi,

    Ok I added an interrupt and wrote the program from what you said. Althouth I get zero on the LCD, seems that the interrupt is not working, can you help me debug it? Do I need to initialize some parameter . I am using the pic16f88
    Ken


    ' Internal Oscillation program
    ' LCD
    ' Main loop display the counts and the interrupt routine counts

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

    '///////////////////////////
    '// Interrupt section //
    '///////////////////////////
    OPTION_REG = %00111000
    'Transition on TOCKI (RA4),
    'Increment on falling edge
    'Prescalar assigned to WDT for a 1:1 TMR0 ratio.
    INTCON.2 = 0 ' Clear Timer0 int flag
    INTCON.5 = 1 ' Enable Timer0 int
    TMR0 = $FF ' Set TMR0 to 255. So one more tick and TMROIF will be set.

    '/////////////////////////
    '// 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 = %11110111 ' Set PORTA to all input
    ' Set PORTA.4 (TOCKI) to input for timer0
    TRISB = %00001111 ' Set PORTB to all output
    PortA = 0

    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////

    Revolution var word
    Counter var word
    oldCounter var word
    feet var word
    feet_left var word
    feet_right var word

    revolution = 0
    Counter = 0
    feet = 0
    PortB.2 = 1 'Put to 5v via a resistor and a pushbutton to ground for reset

    ON INTERRUPT GOTO LapCount

    Mainloop:
    if PortB.2 = 0 then
    goto Reset_variables
    endif

    lcdout $FE,1, "Cter:",dec Counter, " Rev:",dec Revolution
    lcdout $FE,$C0, "Feet:",dec feet/12, ".", dec2 feet//12 'This will save feet, and the // will print the decimal to two decimal point
    pause 20
    GOTO Mainloop

    Reset_variables:
    revolution = 0
    Counter = 0
    feet= 0
    lcdout $FE,1, "Counter:",dec Counter , " Rev:",dec Revolution
    lcdout $FE,$C0, "Feet:",dec feet/12, ".", dec2 feet//12
    pause 20

    GOTO Mainloop

    disable
    LapCount:
    If INTCON.2 = 1 then
    Counter = Counter + 1
    endif
    toggle PORTB.3
    TMR0 = $FF ' Indicate we're in interrupt handler
    INTCON.2 = 0 ' Clear TMR0 Interrupt Flag (Bit 2)

    'Calculate the distance

    if Counter =4 then
    Revolution = Revolution + 1
    Counter =0
    endif

    feet_left = Revolution *3
    feet_right = Revolution *25
    feet_right = feet_right / 100
    feet= feet_left +feet_right

    Resume
    Enable
    end
    Code:
    TRISA = %11110111 ' Set PORTA to all input
    <font color=red> PortA.3 as output</font color>
    ' Set PORTA.4 (TOCKI) to input for timer0
    TRISB = %00001111 ' Set PORTB to all output
    <font color=red> PortB.0:3 as inputs</font color>
    PortA = 0
    For starters, check this to see if it is what you really want.
    PortA.4 is Schmitt Trigger input so a pulldown resistor as shown in the opto's datasheet will be a necessity, it says it outputs min 3.0v so it should swing the S T input OK. The 16F88 has a ton of analog you need to attend to if you want to use it, I am not seeing any code to do that.
    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,

    the hardware seems good, like I said it works at slow speed. In my case I have to use port RA4 right? to get the timer0 from my program.
    I looked through the datasheet and nowhere there is mention on how to disable the schmitt trigger.

    ken

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Lightbulb

    Hi, Ken

    IF you want your Thing to work properly ...

    You HAVE to use QUICK Interrupts and not " On Interrupt" ... especially if using a LCD.

    So, Have a look to asm interrupts or Darrel Instant Interrupts ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

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