Assembly Language Question, I/O ports

Discussion in 'Programmer's Corner' started by ke5nnt, Oct 1, 2009.

  1. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    This instruction for 16F series:

    Code ( (Unknown Language)):
    1.  
    2. bsf     status,5
    3. movlw   B'01001100'
    4. movwf   trisb
    5.  
    Does this essentially mean:
    RB0 = output
    RB1 = input
    RB2 = output
    RB3 = output
    RB4 = input
    RB5 = input
    RB6 = output
    RB7 = output ???

    Or, does reading B'xxxxxxx' from left to right go bit 7,6,5,4,3,2,1,0?

    Another question: Say pin 4 is RA3/MCLR/Vpp and you want to use it as MCLR. Would you set that pin as input whenever you're specifying the literal value? What about pins that ultimately wont be connected to anything in the circuit, are those better set as inputs or outputs?


    I'm sorry to be bothersome with questions like these, but books and online tutorials only go into so much detail... even those specified for "beginners" make too many assumptions of things "you should already know".
     
    Last edited: Oct 1, 2009
  2. JDT

    Well-Known Member

    Feb 12, 2009
    658
    85
    Other way round. RB0 is the RH end (the Least Significant Bit).

    I/O pins that are not used should be set as outputs.

    An unused pin set as an input can "float" between the supply rails and can cause excessive current to flow in parts of the chip. Also, there is a risk of static damage with un-connected inputs. If set as an output, a transistor on the chip internally connects it to ground, shorting any static.
     
  3. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Thanks for the reply. Very helpful.

    I/O pins that are being used as a special function, like MCLR, should be specified as inputs or outputs? Does it matter, or will setting that bit's SFR override any B'xxxxxxxx' command?
     
  4. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Another question regarding the configuration word (bits).

    I'm seeing different ways of doing it and am wondering if they are all practical or correct. They are:

    1. Setting the configuration in the assembler during programming

    2. (double underscore) __config B'xxxxxxxxxxxxxx' (x = 0, or 1)

    3. _CONFIG_CP_OFF&_WDT_OFF&_PWRTE_ON&_XT_OSC


    My preference seems to be with choice 2, which seems the easiest. Are all 3 methods correct though?
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Setting the config bits during programming is not a good idea. You are likely to forget to do it and even if you do remember it leaves you no record of how the bits were set.
    Of the other two methods I prefer 3 since it tells you how the various features are set without you having to dig through the data sheet to decode what the various bits mean. You do however have the syntax wrong. The 3 option should start with the __config command so it should read
    Code ( (Unknown Language)):
    1. __config _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
     
    Last edited: Oct 4, 2009
  6. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Thank you for the response. Option 3 that I gave I was quoting directly from one of the online books referenced in AAC's links forum Useful websites for electronics (Ver. 2) to This Page under the heading Example of How to Write a Program. There seems to be some other mistakes I've noticed in this ebook as well, so I'm grateful for the clarification on that. Following the example on this ebook for specifying the include file, the author says:

    include "pic16f887inc", which returned an error when I tried it. I've since changed it to #include "p16f887.inc" which did not return an error.

    Anyways, point being, thanks for clearing that up.
     
  7. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
  8. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    Thanks Tahmid, always helpful you are.

    I need clarification on something else regarding I/O Ports.

    When specifying direction for port A when PORTA is only 5 bits wide, using binary, would I use:

    movlw B'10011' or...
    movlw B'00010011' ?

    The datasheet says those unimplemented bits are read as 0, so I assume I would specify all 8 bits, just leaving the 3 unimplemented bits as 0.
     
  9. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi,
    When specifying direction, since PORTA is only 5 bits wide, B'10011' is the same as B'00010011', as the bits 5,6 and 7 are unimplemented. By the way B'10011' reads the same as B'00010011' in either hex or decimal. Even if you write B'11110011' it will still be the same as the higher 3 bits are unimplemented and will make no difference if you write 1 or 0 or nothing at all.
    Thanks.
     
Loading...