Опубликовано 2010-03-21 17:40:28 автором Ruslan

How to connect to the microcontroller buttons

In previous lessons I spoke about, as with the MK output information: connections seven-segment display and Lcd display . In this lesson we will enter the information using the buttons. Buttons are several types: fixing the clock. From the name of the button understandable principle of its work: the clock is pressed, contacts enclosed, open - розімкнулися; fixing - record their condition: pressed - bent contacts, again pressed розімкнулися.

Standard wiring diagram buttons is very simple and looks like

wiring buttons to MK The idea of such on foot through the resistor 10K voltage is 5 V, on the leg of the logical unit, but when we press the button we have a leg on the ground and the current through the resistor will flow a little and he will be unable to 5 volts and the pin voltage gives way to 0 volts and is a logical 0. These points we'll catch in the program. Here is a program that will be when you press a button to enable led, at release off

#include <mega8.h>
#include <delay.h>
void main(void)
// initialize port D
PORTD is set=0b00000000;
while (1)
if (PIND & 0b00000100) /*check the logic level on the leg sign & that means bitwise "And"
for example in PIND is 0b00000100, then 0b00000100 & 0b00000100 = 0b00000100 that is true, and if PIND we
0b00000000 the 0b00000000 & 0b00000100 = 0b00000000 and this false */
PORTD is set=0b00000000; // write in zero the seventh bit of the port D
PORTD is set=0b10000000; // write the unit in the seventh bit of the port D
delay_ms(100); // make a delay of 100 milliseconds for protection against contact bounce

In most modern microcontrollers is a built in pull-up resistor R1, so external are not necessarily wiring buttons without pull-up resistor To enable the internal pullup resistor should be at the initialization of the port in the register PORTD is set set the corresponding bit in that the button is in the unit PORTD is set=0b00000100;
And what will happen if the output is configured as output:

  • If the ніжци logical zero nothing bad will happen
  • if the output will suddenly appear logical unit when you press the button, we simply закоротим foot on the earth, and the current flows through it, which foot does not survive (current through the leg must not exceed 40 mA), and most probably it will burn out
Therefore for protection is desirable to put a pin of the microcontroller and-300 Ohm resistor on limiting resistor between the button and microcontroller There are still a lot of ways how to connect button to microcontroller for example through the diodes or by the ADC but I will not describe as the course розщитан for beginners if you have to discover yourself.

Комментарии - (0)

Добавить комментарий

Для отправки комментария вы должны авторизоваться.