EmbeddedRelated.com
Forums

writing bits to PORTA on p16f84

Started by Unknown February 5, 2004
Gary Kato <garykato@aol.com> wrote:
> I don't use gpsim, but the standard MPLAB SIM. MPLAB SIM seems to assume that > input pins are tied low unless told otherwise by the Pin Stimulator. I tried > setting RA0 as an output and setting the latch, then setting RA0 as an input > and RA0 turned off at that point. > > I don't know if gpsim has some way for you to force the value of RA0 to what > you want when it's configured as an input. MPLAB SIM refuses to do that, but > then it has the Pin Stimulator.
Hello Gary, gpsim allows me to turn bits on and off on various registers, when I set certain bits on TRISA to 1 (output), gpsim _will not_ let me turn the appropriate bit on in PORTA register (I can turn on the other bits though, when the appropriate TRISA bits are set to 0) Is this what is happening to you in MPLAB SIM? -v
>gpsim _will not_ let me turn >the appropriate bit on in PORTA register (I can turn on the other bits >though, when the appropriate TRISA bits are set to 0)
Right. If TRISA,0 is set to 1 then MPSIM won't let me set PORTA,0. If TRISA,0 is 0, then MPSIM allows me to set the value of PORTA,0. So, why not just clear TRISA,0 so you can set the value or PORTA,0? You should still be able to read PORTA,0. You just have to remember to change your code once you get real hardware.
>> If this were a real PIC and RA0 was not connected to anything, would it >read 1 >> when RA0 is set as an input? > >It would read an undefined value. Might be 1 or 0, who knows? >
From the diagram in the datasheet, if the bit is set as an input, the output from the latch would be 1. If nothing is driving the input pin high or low, I would think that is the value that would be read. What would be a reason for the value to be 0? Doesn't something need to pull the signal low to make it read 0?
Gary Kato <garykato@aol.com> wrote:
>>gpsim _will not_ let me turn >>the appropriate bit on in PORTA register (I can turn on the other bits >>though, when the appropriate TRISA bits are set to 0) > > Right. If TRISA,0 is set to 1 then MPSIM won't let me set PORTA,0. If TRISA,0 > is 0, then MPSIM allows me to set the value of PORTA,0. > > So, why not just clear TRISA,0 so you can set the value or PORTA,0? You should > still be able to read PORTA,0. You just have to remember to change your code > once you get real hardware.
Hi Gary, This is precisely what I have done for now. As well as put in comments to remind myself later to set the bits. I suppose I wasn't so confuzzled after all... Now if I can get some kind of stimulus system working to simulate voltage on the pins I'll be gtg :) Thanks -v
> From the diagram in the datasheet, if the bit is set as an input, the output > from the latch would be 1.
The TRIS latch, yes. But it would put the ouput driver (the P/N pair) in Hi-Z, exactly as is stated in the data sheet: "Setting a TRISA bit (=1) will make the corresponding PORTA pin an input, i.e., put the corresponding output driver in a hi-impedance mode." The upper transistor drawn in the diagram is a "P" type. It won't act as a weak pull-up if the TRIS bit is 1, as you seem to mean. Port B pins have weak pull-ups, see PORTB description to see the difference.
> If nothing is driving the input pin high or low, I > would think that is the value that would be read. What would be a reason for > the value to be 0? Doesn't something need to pull the signal low to make it > read 0?
No reason for it to be 1 either. See above. In input mode, the pin is simply left "floating". A floating pin is a no-no in the "real world". It could read anything, and possibly even oscillate in some cases, with the side effect of consuming more current. That's why it's important that you connect unused inputs either to ground or Vcc, depending on the circuit. In a simulator, they obviously have to choose a fixed state in this case, since they probably won't simulate a real circuit with all its complexity. That's why you have to use those simulators with a bit of caution.