Newbie - 16F628A and switch latching


Closed Thread
Results 1 to 40 of 46

Hybrid View

  1. #1
    malc-c's Avatar
    malc-c Guest

    Default Newbie - 16F628A and switch latching

    Hi all,

    My first post so be gentile with me

    My first project using PBP is for my 12 year old son's model railway. I'm using a 16F628A with the internal 4Mhz OSC as timing is not an issue (and the internal OSC is claimed to be very precise anyway). The project is not rocket science, simply to detect a train via the momentarly activation of a reed switch. This pulse then turns on an LED which is latched on until the same reed switch is activated again.

    I have two problems:

    1) - The latching is not clean, in that sometimes the LED brightens and remains on when it should be off, or when off it is turned on by the reed switch, but fails to latch

    2) - I'm using WinPIC pro to program the PIC. I have set the PIC up for internal OSC no clock out in the code, but I have to change the settings from XT to RC noClockout in the Winpic application. Is the an issue with the Winpic application, or the code (which compiles OK)

    Anyway, here is my code.

    Code:
    @INTRC_OSC_NOCLKOUT
    @WDT_ON
    @PWRT_ON
    @MCLR_OFF
    @BOD_ON
    @LVP_OFF
    @CPD_OFF
    @PROTECT_OFF
    
    CMCON=7
    TRISB=%11101111         'set RB6 as output and the rest input
    
    SW var PORTB.0          'switch input pin 6 (RB0)
    LED1 var  PORTB.4       'LED1 on pin 10 (RB4)
    Swset var bit
    
    low led1                'RB4 set to LOW (LED1 is off)
    Swset=0                 'Var swset is set low
    
    Main:
    if SW=0 then            'If switch is LOW (Grounded) then
    Toggle Swset            'change swset from 0 to 1
    pauseus 200             'debounce delay
    endif
    
    If Swset=1 then         'if swset is 1
    high led1               'then turn on RB4 (and hence the LED)
    else
    low led1                'else, LED is off
    endif
    
    Goto Main               'go back and keep checking
    
    End
    The reason I've only configured one output on port B is purely for testing..once I have this sorted, RB0, RB1, RB2 and RB3 will all be switch inpts, with RB4 - RB7 their coresponding outputs (we have 4 sidings to monitor )

    TIA


    Malcolm

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


    Did you find this post helpful? Yes | No

    Default

    Your debounce delay is too short... swap PAUSEUS for a straight PAUSE. 200mS is neither here nor there in the great scheme of a train moving past your reed switch, whereas in 200uS which you have currently, the train would barely have moved a fraction of a millimetre... With some experimentation, you might find that PAUSE 50 will do...

  3. #3
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks for the reply,

    I had played with the debounce pause time, but only by a 100's of a uS

    I'll increase the delay and see how I get on.

    Many thanks

    Malcolm

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Hi Malcolm. One suggestion is to make all unused ports as outputs. This helps in noisy environments as electric motors drive PIC's crazy even with a separate supply. Make sure portb.0 is held high with a resistor not too high in value. I once had a small hobby motor drive my PIC crazy until I went down to a 1K resistor as a pullup.

    CMCON = 7
    TRISA = 0
    TRISB = 0 'MAKE ALL OUTPUTS
    PORTA = 0
    PORTB = 0 'ALL OUTPUTS LOW
    INPUT PORTB.0 'RB0 SWITCH INPUT

    MAIN:
    NAP 0 'REDUCE CURRENT DRAIN ON BATTERY
    IF PORTB.0 = 0 THEN CHANGELED 'IF REED SWITCH CLOSED CHANGE LED STATE
    GOTO MAIN

    CHANGELED:
    TOGGLE PORTB.4 'CHANGE LED STATE

    WAITFORRELEASE:
    PAUSE 1000 'WAIT A SECOND
    IF PORTB.0 = 0 THEN WAITFORRELEASE 'WAIT HERE FOR REED TO OPEN
    GOTO MAIN

  5. #5
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Peter,

    Thanks for the advice and I'll make port A all output as I'm only using port B. Oh and yes the inputs on the switches are tied high via a 10K resistor.

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


    Did you find this post helpful? Yes | No

    Talking You ..... again !!!

    Hi,Malc

    Have a look to the " BUTTON " command ...it is really very powerful for such application.

    Add a DEFINE BUTTON_PAUSE 200 at the top of your program if you want a debounce time of 200 ms ( max 255 ms ...) ... that's all !

    read you soon !!!

    Alain

    PS; if not already done, download this : http://www.melabs.com/downloads/pbpm304.pdf
    THE reference manual !!!
    Microcode studio : http://www.melabs.com/downloads/mcs2300.zip
    Last edited by Acetronics2; - 14th May 2006 at 13:57.
    ************************************************** ***********************
    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. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  2. Addressing with Dip Switch or ???
    By tazntex in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 16th September 2008, 12:19
  3. 16f628a wont wake up
    By Peter1960 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 25th August 2008, 14:22
  4. SLEEP mode 16F628A
    By Michael in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 25th April 2006, 17:26
  5. 16F628A using PORTA.4, and it works, but...
    By zx81 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th August 2005, 08:45

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