Simple If.. Then help:


Closed Thread
Results 1 to 7 of 7
  1. #1
    Tear's Avatar
    Tear Guest

    Default Simple If.. Then help:

    In this very simple code I am just trying to play with the if statement. I have set port A pin 0 to be an input and have it hotwired to be high. However, the led connected to port B pin 0 WONT TURN ON. The led for port B pin 1 is just a heartbeat. I know that my circuit is wired correctly, and I can't see anything wrong in this code. Any help would be appreciated.



    TRISA.0 = 1
    loop1:
    if PORTA.0 = 1 then
    PORTB.0 = 1
    endif
    high PORTB.1
    goto loop1
    end


    EDIT: I went back and changed the line:

    PORTB.0 = 1

    to
    high PORTB.0

    After this change the led is always on even when I disconnect PORTA.0 from the 5 volt supply. It seems as if the IF statement is being ignored. Also, I dont have resistors connected to any of the PORTA or PORTB pins. I dont think this is the problem though.
    Last edited by Tear; - 17th June 2005 at 17:13. Reason: Additional information

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    hello Tear,

    Tear>>In this very simple code I am just trying to play with the if statement. I have set port A pin 0 to be an input and have it hotwired to be high. However, the led connected to port B pin 0 WONT TURN ON. The led for port B pin 1 is just a heartbeat. I know that my circuit is wired correctly, and I can't see anything wrong in this code. Any help would be appreciated.<<


    *IF* this is your WHOLE code ( I say IF), where are your pin Flags?
    I don't know what chip you are using, but many many chips have port A pins that have MULTIPLE functions... Thus the pin can do a A/D, it can do a Comparitor operation, and it can also do Digital I/O.

    you need to assign these pins the function you want them to do. (Data sheet is helpful here!).

    for example
    TRISA =%00000000
    TRISB =%00000000

    This assigns all the Port A pins as output pins. Your coding tells me this is what you want....

    But now, do your Port A pins have AD coverters and comparitors?? If so, you must turn them off!!

    CMCON to disable the comparators and
    ANSEL to disable your AD's

    CMCON=7 will probably do it. this turns off the comparators
    Ansel = 0 will assign all pins digital.


    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  3. #3
    Tear's Avatar
    Tear Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Dwayne,

    This is my full code:

    TRISA = %00000011
    TRISB = %00000000
    loop1:
    if PORTA.0 = 1 then
    PORTB.0 = 1
    endif
    high PORTB.1
    goto loop1
    end


    I am using the PIC16F84. What I am trying to do is just have PORTA.0 be an input and if it is high then it will turn the led at PORTB.0 on otherwise the led should be off. PORTB.1 is just the heartbeat.

    What is happening is that the led is on all the time no matter if PORTA.0 is connected to 5 volt or ground. It is like it is just missing the IF statement and executing the operation anyway. (Which I am guessing means that the microcontroller is somehow always seeing PORTA.0 as high?)

    Thanks for the help.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    What do you get with something like this?
    Code:
    TRISA = %00000011
    TRISB = %00000000
    loop1:
       if PORTA.0 = 1 then
          PORTB.0 = 1
       else
          PORTB.0 = 0
       endif
       high PORTB.1
       goto loop1
       end
    Your code as-is will never clear PORTB.0 to indicate PORTA.0 is at ground.

    You could even simplify this with PORTB.0 = PORTA.0 to reflect the logic
    input on RA0 to RB0.. or the inverse with PORTB.0 = ~PORTA.0.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Tear,

    Bruce beat me to the punch..<g> I was typing away, and my automessage came on.... Bruce typed what I was going to type.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  6. #6
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Tear,

    Using Bruces Code...

    TRISA = %00000011
    TRISB = %00000000
    loop1:
    if PORTA.0 = 1 then
    PORTB.0 = 1
    else
    PORTB.0 = 0
    endif
    goto loop1
    end


    I removed one one of his code (3rd line from the bottom)

    high PORTB.1


    I think without this line, you may have what you are looking for...

    If Porta is on, PortB is on.... If POrta is off.. POrt b is off...
    This also assumes Portb will STAY on until Port A is off. (then PortB will turn off).

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  7. #7
    Tear's Avatar
    Tear Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks for the help guys!


    It is one of the off days on my part

Similar Threads

  1. Simple RF remote control code
    By Bruce in forum Code Examples
    Replies: 13
    Last Post: - 22nd January 2014, 10:45
  2. Simple Blinking LED - WTF!!
    By johnnylynx in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st February 2010, 06:19
  3. Simple LCD code not working!...WHY?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 29th November 2009, 19:48
  4. Replies: 0
    Last Post: - 2nd February 2009, 23:23
  5. Replies: 4
    Last Post: - 7th September 2005, 14:11

Members who have read this thread : 1

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