1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

writing bits to PORTA on p16f84

Discussion in 'Embedded' started by Guest, Feb 5, 2004.

  1. Guest

    Guest Guest

    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
    movlw 0x01 ;set PORTA RA0 pin to input
    movwf TRISA
    bcf STATUS,RP0 ;select bank 0

    movlw 0x10 ;set counter to 10
    movwf tmp
    bsf PORTA,0 ;simulate input on port RA0
    ;this is where I believe I'm
    ;having probelms

    main
    btfsc PORTA,0
    decfsz tmp,1 ;countdown from 20
    ;this never gets executed as PORTA bit 0 is
    ;always zero
    goto main
    end


    Here is the snippet of my session with the debugger. The important
    part is:

    gpsim> 0x0000000000000008 p16f84 0x0007 0x1405 bsf porta,0
    read: 0x00 from porta
    wrote: 0x00 to porta

    According to the debugger, PORTA never gets bit 0 set to 1. Am I perhaps
    missing something, or do I misunderstand how this works. If I want to
    configure a pin as an input I should turn on the appropriate bits in
    TRISA and set them to 1, no?

    I have read the faq and looked at other peoples code, and they seem to
    be doing the same thing I am. So what am I missing?

    The entire debugging sessiong step by step is included below in case
    any of it is relevant.

    Thanks!


    Found pic16f84
    f84 construct
    TMRO::start
    Cycle break point was ignored because cycle 0 has already gone by
    current cycle is 0
    Hex file "tmp.hex"
    ** SETTING CONFIG address = 0x2007 value = 0x3ffb
    --- Reset
    gpsim> 0x0000000000000001 p16f84 0x0000 0x2801 goto 0x0001
    gpsim> 0x0000000000000002 p16f84 0x0001 0x1683 bsf status,5
    read: 0x18 from status
    wrote: 0x38 to status
    gpsim> 0x0000000000000003 p16f84 0x0002 0x3001 movlw 0x01
    wrote: 0x01 to W
    gpsim> 0x0000000000000004 p16f84 0x0003 0x0085 movwf porta
    read: 0x01 from W
    wrote: 0x00 to porta
    wrote: 0x01 to trisa
    gpsim> 0x0000000000000005 p16f84 0x0004 0x1283 bcf status,5
    read: 0x38 from status
    wrote: 0x18 to status
    gpsim> 0x0000000000000006 p16f84 0x0005 0x3010 movlw 0x10
    wrote: 0x10 to W
    gpsim> 0x0000000000000007 p16f84 0x0006 0x008C movwf 0x0c
    read: 0x10 from W
    wrote: 0x10 to 0x0c
    gpsim> 0x0000000000000008 p16f84 0x0007 0x1405 bsf porta,0
    read: 0x00 from porta
    wrote: 0x00 to porta
    gpsim> 0x0000000000000009 p16f84 0x0008 0x1805 btfsc porta,0
    read: 0x00 from porta
    skipped: 000a goto 0x0008
    gpsim> 0x000000000000000B p16f84 0x000A 0x2808 goto 0x0008
    gpsim> 0x000000000000000D p16f84 0x0008 0x1805 btfsc porta,0
    read: 0x00 from porta
    skipped: 000a goto 0x0008
    gpsim> 0x000000000000000F p16f84 0x000A 0x2808 goto 0x0008
    gpsim> 0x0000000000000011 p16f84 0x0008 0x1805 btfsc porta,0
    read: 0x00 from porta
    skipped: 000a goto 0x0008
     
    Guest, Feb 5, 2004
    #1
    1. Advertisements

  2. Guest

    Gary Kato Guest

    I know this is true of some PICs, maybe all. It catches just about everyone.
    :)

    By default, the port A pins are set to be inputs to the analog to digital
    converters, not digital I/O. Looks at the documentation on the ADCON
    register(s) to see how to do that.
     
    Gary Kato, Feb 5, 2004
    #2
    1. Advertisements

  3. Guest

    Leon Heller Guest

    The 16F84 doesn't have an ADC.


    Leon
     
    Leon Heller, Feb 5, 2004
    #3
  4. Guest

    Gary Kato Guest

    The 16F84 doesn't have an ADC.
    Ah, good point.

    I'm wondering why RA0 is reading 0 instead of 1. It looks like if the pin is
    set as an input, it should always read as a 1. I'm wondering if the simulator
    defaults to assuming the pin is being pulled low by something.
     
    Gary Kato, Feb 5, 2004
    #4
  5. Guest

    Guillaume Guest

    When set as an input, a read on an I/O line on PICs always gives the
    actual (external) logic state on the pin, and not at all whatever you
    write in the output register.
     
    Guillaume, Feb 5, 2004
    #5
  6. Guest

    Gary Kato Guest

    When set as an input, a read on an I/O line on PICs always gives the
    If this were a real PIC and RA0 was not connected to anything, would it read 1
    when RA0 is set as an input?
     
    Gary Kato, Feb 5, 2004
    #6
  7. Guest

    Gary Kato Guest

    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.
     
    Gary Kato, Feb 5, 2004
    #7
  8. Guest

    goose Guest

    ^^^^^^^^^^^^^

    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.
    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
     
    goose, Feb 5, 2004
    #8
  9. Guest

    Gary Kato Guest

    Maybe I'm overthinking this now. What happens if you set RA0 as an output bit.
    When you change RA0, does reading RA0 reflect the change?

    This works in MPSIM.
     
    Gary Kato, Feb 5, 2004
    #9
  10. Guest

    Guillaume Guest

    If this were a real PIC and RA0 was not connected to anything, would it read 1
    It would read an undefined value. Might be 1 or 0, who knows?
     
    Guillaume, Feb 5, 2004
    #10
  11. Guest

    Al Borowski Guest

    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
     
    Al Borowski, Feb 5, 2004
    #11
  12. Guest

    foobear Guest


    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
     
    foobear, Feb 5, 2004
    #12
  13. Guest

    foobear Guest

    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
     
    foobear, Feb 5, 2004
    #13
  14. Guest

    Gary Kato Guest

    gpsim _will not_ let me turn
    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.
     
    Gary Kato, Feb 6, 2004
    #14
  15. Guest

    Gary Kato Guest

    If this were a real PIC and RA0 was not connected to anything, would it
    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, Feb 6, 2004
    #15
  16. Guest

    foobear Guest

    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
     
    foobear, Feb 6, 2004
    #16
  17. Guest

    Guillaume Guest

    From the diagram in the datasheet, if the bit is set as an input, the output
    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.
    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.
     
    Guillaume, Feb 6, 2004
    #17
  18. Guest

    Gary Kato Guest

    The upper transistor drawn in the diagram is a "P" type. It won't
    Ah. Thanks for clearing that up. I'm a hardware dunce.
     
    Gary Kato, Feb 6, 2004
    #18
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.