Sunday, August 15, 2021

ATMega16 Push Button And 7-Segment Display

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.



ATMega16 Push Button And 7-Segment Display
Running program on experiment board

ATMega16 Push Button And 7-Segment Display
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.

ATMega16 Push Button And 7-Segment Display
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.

  1. /*
  2.  * btnSsdCa.c
  3.  *
  4.  * Created: 11/15/2020 8:40:33 PM
  5.  * Author : Admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #define F_CPU 16000000UL
  11. #include <util/delay.h>
  12.  
  13. int main(void)
  14. {
  15. //7-Segment data table
  16. unsigned char cAnode[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
  17. 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
  18. //counting parameter
  19. unsigned char cnt=0;
  20. //Port C As Output
  21. DDRC=0xFF;
  22. //PIND2 As Input
  23. DDRD&=~(1<<2);
  24. //Set PIND2 High
  25. PORTD=(1<<2);
  26. while (1)
  27. {
  28. //Display the 7-Segments Data
  29. PORTC=cAnode[cnt];
  30. //Increase the counter
  31. if ((PIND&0x04)==0)
  32. {
  33. _delay_ms(250);
  34. cnt++;
  35. }
  36. if(cnt>15) cnt=0;
  37. }
  38. }

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.



ATMega16 Push Button And 7-Segment Display
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

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)