This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:avr:io [2010/02/01 14:52] – external edit 127.0.0.1 | en:avr:io [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Digital Inputs/ | ||
| + | All the buses on an AVR are both readable and writable, if they are used in the default logical input/ | ||
| + | |||
| + | * PORT - for setting the output value of the bus. | ||
| + | * PIN - for reading the input on the bus. | ||
| + | * DDR - for setting the direction of the bus. | ||
| + | |||
| + | <box 100% round # | ||
| + | |||
| + | Task: make pins 0-3 on bus B inputs, pins 4-7 outputs, set pin 5 high and read the values of pins 0-3 to a variable. The C code for it looks like this: | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + | unsigned char x; | ||
| + | |||
| + | // Pins 0-3 as inputs, 4-7 as outputs | ||
| + | DDRB = 0xF0; | ||
| + | |||
| + | // Set pin five as high | ||
| + | PORTB |= (1 << PIN5); | ||
| + | |||
| + | // Read the values from inputs 0-3 | ||
| + | x = PINB & 0x0F; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | </ | ||
| + | |||
| + | In this example, the inputs are used in Hi-Z (high impedance) mode. In essence, the input does not put virtually any load on the source of the signal. This mode might be necessary, if the pin is used as a data bus. It is wise to use a pull-up resistor on the input, if the pin is used for a button, a switch or any other solution, where the input is connected to ground. For that, the output bit of the corresponding pin must be set high in the input mode - as a result, a resistor is placed between the supply voltage and the input, which keeps the input voltage high unless it is being pulled down by something. The goal of a pull-up resistor is to prevent floating of the input due to static electricity and other interferences. After booting the controller, all IO buses are in the high impedance input mode by default. | ||
| + | |||
| + | Usually, the pins on the IO bus are also used for other peripherals, | ||