Main Menu
Home
AVR News
Atmel AVR
AVR Development Tools
Valuable Tools
WinAVR toolset
Makefile for WinAVR
AVR Virtual Simulators
Hardware for prototyping
GCC and AVR-GCC
Short introduction to C
AVR-GCC Tutorial
Example AVR Projects
AVR-GCC articles
WinAVR Site Map
Scienceprog FORUM
ScienceProg BLOG
Disclaimer
WinARM Tutorial
Login Form





Lost Password?
No account yet? Register
Latest comments
Programming AVR ADC module...
hi please send me example usart with...
More...

Measuring motor speed and...
main.c: In function...
More...

Running TX433 and RX433 RF...
(Hopefully this is not a re-post). I am...
More...

Running TX433 and RX433 RF...
I just bnought a pair of the RX/TX 433...
More...

Accessing AVR microcontroller...
You need to read PORTD 5th bit and...
More...

Syndicate
Friendly sites

Related Items:

Recommended sites


Latest News
Output number when button is pressed PDF Print E-mail
Written by Administrator   
Thursday, 13 September 2007

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.

Image

The program is very simple:


#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
	UCSRC=(1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|
	(0<<USBS)|(0<<UCSZ2)|(1<<UCSZ1)|(1<<UCSZ0);	
//Enable Transmitter and Receiver
	UCSRB=(1<<RXEN)|(1<<TXEN);
	DDRA=0x00;	//all A port pins as input
	PORTA=0xFF;	//Enable internal Pull-Up resistors
	DDRB=0xFF;	//All B port pins as output
	while(1)
		{
			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.


Related Items:

   

Users' Comments  
 

Average user rating

 


Add your comment
Only registered users can comment an article. Please login or register.

No comment posted



mXcomment 1.0.6 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved
< Prev   Next >

© 2008 WinAVR AVR-GCC Tutorial
Joomla! is Free Software released under the GNU/GPL License.