Output number when button is pressed

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:

#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#ifndef F_CPU
#define F_CPU 1000000
//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
//Enable Transmitter and Receiver
	DDRA=0x00;	//all A port pins as input
	PORTA=0xFF;	//Enable internal Pull-Up resistors
	DDRB=0xFF;	//All B port pins as output
			c=1; 	//start with first LED
			while(PINA==0xFF) //wait for button to be pressed
					if (c==0) c=1;	//if light passed all leds start over
					PORTB=~c;		//LED on
					_delay_ms(100);//short delay
					n++;			//increase number value
					c=c<<1;			//shift li9ghted LED
			//wait for empty transmit buffer
			while (!(UCSRA&(1<<UDRE))){};
			UDR=n;		//send number to USART
			PORTB=~n;				//and dislpaly number with led(binary)
			_delay_ms(200);	//long delay
			PORTB=0xFF;			//All LEDs OFF
return 0;

You can compile it with AVRStudio4. If you want to simulate the circuit by yourself, you can download Proteus design file . The only limitation is that Atmega16 model doesn't support USART (only UART), so no terminal modeling is present.


good proj

how could i get that code in assmbly??

after comping the code in avr gcc u will get .asm file also 


thanks for nice tutorial. I'm trying on Atmega1281 to send the char number via usart(same as u did), however receive false chars, not corresponding with the number sent. Usart is initialized,I wrote simple USARTWriteChar function:

void USARTWriteChar(char data){
//Wait until transmitter is ready
while (!(UCSR0A & (1<<UDRE0))){}

main function looks like this:
int main(void)
//Variable declaration
char n;

//Usart initialization with 9600 baud rate, UBRR=8MHz/(16*9600)-1=51

Thanks for comment.

can u attach the proteus pin connection  screen shot.......will it work if i just do copy nd paste in avr studio4 nd trying to simulate in proteus with the same