Reply by Guillaume February 6, 20042004-02-06
> 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.
Reply by February 6, 20042004-02-06
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
Reply by Gary Kato February 5, 20042004-02-05
>> 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?
Reply by Gary Kato February 5, 20042004-02-05
>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.
Reply by February 5, 20042004-02-05
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
Reply by February 5, 20042004-02-05
goose <ruse@webmail.co.za> wrote:
>> init >> bsf STATUS,RP0 ;select bank 1 >>[1] movlw 0x01 ;set PORTA RA0 pin to input >>[2] movwf TRISA > ^^^^^^^^^^^^^ > > here you set it to be an input. i.e. you can only intelligbly(sp?) > *read* from it, writing to it will not have any effect. > >> bcf STATUS,RP0 ;select bank 0 >> >> movlw 0x10 ;set counter to 10 >> movwf tmp >>[3] bsf PORTA,0 ;simulate input on port RA0 >> ;this is where I believe I'm >> ;having probelms > > there is no problem. you cannot turn an input on and > off (high and low) from *within* the chip, you need to > do it from outside. > > try this: > 1. bridge RA0 and RA1. > 2. replace lines [1] and [2] above with this: > bsf TRISA, 1 ; RA0 is now input > bcf TRISA, 0 ; RA1 is now output > 3. replace line [3] with > bsf PORTA, 1 ; this will turn on RA1, which will > ; make RA0 high, which you can then read
Hi goose, Thanks for the information. I thought that might have been the cause but I was uncertain, as I'm still somewhat new to PIC software development I don't have breadboard components at the moment, and the simulator I am using (gpsim) is still under development and its features are somewhat limited. As per your suggestion, is bridging RA0 and RA1 possible via some instructions on the PIC? Or were you thinking about doing this via a simulator? Thanks -v
Reply by Al Borowski February 5, 20042004-02-05
> main > btfsc PORTA,0 > decfsz tmp,1 ;countdown from 20 > ;this never gets executed as PORTA bit 0 is > ;always zero > goto main > end > >
Just for future reference, when 'goto main' is skipped, the microcontroller will *not* just stop. Instead it will keep executing the rest of (unwritten) program memory and roll back to the beginning of the program. At the and, you should put a stop goto stop end Al
Reply by Guillaume February 5, 20042004-02-05
> 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?
Reply by goose February 5, 20042004-02-05
<foobear@nospam.foobear.net> wrote in message news:<0wnUb.151$FF5.41618@petpeeve.ziplink.net>...
> Hello folks, > > I'm writing a simple program for the 16f84 pic to learn how it works. I > would like to simulate a switchbutton on pin RA0, that when pressed, > causes an internal counter to decrement. My problem seems to be when I > try to turn on the bits on PORTA RA0 to simulate the pin going high, it > never gets done. > > I am using the gpasm assembler version 0.10.0 and the gpsim simulator > version 0.21.1 > > Here is the program > > include p16f84.inc > list p=16f84 > radix hex > __fuses _CP_OFF & _WDT_OFF > > org 0x00 > goto init > > cblock 0x0c > tmp > endc > > init > bsf STATUS,RP0 ;select bank 1 >[1] movlw 0x01 ;set PORTA RA0 pin to input >[2] movwf TRISA
^^^^^^^^^^^^^ here you set it to be an input. i.e. you can only intelligbly(sp?) *read* from it, writing to it will not have any effect.
> bcf STATUS,RP0 ;select bank 0 > > movlw 0x10 ;set counter to 10 > movwf tmp >[3] bsf PORTA,0 ;simulate input on port RA0 > ;this is where I believe I'm > ;having probelms
there is no problem. you cannot turn an input on and off (high and low) from *within* the chip, you need to do it from outside. try this: 1. bridge RA0 and RA1. 2. replace lines [1] and [2] above with this: bsf TRISA, 1 ; RA0 is now input bcf TRISA, 0 ; RA1 is now output 3. replace line [3] with bsf PORTA, 1 ; this will turn on RA1, which will ; make RA0 high, which you can then read hth goose, hand
Reply by Gary Kato February 5, 20042004-02-05
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.