PDA

View Full Version : Simple If.. Then help:



Tear
- 17th June 2005, 16:57
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.

Dwayne
- 17th June 2005, 17:36
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

Tear
- 17th June 2005, 18:39
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.

Bruce
- 17th June 2005, 19:30
What do you get with something like this?


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.

Dwayne
- 17th June 2005, 19:32
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

Dwayne
- 17th June 2005, 19:37
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

Tear
- 17th June 2005, 20:05
Thanks for the help guys!


It is one of the off days on my part :(