0% found this document useful (0 votes)
17 views18 pages

Lecture 8

The document outlines a program for an AVR microcontroller that facilitates serial communication with a computer via UART. It includes functionality to send a greeting, receive a byte of data, and control LEDs based on an index value received from the computer. The program also handles out-of-range inputs by sending an appropriate message back to the computer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

Lecture 8

The document outlines a program for an AVR microcontroller that facilitates serial communication with a computer via UART. It includes functionality to send a greeting, receive a byte of data, and control LEDs based on an index value received from the computer. The program also handles out-of-range inputs by sending an appropriate message back to the computer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EMBEDDED SYSTEMS

Serial Communication-3

Dr. Muhammad Awais


Department of Electrical & Computer Engineering, COMSATS University Islamabad Wah Campus
void printString(const char myString[])
{
uint8_t i = 0;
/* Srings are ended by a null-charachetr in C */
while (myString[i])
{
transmitByte(myString[i]);
i++;
}
}
Hello Computer!
This is AVR.

AVR Computer
Microcontroller
Hello Computer!
This is AVR

AVR Computer
Microcontroller
AVR Computer
Microcontroller

A
AVR Computer
Microcontroller

A
A
AVR Computer
Microcontroller
A
AVR Computer
Microcontroller
AVR Computer
Microcontroller

B
AVR Computer
Microcontroller

B
B
AVR Computer
Microcontroller
B
AVR Computer
Microcontroller
Develop an AVR based circuit that

1. Sends a greetings message to computer


2. Receives a byte of data from computer
3. Send the same received byte back to computer
4. Go to step 2
Initialize UART

Send Greetings to computer


while(1)
{
receive byte from computer
send same byte back to computer
}
int main(void)

// ------- Preamble -------- // {

char serialCharacter;
#include <binaryMacro.h>
// ----- Inits --------- //
#include <macros.h> initUSART();

#include <pinDefines.h> /* to test */

printString("Hello computer!\r\n");
#include <portpins.h>
printString("This is AVR!\r\n");
#include <USART.c> // ------ Event loop ------ //

while (1)

{
#include <avr/io.h>
serialCharacter = receiveByte();
#include <util/delay.h> transmitByte(serialCharacter);

return 0;

}
 Build an AVR circuit and program it
such that
 Through UART, it asks a laptop to send
an index value.
 Reads the index value
 Turns on index-th LED out of 6 LEDs
connected to the output pins (as shown
in the figure).
 If index is less than 2 or greater than 7,
AVR shall send an «out of range»
message to laptop
Initialize UART
Initialize output port
Send Greetings to Laptop
while(1)
{
Send prompt to laptop
Receive index value
If value is not in range
Send «Out of range» message
else
Turn on index-th LED
}
#include <binaryMacro.h>
#include <macros.h>
#include <pinDefines.h>
#include <portpins.h>
#include <USART.c>
// ------- Preamble -------- //
/* A program that turns on an LED at index value received from UART receiver. */
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
char serialCharacter;
// -------- Inits --------- //
LED_DDR = 0xff; /* set up LEDs for output */
DDRD = 0xff;
initUSART();
/* to test */
printString("Hello Laptop!\r\n");
printString("This is AVR.\r\n");
// ------ Event loop ------ //
while (1)
{
printString("Enter a number, from 2 to 7 and I will turn on corresponding LED\r\n");
serialCharacter = receiveByte();
transmitByte(serialCharacter);
printString("\n");
if ((serialCharacter < 0x32) || (serialCharacter > 0x37))
printString("The number you entered is out of range \r\n");
else
{
serialCharacter = serialCharacter-0x30;

LED_PORT= (1 << serialCharacter);

PORTD = LED_PORT;
}
}/* End event loop */
return 0;
}

You might also like