View Full Version : A question about PICBasic
ciroman
- 28th December 2009, 15:00
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
TRISC = 0
and then
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
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!
Melanie
- 28th December 2009, 17:18
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.
ciroman
- 28th December 2009, 18:29
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
rmteo
- 28th December 2009, 18:40
To learn about I/O ports Chapter 3: I/O Ports (http://www.mikroe.com/en/books/picmcubook/ch3/)
In fact, download and read the entire book.
Melanie
- 28th December 2009, 20:53
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
ciroman
- 30th December 2009, 01:01
Great explanation! I get it now, thanks for this, really! I don't think anyone could outdo you haha :D
Thanks Melanie, you're awesome!
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.