figures as input
When you set bits in DDRx to 0, i.e. make port pins as inputs, then corresponding bits in PORTx register are used to activate/deactivate pull-up registers associated with that pin. In order to activate pull-up resister, set bit in PORTx to 1, and to deactivate (i.e to make port pin tri stated) set it to 0.
In input mode, when pull-up is enabled, default state of pin becomes ‘1’. So even if you don’t connect anything to pin and if you try to read it, it will read as 1. Now, when you externally drive that pin to zero(i.e. connect to ground / or pull-down), only then it will be read as 0.
However, if you configure pin as tri-state. Then pin goes into state of high impedance. We can say, it is now simply connected to input of some OpAmp inside the uC and no other circuit is driving it from uC. Thus pin has very high impedance. In this case, if pin is left floating (i.e. kept unconnected) then even small static charge present on surrounding objects can change logic state of pin. If you try to read corresponding bit in pin register, its state cannot be predicted. This may cause your program to go haywire, if it depends on input from that particular pin.
Thus while, taking inputs from pins / using micro-switches to take input, always enable pull-up resistors on input pins.
NOTE: while using on-chip ADC, ADC port pins must be configured as tri-stated input.
Example :
وقتی ورودی تعریف می کنیم ،
بحث پول آپ و پول داون است ،
یعنی همین جوری اون را رها نمی کنیم
و بعد ورودی تعریف کردن ، می آییم پول آپ یا پول داون می کنیم...
این به خاطر کنترل کار است تا با اندکی نویز ، اون پایه اشتباه نشون ندهد.
PHP:
to make port a as input with pull-ups enabled and read data from port a
DDRA = 0x00; //make port a as input
PORTA = 0xFF; //enable all pull-ups
y = PINA; //read data from port a pins
to make port b as tri stated input
DDRB = 0x00; //make port b as input
PORTB = 0x00; //disable pull-ups and make it tri state
to make lower nibble of port a as output, higher nibble as input with pull-ups enabled
DDRA = 0x0F; //lower nib> output, higher nib> input
PORTA = 0xF0; //lower nib> set output pins to 0,
//higher nib> enable pull-ups
در کدهای بالا وقتی از کلمه PORTx استفاده کردیم مثلا PORTA ,
داریم پول آپ و پول داون می کنیم ، چون قبلش اون پین ها را به عنوان ورودی انتخاب کردیم.