A question about PICBasic


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Dec 2009
    Posts
    7

    Default A question about PICBasic

    Hello everyone! This is my first post here and I am aksi quite new to PICBasic(I started less than a week ago).

    I have a few questions to ask but what I would like to ask first is why is this so:

    When you declare
    Code:
    TRISC = 0
    and then
    Code:
    PORTC = 1
    why does that make the pin0 of PORTC and output?

    because if you do "the same" (atleast for me-as a beginner it is the same)
    and declare

    Code:
    TRISC = %00000001
    that makes all of the other pins outputs and this one an input?

    so what is the difference? If my question is not clear enough please tell me and I will try to rephrase it.

    Thanks for everything, ciro!

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Setting TRISC=0 is the same as TRISC=%00000000.

    PORTC=1 has now set Bits 7 thru 1 of that Port to OUTPUT LOW, and Bit 0 has been set OUTPUT HIGH... this is the same as setting PORTC=%00000001.

    TRISC=%00000001 however as you correctly state, sets Bits 7 thru 1 as OUTPUT and Bit 0 as INPUT.

    TRIS sets I/O Direction (either for INPUT or OUTPUT), whereas PORTC=x will physically set PORTC to the value of x.

    It is always a good idea to initialise a PICs Ports with it's appropriate TRIS before performing any I/O. Don't rely on other commands to do it for you - they might not.

  3. #3
    Join Date
    Dec 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Thanks for the fast reply but I still don't think I quite understand it.

    So using PORTC = 1 sets the pin0 of PORTC to output physically but using TRISC = %11111110 doesnt?

    I mean why is TRIS used anyway if you have PORT to control the same things?

    And why if you use TRISC = 0(which makes all of the PORTC pins outputs) and then PORTC = 1 only makes that one pin an output if you have already previously stated that all of the PORTC pins are outputs with TRISC?(if you know what I mean)

    And why is 0 used as an output with TRISC and 1 with PORTC(I think)?


    Thanks again for your efforts! I really do appreciate it,
    ciro

  4. #4
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    To learn about I/O ports Chapter 3: I/O Ports

    In fact, download and read the entire book.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    TRIS controls whether the pin is an INPUT or an OUTPUT.

    PORT (when used as an OUTPUT) will control if the pin is LOW (0v) or HIGH (+5v).

    Each PIC pin can actually have THREE STATES... (consider TRIS=TRIState) it can be... (for example taking PORTB.0)...

    High Impedance (set for INPUT) when TRISB.0=1 (irrespective if PORTB.0 is set to 1 or 0)

    OUTPUT LOW (set at 0v) when TRISB.0=0:PORTB.0=0

    OUTPUT HIGH (set at +5v) when TRISB.0=0:PORTB.0=1

    So, in summary, TRIS will set the pin (or Port) for INPUT or OUTPUT, but PORT will set the pin HIGH or LOW (once it is an OUTPUT).

    Just to throw another variable into the equation, PORT can also be used for INPUT operations (when TRIS had previously set the pins/Port INPUT) thus...

    MyVar=PORTB.0 ' Read the state of just PORTB Bit 0 into variable MyVar

    MyVar=PORTB ' Read the state of ALL the pins of PORTB into variable MyVar

  6. #6
    Join Date
    Dec 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Default

    Great explanation! I get it now, thanks for this, really! I don't think anyone could outdo you haha

    Thanks Melanie, you're awesome!

Similar Threads

  1. Question about PICBASIC Pro.
    By tkdev2007 in forum General
    Replies: 5
    Last Post: - 10th April 2007, 21:31
  2. Clock Question - New to PicBasic
    By cometboy in forum mel PIC BASIC
    Replies: 3
    Last Post: - 3rd November 2006, 00:39
  3. Question for all that use MELABS PICBASIC PRO
    By oskuro in forum Off Topic
    Replies: 2
    Last Post: - 24th March 2005, 17:15
  4. Basic Question about PICBASIC
    By zonkwilliams in forum General
    Replies: 1
    Last Post: - 10th March 2005, 21:55
  5. PicBasic Question (PIC16F84)
    By Dan_lay in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 25th August 2004, 23:45

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts