Reading if Pin is High or Low?


Closed Thread
Results 1 to 30 of 30

Hybrid View

  1. #1
    Spindle's Avatar
    Spindle Guest

    Exclamation Reading if Pin is High or Low?

    Hey guys/gals.

    I am working on a project that involved an SPDT switch. I have the middle of the switch wired to a 5volt source and the other two wired to PORTB.5 and PORTB.7. What I want to do is check if PORTB.B or PORTB.7 goes high so then I can tell my PIC to drive my motor. PORTB.5 will be to drive my motor forward and PORTB.7 is to drive my motor reverse. Is there something similar to.

    if PORTB.5 = HIGH Then?

    Thanks in advanced.

    - Matthew

  2. #2
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    yes, it's
    IF portb.5 = 1 THEN 'check if portb.5 is high

    Cheers, Art.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    1. Be sure you place a pull down resistor on both PORTB pins
    2. Be sure your pin is define as input with the TRIS setting too
    my 2 cents... as always
    Code:
    TRISB.5=1
    
    IF PORTB.5 THEN ' do the same as IF PORTB.5=1 THEN
        ' do something here
        ENDIF
    OR, if your PIC have some A/D built in, you can use one channel to read your switch and do all the according stuff on 1 pin. You just need few resistors.
    something like this...
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=385&stc=1">

    once it's done you use a snip of code like this
    Code:
    ADCIN 0,ForwardReverse
    IF ForwardReverse<110 then
        Gosub MotorReverse
        ENDIF
    
    IF ForwardReverse>140 then
        Gosub MotorForward
        ENDIF
    110 and 140 are arbitrary value. Could work as is, but test it before. Set up a 8 bit A/D conversion.
    Attached Images Attached Images  
    Last edited by mister_e; - 31st July 2005 at 10:15.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Spindle's Avatar
    Spindle Guest


    Did you find this post helpful? Yes | No

    Default

    hrmm, something isn't right...

    [code]

    CMCON = 7 ' Disable analog comparator
    TRISA = %11110110 ' set PORTA to input
    TRISB = %11110000 ' set PORTB to output
    DEFINE HSER_BAUD 9600 'send serial data at 2400 baud

    UP VAR PORTB.5
    DOWN var PORTB.7
    FM VAR PORTB.2

    POSITION var word 'Position value
    VELOCITY var word 'Average velocity value
    FUNCTION var word 'Function register
    I var byte 'Loop variable

    TRISB.5 = 1
    TRISB.7 = 1

    MAIN:

    if UP THen
    serout FM, T9600 ,["W01 01 200", CR, LF] 'Write velocity limit value
    serout FM,T9600,["P01 270000", CR, LF] 'Send MOVETO_ABSOLUTE command

    for I = 1 to 100
    pause 25
    serout FM,T9600,["R01 00",CR,LF]
    serout FM,T9600,["R01 14",CR,LF]
    next
    endif

    if DOWN THEN
    serout FM, T9600 ,["W01 01 200", CR, LF] 'Write velocity limit value
    serout FM,T9600,["P01 0", CR, LF] 'Send MOVETO_ABSOLUTE command

    for I = 1 to 100
    pause 25
    serout FM,T9600,["R01 00",CR,LF]
    serout FM,T9600,["R01 14",CR,LF]
    next
    endif

    goto MAIN

    END

    [code/]

    I am trying to see which pin is high, RB7 or RB5 and then send the right command to the motor driving circuit. No movement however :\

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Code:
    CMCON = 7         ' Disable analog comparator
    TRISA = %11110110 ' set PORTA to input
    TRISB = %11110000 ' set PORTB to output
    
    UP   VAR PORTB.5
    DOWN var PORTB.7
    FM   VAR PORTB.2 
    
    POSITION var word    'Position value 
    VELOCITY var word    'Average velocity value 
    FUNCTION var word    'Function register 
    I        var byte    'Loop variable
    T9600    con 2 ' <== Baudrate definition in case you don't use
                   ' INCLUDE "modedefs.bas" line
    
    MAIN:
        if UP THen
           serout FM, T9600 ,["W01 01 200", CR, LF] 'Write velocity limit value 
           serout FM,T9600,["P01 270000", CR, LF]   'Send MOVETO_ABSOLUTE command
        
           for I = 1 to 100
               pause 25
               serout FM,T9600,["R01 00",CR,LF]
               serout FM,T9600,["R01 14",CR,LF]
               next 
           endif
        
        if DOWN THEN
           serout FM, T9600 ,["W01 01 200", CR, LF] 'Write velocity limit value 
           serout FM,T9600,["P01 0", CR, LF]        'Send MOVETO_ABSOLUTE command
        
           for I = 1 to 100
               pause 25
               serout FM,T9600,["R01 00",CR,LF]
               serout FM,T9600,["R01 14",CR,LF]
               next 
           endif
        
        goto MAIN
    The DEFINE HSER_BAUD 9600 'send serial data at 2400 baud have nothing to do with SEROUT, AND i don't see any crystal speed declaration. @9600 bauds with SEROUT, 8MHZ or higher is highely recommended.
    Last edited by mister_e; - 31st July 2005 at 09:30.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Spindle's Avatar
    Spindle Guest


    Did you find this post helpful? Yes | No

    Default

    I am using a PIC16F628 if that helps. It still isn't working for some reason. When i flip the switch nothing happend, but then the motor starts turning in like 30-40 secs. Its weird hehe. Its also 3:40am and getting late. I'll look at it again with fresh eyes in the morning but any more help is still apreciated.

Similar Threads

  1. SERIN MIDI out of Synch?
    By jncortes in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 9th June 2009, 20:08
  2. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  3. Pic16f84 and RC5 kode
    By terminator in forum Bluetooth
    Replies: 5
    Last Post: - 18th June 2007, 21:40
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. sample code for M25P32
    By Pedro Santos in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th January 2007, 02:37

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