📜 ⬆️ ⬇️

Device and operation of input / output ports of AVR microcontrollers. Part 4

Connect button to I / O port line

After reviewing this material, in which everything is described in great detail and in detail with many examples, you can easily master and program the input / output ports of AVR microcontrollers.


An example will be considered on an ATMega8 microcontroller.
')
We will write the program in Atmel Studio 6.0 .

We will emulate the circuit in Proteus 7 Professional .


The most common task when creating projects for microcontrollers is to connect buttons. Despite its simplicity, this task has significant, possibly non-obvious features.
If you connect one of the contacts of the button, for example, to the common wire ("ground"), and the second to the selected line of the I / O port of the microcontroller, which is switched to the "Input" mode, it turns out that this method does not work. When the button is pressed, the microcontroller port line is connected to the ground, and the program will read the “0” log from this line of the I / O port, but when the button is released, the microcontroller output will not be connected to anything that is often called “hanging in the air”. In this case, the program will count from the output and the log. "0" and log. "1" randomly, since no not connected to the line I / O port will be induced.
Correct connection assumes that in the open state the microcontroller's output must be connected through a resistor, for example, to the power bus, and in the closed state to the ground, or vice versa. The resistance of the resistor should not be too small so that the current flowing through it with the button contacts closed is not too large. Usually use values ​​of the order of 10-100 kOhm.



Figure: Connected button with a taut power bus.
With this connection, the line status of the input / output port will be:
- with the pressed button is equal to the log. "1";
- when the button is pressed, the log is “0”;



Figure: Connected button with tightened ground.
With this connection, the line status of the input / output port will be:
- with the pressed button is equal to the log. "0";
- when the button is pressed, the log is "1";

- connection to the line of the I / O port of a button with a tightened power bus:



//    #include <AVR/io.h> #include <stdint.h> //   int main(void) { //   / DDRB = 0b11111111; //    B   "" PORTB = 0b00000000; //    B  .«0» (     GND) DDRD = 0b00000000; //    D   "" PORTD = 0b11111111; //    D  .«1» (     Vcc) //   while (1) { //:   PD0 .«0»    if ((PIND&(1 << PD0)) == 0) { // PB0   .«1» PORTB |= (1 << PB0); } else { // PB0   .«0» PORTB &= ~(1 << PB0); } } } 


- connections to the line of the input / output port of the button with the tightened ground:



 //    #include <AVR/io.h> #include <stdint.h> //   int main(void) { //   / DDRB = 0b11111111; //    B   "" PORTB = 0b00000000; //    B  .«0» (     GND) DDRD = 0b00000000; //    D   "" PORTD = 0b11111111; //    D  .«1» (     Vcc) //   while (1) { //:   PD0 .«1»    if ((PIND&(1 << PD0)) == 1) { // PB0   .«1» PORTB |= (1 << PB0); } else { // PB0   .«0» PORTB &= ~(1 << PB0); } } } 

Source: https://habr.com/ru/post/256269/


All Articles