Overview
A button is one of the old style user to controller interface. It still useful in many low end control application. However some modern digital control system implement a button in software via a computer software GUI or a touch panel.
Running program on experiment board |
Some tactile switches are examples of input button. |
Most of many on-board button interface, a tactile switch is usually found. They are varied in size and package type.
Interfacing A Button to ATMega16
Using a digital input pin of ATMega16, interfacing and programming for the user input using a button is effortless.
The program test the button pressed using a high or a low logic level. It test whether the input changes its logic state.
Programming example
A simple programming example, the ATMega16 microcontroller keeps track of the input change from high to low. The microcontroller increase a counting variable whenever this happens.
Schematic Diagram |
The 7-Segments display is just like the previous post. A tactile switch connects to PIND2. It creates a low logic level whenever it’s pressed. It’s normal high logic level doesn’t need an external resistor. The program turn PIND2 high in code.
In AVR GCC we test the present of a low logic level of the input by using the expression,
(PIND&0x04)==0
This expression return true if PIND2 equal to ‘0’ (low logic level).
An electro-mechanical switch creates noise which is called bouncing. It varies the input logic between high and low repeatedly for some milli-seconds. There exists many technique to eliminate this phenomenon. However, I use a timing delay about 250 mS to bypass it.
This embedded program example counts the input increment from 0 to 15 before it rolls back to 0.
The AVR GCC coding example lists below.
/* * btnSsdCa.c * * Created: 11/15/2020 8:40:33 PM * Author : Admin */ #include <avr/io.h> #define F_CPU 16000000UL #include <util/delay.h> int main(void) { //7-Segment data table unsigned char cAnode[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; //counting parameter unsigned char cnt=0; //Port C As Output DDRC=0xFF; //PIND2 As Input DDRD&=~(1<<2); //Set PIND2 High PORTD=(1<<2); while (1) { //Display the 7-Segments Data PORTC=cAnode[cnt]; //Increase the counter if ((PIND&0x04)==0) { _delay_ms(250); cnt++; } if(cnt>15) cnt=0; } }
An input tactile switch is already mounted on board, connects to PIND2 for a dedicated purpose. It excepts the single-digit 7-Segments display that need external circuit connection.
This example program runs on my development board. |
Click here to download this example program in zip file. See a video of this example on YouTube:
No comments:
Post a Comment