• AVR Freaks

Helpful ReplyHot!Setting a PIC 8 pin as output

Page: 12 > Showing page 1 of 2
Author
WeHongKongers
Super Member
  • Total Posts : 261
  • Reward points : 0
  • Status: offline
2020/10/16 03:26:52 (permalink)
0

Setting a PIC 8 pin as output

Hello,
If I want to set a PIC 8 pin as output, should I write:
TRISCbits.TRISC1 = 0;
or
LATCbits.LATC1 = 1;
or
both of the above at the same time?
 
My book says Latch is good for output but it has not mentioned whether we must set its TRIS also.
 
#1
ric
Super Member
  • Total Posts : 28713
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Setting a PIC 8 pin as output 2020/10/16 03:35:11 (permalink) ☄ Helpfulby WeHongKongers 2020/10/16 03:51:11
+1 (1)
Not sure what you mean by "PIC 8 pin".
Are you trying to say "8 bit PIC" ?
 
Anyway, it is the TRIS bit that switches a pin from INPUT to OUTPUT.
Only once the pin has been made an output can you set it high or low using the LAT register.
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 03:44:31 (permalink)
0 (2)
You should give more details 
 
Do you want to make pin as input or output ? 
For Example 
TRISA = 0b0000000; // Port A pin 7 connected to LED.. make pin 7 as output pin  
TRISB = 0b1000000; // Port B pin 7 connected to sensor .. make  pin 7 as input pin
post edited by Djsarkar - 2020/10/16 03:54:09
#3
WeHongKongers
Super Member
  • Total Posts : 261
  • Reward points : 0
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 03:56:06 (permalink)
0
I see.
 
Now, I set C1 as an output
TRISCbits.TRISC1 = 0;
 
If I set
LATCbits.LATC1 = 0;
will C1 still work as an output pin?  Or it only works as a weak output pin at lower voltage such as 3.3V DC?
 
#4
ric
Super Member
  • Total Posts : 28713
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Setting a PIC 8 pin as output 2020/10/16 04:11:58 (permalink)
0
HKPhysicist
Now, I set C1 as an output
TRISCbits.TRISC1 = 0;

Yes, that makes it an output, but does not say if it should output low, or high.
 

If I set
LATCbits.LATC1 = 0;
will C1 still work as an output pin?  Or it only works as a weak output pin at lower voltage such as 3.3V DC?

That sets the pin low, but only if the TRIS bit is telling it to be an output.
So, TRIS specifies the direction (input or output), LAT specifies the state (high or low, but only if it is an output).
 
Where are you getting these strange ideas about weak outputs, and lower voltages?
Note, we could be much more specific if you revealed which actual PIC device you are referring to.
 
 
post edited by ric - 2020/10/16 04:13:05

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#5
Mysil
Super Member
  • Total Posts : 3816
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 04:44:41 (permalink)
+4 (4)
Hi,
About question in message #1:

LATCbits.LATC1 = 1;    // Prepare to make High output
//  or
LATCbits.LATC1 = 0;    // Prepare to make Low output
// and
TRISCbits.TRISC1 = 0;  // Output drive On

These registers may be set at the same time, or at different time or different places in code.
If TRIS__ bit have already been set for Output, it may remain so with no further manipulation,
then LAT__ = 1;  and LAT__ = 0; will toggle the pin high and low.
 
If the Pin is connected to some sensitive equipment, like a communication line or motor drive,
then setting Latch register to the intended state Before enabling Output drive,
may help to avoid unwanted spikes in the signal.
 
 
There are some mistakes in examples shown in message #3,
code do not do what the comments say:
TRISA = 0b0000000; // Port A pin 7 connected to LED.. set pin 7 as output pin 

Code above actually set All pins of PortA to output, not just pin RA7.
 
Also here, comment is wrong:
TRISB = 0b1000000; // Port B pin 7 connected to sensor .. set pin 7 as input pin

Code above actually set pin RB6 to Input, and all other pins of RBx to Output. 
Bit numbers in registers are counted from RB0 in the rightmost location,
and binary constant in statement above have only 7 digits, but register have 8 bits.
 
Maybe this is what was intended when code was written, but incorrect comments make for errors that may be difficult  to identify. Even more when publishing code for others to use.
 
    Mysil
#6
Djsarkar
Senior Member
  • Total Posts : 175
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 05:50:29 (permalink)
0
Mysil
Maybe this is what was intended when code was written, but incorrect comments make for errors that may be difficult  to identify. Even more when publishing code for others to use.



Thanks Mysil for correcting me. writing good comment is always helpful 
#7
WeHongKongers
Super Member
  • Total Posts : 261
  • Reward points : 0
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 08:42:00 (permalink)
-1 (1)
I see.
 
High output for rough equipment;
and low output for sensitive equipment.  grin: grin
#8
1and0
Access is Denied
  • Total Posts : 11353
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 09:03:53 (permalink)
0
HKPhysicist
I see.
 
High output for rough equipment;
and low output for sensitive equipment.  grin:

What are you talking about?!
#9
vexorg
Senior Member
  • Total Posts : 176
  • Reward points : 0
  • Joined: 2019/09/27 10:59:40
  • Location: 0
  • Status: online
Re: Setting a PIC 8 pin as output 2020/10/16 09:48:48 (permalink)
0
Look at the schematic for the pic i/o pins.
 
TRIS sets the port direction, or tri-state in old logic terms.
1 means the port is not controlled by the pic, and the outside world sets it high or low (ignoring pull ups). - an input pin.
0 means the pic will drive the line high or low, an output pin.
 
LAT sets the output "latch" that drives that pin if it's an output, write 1 to LAT will set the pin high.
 
So, TRIS sets the direction of the pin
LAT sets the value if it's an output
PORT is used to read the pin if it's an input (also works for output set pins)
 
Not quite sure the need for LAT over PORT as the older PICs just used PORT for read and write.
#10
Mysil
Super Member
  • Total Posts : 3816
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 10:36:54 (permalink)
0
No such rule:
High output for rough equipment;
and low output for sensitive equipment.

What I tried to say in message #6, 
is that you need to know what equipment is connected to microcontroller pins,
and should think about what their properties are,
and what will happen when microcontroller have been Reset, and start operating.
 
Example: Asynchronous Serial communication (UART), have High logic level as Stop bit signal.
MAX232  type signal converters have internal weak pull-up on TTL/CMOS inputs,
to keep Stop level when there is no input signal.
What will be the effect if microcontroller drive TX output line Low, and then use 100 microseconds before UART peripheral is initialized and enabled.
 
    Mysil
#11
Mysil
Super Member
  • Total Posts : 3816
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 10:50:28 (permalink)
+1 (1)
Hi,
Not quite sure the need for LAT over PORT as the older PICs just used PORT for read and write.

Lot of bad experiences, search for RMW effect (Read-Modify-Write) in forum messages.
As microcontrollers get faster, 32 MHz,  64 MHz or higher system clock frequency, the problem just get worse.
 
    Mysil
 
#12
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 12019
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 11:05:02 (permalink)
0
Not quite sure the need for LAT over PORT as the older PICs just used PORT for read and write.

 
Googling for site:microchip.com "rmw" says there are 2,000+ posts on this forum caused by using PORT instead of LAT.  I doubt it's actually that high, but to those of us who've been around for a decade or more, it sure seems like it.
#13
upand_at_them
Super Member
  • Total Posts : 681
  • Reward points : 0
  • Joined: 2005/05/16 07:02:38
  • Location: Pennsylvania
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 11:07:37 (permalink)
+1 (1)
vexorgNot quite sure the need for LAT over PORT as the older PICs just used PORT for read and write.

 
Microchip implemented LAT *because* of the RMW problem.  They're not in the habit of adding unnecessary features to the silicon die.
#14
1and0
Access is Denied
  • Total Posts : 11353
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/16 11:36:39 (permalink)
0
vexorg
Not quite sure the need for LAT over PORT as the older PICs just used PORT for read and write.

As others have said, using LAT avoids the infamous RMW problem. You can write to PORT, provided you know your hardware, as RMW is a hardware dependent issue external to the PIC device. In my years of writing to PORTx on the older PICs, I have encountered the RMW problem only two times and the fixes are adding NOPs or rearranging the order of instructions. ;) Your mileage may vary.
 
 
post edited by 1and0 - 2020/10/16 11:57:14
#15
vexorg
Senior Member
  • Total Posts : 176
  • Reward points : 0
  • Joined: 2019/09/27 10:59:40
  • Location: 0
  • Status: online
Re: Setting a PIC 8 pin as output 2020/10/17 03:36:27 (permalink)
0
I had several years out and obviously things moved on from 16C / 17C in pure assembly, to new C code and 32 bit pics I use. I'd just adopted it because all examples used it.
 
Are older pics still the same or has the LAT method somehow been applied through the code? since you could only read or write to the SFR directly.
 
And if it was such an issue, why did they not just change the PORT write method to be the LAT method, is there any reason for keeping a PORT write now?
#16
rpg7
Super Member
  • Total Posts : 1416
  • Reward points : 0
  • Joined: 2003/11/07 12:47:35
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/17 06:39:37 (permalink)
0
vexorg
And if it was such an issue, why did they not just change the PORT write method to be the LAT method, is there any reason for keeping a PORT write now?


It's only an issue setting or clearing individual bits. Whole port writes work fine. Ironically, it is the READ of the READ/MODIFY/WRITE that is the problem. 
They probably kept writes to ports to support legacy code.
#17
1and0
Access is Denied
  • Total Posts : 11353
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/17 10:02:28 (permalink)
0
vexorg
Are older pics still the same or has the LAT method somehow been applied through the code? since you could only read or write to the SFR directly.

The older PICs are still the same. LATs are additional SFRs added in the newer PIC devices, not code.
 

And if it was such an issue, why did they not just change the PORT write method to be the LAT method, is there any reason for keeping a PORT write now?

Because an instruction cycle of all old and new 8-bit PIC devices consists of four Q cycles: decode, Read, process (Modify), and Write.
#18
vexorg
Senior Member
  • Total Posts : 176
  • Reward points : 0
  • Joined: 2019/09/27 10:59:40
  • Location: 0
  • Status: online
Re: Setting a PIC 8 pin as output 2020/10/18 04:56:31 (permalink)
0
1and0
Because an instruction cycle of all old and new 8-bit PIC devices consists of four Q cycles: decode, Read, process (Modify), and Write.



You're missing what I mean, why add LAT to new pics, and not just have PORT doing the LAT function on the new one, or is it more a case of keeping the instruction code value the same.
If writing to PORTs on new pics is bad, why keep it there
 
#19
Mysil
Super Member
  • Total Posts : 3816
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: offline
Re: Setting a PIC 8 pin as output 2020/10/18 06:20:33 (permalink)
+1 (1)
Hi,
Reason is Compatibility with existing code.
Writing to Port register address is Not bad, if All bits in register are written with determined values.
Problem may occur with instructions that read a register value, do something with some bits and store the result in the same register.
In PIC microcontroller, this include instruction to Set one bit, Clear one bit, Invert one bit, OR regster value, AND register value, XOR with register value, Add to Port register value ....
 
Problem with old PIC microcontrollers, was not with Writing to Port LATch registers,
All microcontrollers have Latch registers that hold values between instructions,
limitation was that there was no address decoder or logic to read Latch register value back into the CPU.
 
Alternative could be that pins set for Output would  Read from Output Latches, while Pins set for Tristate high impedance Input would Read from Input Buffers.
I think this would cause more confusion and mistakes.
 
    Mysil
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5