Sie sind auf Seite 1von 3

How to interface serial port with Atmega16 micrcontroller ?

http://www.bredboard.com/blog/?p=589

Learn from Experts

How to interface serial port with Atmega16 micrcontroller ?


In this tutorial we are interfacing serial port with Atmega16 microcontroller. Hardware required:

BredBoard
*

Atmega16 microcontroller development board. RS232 serial connector and MAX232 IC ISP programmer

Categories
Communication Embedded Systems Hardware Interfacing Microcontrollers Projects

Archives
September 2012 (2) August 2012 (1) July 2012 (11)

Blogroll
Ameet Patil BredBoard Shop Online Spundhan The above diagram shows the connection of serial port to microcontroller. Writing C code:The next step is to have code to run this project. Here we use C-language for programming and avr-gcc compiler to compile our program. And now create a simple serial.c with follwoing content. serial.c

Cloud

Atmega16 bredboard
buzzer computer

01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34.

#include <avr/io.h> //include the header file for avr microcontroller #define F_CPU 16000000L //crystal frequency #include <util/delay.h> /* Method to initialise serial port*/ void serial_init() { // set baud rate 9600 bps UBRRH = 0x00; UBRRL = 0xCF; //Enable communication in duplex mode UCSRA = (1 << U2X); //Enable serial transmission and recieve UCSRB = (1 << TXEN) | (1 << RXEN); //Set stop bit value as 1 and data frame size as 8 bit UCSRC = (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1); } /* Method to write data to serial port*/ void serial_write(unsigned char data) { //Wait untill data transmission and UDRE is set to empty while(!(UCSRA&amp;amp;amp;amp;(1<<UDRE))); //write the data into UDR UDR = data; } /* Method to read data from serial port*/ unsigned char serial_read(){ // Do nothing until data have been received and is ready to be read from UDR while ((UCSRA &amp;amp;amp;amp; (1 << RXC)) == 0); // return the byte return(UDR); }

embedded

GPS Receiver

GSM modem Home Automation interrupts Key lock Project

Keypad LCD LED

micrcontroller

Program Microcontroller relay serial Serial Communication

spundhan

1 of 3

19-Apr-13 1:02 AM

How to interface serial port with Atmega16 micrcontroller ? |

http://www.bredboard.com/blog/?p=589

35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48.

/* main Method*/ int main(){ char value; //method to initialise serial port serial_init(); while(1) { _delay_ms(10); //read data from serial and store it into value value = serial_read(); //write value onto the serial serial_write(value); } return 0; }

Here first we have to initialise the serial port. serial_init() method is used to initialise the serial port.

1. 2.

UBRRH = 0x00; UBRRL = 0xCF;

UBRR is USART BaudRate Register. There are two registers to set baudrate, i.e higher and lower. These two registers together stores baudrate value. UCSR is USART control and status register. It contains three parts like UCSRA,UCSRB and UCSRC. These three registers are mainly used to configure serial device.

1.

UCSRA = (1 << U2X);

UCSRA is configured to enable communication in both Tx and Rx mode i.e duplex mode.

1.

UCSRB = (1 << TXEN) | (1 << RXEN);

UCSRB is configured to make USART reciever and transmitter enable.

1.

UCSRC = (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);

USCRC is configured in such way that serial port must use 1 stop bit i.e (1 << USBS) and data frame of 8 bits i.e [(1 << UCSZ0) | (UCSZ1)]. serial_read() method is used to read data from serial. To recieve data from serial we have to enable recieve pin of UCSRA i.e (1 << RXC). And this method returns data into the UDR.Similarly serial_write() method is used to write data on serial. Now open terminal execute the follwoing commands.

1.

$ avr-gcc -mmcu=atmega16 -o serial.out -WL,-Map, serial.map serial.c -Wall

It compiles your serial.c file into serial.out file.

1.

$ avr-objcopy -R .eeprom -O ihex serial.out serial.hex

It generates serial.hex files

1.

$ avrdude -p m16 -c usbasp -P usb -U flash:w:serial.hex

It loads serial.hex file into microcontroller. Now open Cutecom on your computer, set baud rate values, stop bit, start bit and databits.And these values should match the values which are set in your program. Now click on open device and type data into input area. This data will appear on the output area. The following figure shows the working of serial with the Cutecom terminal.

2 of 3

19-Apr-13 1:02 AM

How to interface serial port with Atmega16 micrcontroller ? |

http://www.bredboard.com/blog/?p=589

Posted in Communication | Microcontrollers

Tagged: Atmega16 | bredboard | embedded | micrcontroller | serial | spundhan

About The Author


bredboard has blogged 14 posts

Leave a Reply
Your email address will not be published. Required fields are marked *

Previous Post
2012 Spundhan Softwares Pvt. Ltd. All rights reserved.

Next Post

3 of 3

19-Apr-13 1:02 AM