Beruflich Dokumente
Kultur Dokumente
http://www.bredboard.com/blog/?p=589
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;(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; (1 << RXC)) == 0); // return the byte return(UDR); }
embedded
GPS Receiver
micrcontroller
spundhan
1 of 3
19-Apr-13 1:02 AM
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.
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 is configured to enable communication in both Tx and Rx mode i.e duplex mode.
1.
1.
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.
1.
1.
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
http://www.bredboard.com/blog/?p=589
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