hi all,

i've just a question...

loking my source code effect usin MPLAB ICD 2 debugger i see that

clrf PORTA
clrf PORTB
clrf PORTC

clrf TRISA
clrf TRISB
clrf TRISC


using this sequence seems that clrf PORTA, PORTB and PORTC don't have any effect (i put values to 0xFF with the debugger before the instruction is executed). So after clrf PORTA i don't have the port bits to 0. Same for PORTB and PORTC.

If i invert the sequnce in

clrf TRISA
clrf TRISB
clrf TRISC

clrf PORTA
clrf PORTB
clrf PORTC

now the port clearing works. Could someone explain the reason of this ? Should be correct my first sequence or my second one ? How are the ports pin and TRIS registers set at powerup ?


THX B7
Posted on 2003-11-27 12:06:39 by Bit7
easy...

Tris is used to define the Input or Output state of the port's bits. By default i would assume the ports are set to INPUT mode on startup. Clearing inputs has no action (after all, they are inputs). By doing CLRF on a tris register reassigns the port bits status to outputs (1 = input, 0=output). When you clear it after this it should work no problem, and Output a value 0 for every bit...

Regards,
:NaN:
Posted on 2003-11-27 21:48:33 by NaN
thx NAN,

you're right, it was easy, just as reading the datasheet :)
At power on trisa,b,c,d register are set at 11111111. This i think to protect pic i/o pin circuits.
So i can't understand, on datasheet they show sample code as

clrf PORTA
clrf TRISA

but it don't seems to have a sense,

it shuld br

clrf TRISA
clrf PORTA

to have portA pins to 0...
mmm what's right ?
Posted on 2003-11-28 12:58:45 by Bit7
clrf TRISA

Is a short cut. Normally you write to it with a value that is non-zero, such that some PortA pins are inputs, and other are outputs. This is dependant on what value you write to it (8 bit value). 00h = 00000000b , so CLRF does this by setting all to 0, or OUTPUTS.

If you wanted every other pin an input, starting at bit zero: 01010101b the value would be 55h.

movlw 0x55
movwf TRISA

(if my memory serves me correctly).

You should *always* TRIS before you clear or set the state of I/O, otherwise it wont know what your thinking each pin is to be (I or O).

Regards,
:NaN:
Posted on 2003-11-29 22:24:50 by NaN
Actually, if you write the PORT first, you will know what will appear at the pins when you set that port as an output.

If you look at the functional diagrams in the datasheet, you will see:
1) Writes are always stored in the output register
2) Reads always follow the pin
3) TRIS, the tri-state register, simply enables the output register via a tri-state buffer

Nan,

that code is correct. You could also use 0xAA, or whatever you heart contends.
Posted on 2003-12-03 02:15:47 by eet_1024
true, this is the reaso why the datasheet show samples a clrf POTA vefore clrf TRISA, the PORT registers are latched, you will see the output value at the moment of clrf TRISA.
Posted on 2003-12-03 11:45:49 by Bit7