Newbie - 16F628A and switch latching


Closed Thread
Results 1 to 40 of 46

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Yeah, I think I might have a go at the BUTTON statement...

    Let me go away and RTFM first, have a play, try and get something together before bailing be out

    Before I start though, any comments on configuring PORTA (inc MCLR) for in/outputs ??

    Malc

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink Enlightments ???? Ok ...

    Here a cutout ...

    PORTA = 0 ' declare port level BEFORE port direction = safe power-on

    PORTB = 0

    'Pour 16F628

    CMCON = 7 ' PortA Digital inputs
    CCP1CON = 0 ' PWM off

    ' DATA @1,word 625,0,word 545,word 750 ' pre-setting EEPROM


    '************************************************* *****************************
    ' Initialisation commutation : Vérification batterie
    '************************************************* *****************************

    init:
    battok = 1 ' setting critical variables ...
    dejavu = 0
    bougie = 0
    error = 0
    Servo = 0
    TRISA = %00000011 ' Port Direction
    TRISB = %00000001

    Pause 400 ' Stabilisation au branchement

    GoSub verbatt ' beginning of real program ...batt testing i.e.


    Cheers

    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 " !!!
    *****************************************

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


    Did you find this post helpful? Yes | No

    Default Don't you just love the manual.

    Ok, trying to get my head round the BUTTON statement, and failing.. bu I just love the line

    In general, it is easier to simply read the state of the pin in an IF..THEN
    than to use the BUTTON command as follows:
    If PORTB.2 = 1 Then notpressed
    which was what I was originally doing .....

    Oh well ... I'll keep trying

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


    Did you find this post helpful? Yes | No

    Default It works !

    Ok after a lot of tries I finally have something that works. Here's the centre section of the code with the initialisation parts ommited for clarity

    Code:
    LED1 var  PORTB.4       'LED1 (green) on pin 10 (RB4)
    LED2 var  PORTB.5       'LED1 (red) on pin 11 (RB5)
    
    SW0 var PORTB.0          'switch input pin 6 (RB0)
    SW1 var PORTB.1          'switch input pin 7 (RB1)
    
    SWFLAG1 var Byte      ' counter for track 1
    
    Swset1 var bit
    Swset2 var bit
    
    low led1
    low led2 
                   
    Swset1=0 
    Swset2=0
                 
    SWFLAG1=0
    
    Main:
    
    if SWFLAG1 >2 then     ' if the counter is >2 then reset all
        swset1=0
        swset2=0
        SWFLAG1=0
    endif
    
    If SW0=0 Then
    swflag1 = swflag1 +1  ' increase count by 1
    swset1=1
    pause 500
    endif
    
    If SW1=0 Then 
    swflag1 = swflag1 +1  'increase count by 1
    swset2=1
    pause 500
    endif
    
    if swset1=1 then
    high led1                'then turn on RB4 (and hence the LED)
    else
    low led1                 'else, LED is off
    endif
    
    If Swset2=1 then       
    high led2                'then turn on RB5 (and hence the LED)
    else
    low led2                 'else, LED is off
    endif
    Basically rather than try and compare a condition of swset1 and swset2 and SW0, I simply used SWFLAG1 as a counter to count the number of times the switches are activated, and if it is greater than 2 reset the couter and swset 1 and 2 to 0.

    Now when sw0 goes low for the first time SWFLAG1 is = 1, when sw1 goes low SWFLAG1 =2 then when the loco is placed at the front of the train andis driven over SW0 for the second time SWFLAG1 =3. As the "IF SWFLAG1 > 2" condition is reached, SWFLAG1 is reset to 0, as is swset1 and swset2.

    Now just have to replicate this for the other 6 inputs and outputs

    I know this may seem trivial to the more experienced programmers, but its my first real program with PBP that is beyond the simple flash a LED, and I'm really pleased that I managed to figure something out, after some guidance from you guys here.

    Thanks

    Malcolm

    Oh, and seeing that I RTFM and did my homework... do I qualify for a dozen lashes form Mel (alias Ms Whippy )

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink BUTTON .... why ???

    Quote Originally Posted by malc-c
    Ok, trying to get my head round the BUTTON statement, and failing.. bu I just love the line



    which was what I was originally doing .....

    Oh well ... I'll keep trying
    Hi,Malc

    The IF THEN Statement implies your switch is an ideal switch and debouncing not tricky ...

    BUTTON is more complicated and slow ... but works in very difficult situations.

    The state of art is to verify the button has not changed its state BEFORE debouncing time has ended ...
    Multiple IF ... THEN ... ELSE following each others reach that GOAL ...

    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. 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