Lab Four

Lab Four - Lab Four - AVR Serial Communications Wallace...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lab Four - AVR Serial Communications Wallace Kerrigan, 1541317 [email protected] 17 October 2011 Part One Figure 1. Flow Chart for Transfer of Data from PC to Hyper-Terminal using Polling Method Code /* A simple program to demonstrate the use of USART of AVR micro ************************************************************* Hardware: ATmega644 @ 8MHz Suitable level converter on RX/TX line Connected to PC via RS232 PC Software: Hyper terminal @ 4800 baud No Parity, 1 Stop Bit, 8 data bits Flow Control = NONE */ #define F_CPU 1000000UL #include <avr/io.h> #include <avr/interrupt.h> char slendy; char USARTReadChar(void); void USARTWriteChar(char slendy); void wait(void) { asm volatile ("nop"::); } void USARTInit() // This function is used to initialize the USART at a given UBRR value { // Set Baud rate UBRR0L = 0x0C; // Binary conversion: 0b00001100 UBRR0H = 0x00; // Binary conversion: 0b00000000 /* Set Frame Format >> Asynchronous mode, No Parity, 1 StopBit, char size 8 */ UCSR0C = 0b00000110; //Enable the receiver and transmitter UCSR0B = (1<<RXEN0)|(1<<TXEN0); } //This function is used to read the available data //from USART. This function will wait (poll) until data is //available. char USARTReadChar() { //Wait until a slendy is available while(!(UCSR0A & (1<<RXC0))) { //Do nothing } //Now USART has got slendy from host //and is available is buffer return UDR0; } //This function writes the given "slendy " to //the USART which then transmit it via TX line void USARTWriteChar(char slendy) { //Wait until the transmitter is ready while(!(UCSR0A & (1<<UDRE0))) { //Do nothing } //Now write the slendy to USART buffer UDR0= slendy; } int main(void) // Main function { USARTInit(12); for(;;) { char slendy = USARTReadChar(); // Define USARTReadChar() as "slendy " for(int i = 0;i < 100; i++) wait(); USARTWriteChar('['); for(int i = 0;i < 100; i++) wait(); USARTWriteChar(slendy); for(int i = 0;i < 100; i++) wait(); USARTWriteChar(']'); for(int i = 0;i < 100; i++) wait(); } } Explanation In order to program the microcontroller to interact with Hyper-Terminal, many commands from the Serial Interface Bus were implemented. To begin, several characters were defined as slendy and USARTReadChar. The function USARTInit() was used to initialize the USART at a given UBRR value. Next, the baud rate was set based on the requested rate which was given. The baud rate registers UBRR0L and UBRR0H were used for this. The command UCSR0C was used next in order to set the frame format to asynchronous mode, no parity, one stop bit, and character size of eight. As well, UCSR0B was used to enable the receiver and transmitter. A character function was implemented next in order to read the available date from USART. It would then wait, or poll, until the data was available. To do this, a while function was implemented. Once USART had its data from the host, it would be available to buffer. This was when the return UDR0 command came in. The next function wrote this same data, which was when the return UDR0 command came in....
View Full Document

This note was uploaded on 11/21/2011 for the course ECE 3411 taught by Professor Park during the Fall '11 term at UConn.

Page1 / 12

Lab Four - Lab Four - AVR Serial Communications Wallace...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online