Lab Four - Lab Four Wallace Part One AVR Serial...

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

View Full Document Right Arrow Icon
Lab Four - AVR Serial Communications Wallace Kerrigan, 1541317 [email protected] 17 October 2011 Part One
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
************************************************************* 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.
Image of page 3

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

View Full Document Right Arrow Icon
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();
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern