[cpu]cpu unused pin应该怎样从硬件和软件上处理

Micro Community

1. 

This is a common question with lots of replies and lots of opinions.
Preferred option. Tie pins to ground or "high" through 5k to 20k resistors.
Option to avoid: Don't let unused pins float as inputs.
My option: Set unused pins as outputs with no connections.
Floating input can play havoc on chip operation. The resistor technique is most conservative. Setting to outputs works fine for non critical applications.

2.

They are various alternatives.
Software set the pin to output (except the MCLR, e.t.c.). 
Software set internal pull-up resistor if exists.
Tied an external resistor to Vcc or Vss

3. 

You're right. Like all CMOS devices, PIC input pins should not be left floating. 

I don't know that there is any general rule, though. (That's like asking "What's the best car?") 
Everybody seems to have a different opinion of what to do and why their particular way is "the best way". Some of the opinions include: 1) Set the unused pins as outputs and pull them either up or down through various size resistors. 2) Set the unused pins as inputs and pull them either up or down through various size resistors. Either way avoids floating inputs. In either case, I'd suggest not connecting the pins directly to Vdd or Vss.
By using pullup or pulldown resistors, you have future possibility of using the pins as either inputs or outputs without having to cut wires or PCB traces.
This also avoids the risk of a bug being able to flip an input pin to an output and then connect ground and Vdd by trying to drive the wrong state onto the pin.

StackExchange

16
down vote
There are many things that you can do, but the correct one depends on what you are trying to accomplish. I'll go over each of the common things:
Leave the pin unconnected, set as an input, and no pullup/pulldown. This is probably the worst thing you can do. A floating input could cause input noise, even if that input is not being used for anything. This could result in increased EMI and increased power consumption. There is no good reason to do this.
Leave the pin unconnected, set as an input, with internal or external pullups/pulldowns. This option is nice because the pin is available for future use (in case your design is not proven to work). Accidentally shorting the pin will likely not cause problems. This solution does nothing for EMI, however.
Leave the pin unconnected, set as an output, and driven high or low. This is nice because the pin is still available for future use, but accidental shorts could easily kill the thing. This is the worst case scenario for EMI, because each pin forms a tiny stub of an antenna.
Set the pin as a input, connect the pin to GND or VCC. The pin is not easily available for use if you have to rework the design. A good solution solution for EMI. One nice side benefit of this is that you can sometimes more easily route your power/gnd on the PCB since you can use the pads as a "route through"-- especally helpful on 2 layer PCB's with QFN or TQFP packages.
Set the pin as an OUTPUT, connect the pin to GND , drive the pin low as appropriate. Sometimes this is called a "virtual gnd", and improves the grounding of the chip. This solution is the absolute best for EMI, but does not tolerate mistakes of any kind. For example, if you enable the output before setting the output value low and the pin is driven high for a moment then you risk damaging the part. This is almost never done in MCU's, but is more frequently used in FPGAs where additional grounding helps to minimize simultaneous-switching-noise. I would not do this without consulting the chip manufacturer.
Without knowing specific issues that you might be having, solution #2 or #3 are the best balance of pro's and con's. Solutions #4 and #5 are reasonable solutions if EMI is a problem but your design is otherwise close to final (I.E., you are not likely to have a design change that will require more pins).

  另外分享一篇PIC单片机的官方文章

http://www.piclist.com/techref/logic/xtrapins.htm

  • Hardware:using resistors, to either ground ("pulldown") or the positive supply voltage ("pullup"). Anything from about 1K to about 10K will work fine.
  • Software: If they are programmable I/O pins, also leave them tri-stated.
原文地址:https://www.cnblogs.com/aaronLinux/p/9083480.html