You Are Here: Home » AVR ATmega Projects » Output number when button is pressed using Atmega16 microcontroller

Output number when button is pressed using Atmega16 microcontroller




This is simple demo program of reading button state, lighting LEDs, sending information via USART. 8 buttons are connected to Atmega16 port A, 8 LEDs to port B via current limiting resistors. While none of buttons arent pressed there is running light on LEDs performed, but when any of buttons is pressed then LEDs display current 8 buit counter value in binary format. Same value is sent via USART – you can see number in terminal if connected.

The program is very simple:

 leds buttons




#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#ifndef F_CPU
#define F_CPU 1000000
#endif
//set desired baud rate
#define BAUDRATE 9600
//calculate UBRR value
#define UBRRVAL ((F_CPU/(BAUDRATE*16UL))-1)
char n, //number
 c; //current state of display
int main(void)
{
 //Set baud rate
 UBRRL=UBRRVAL; //low byte
 UBRRH=(UBRRVAL>>8); //high byte
//Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size

For more detail: Output number when button is pressed using Atmega16 microcontroller

Leave a Comment

You must be logged in to post a comment.

Read previous post:
How the iPod Shuffle Works
How the iPod Shuffle Works

Inside the iPod Shuffle Although an iPod with 240 or 500 songs might not sound so impressive from the start,...

Close
Scroll to top