Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Topic 5 : UART Serial Port

LCD1
LM016L
1 U4 1 U3 1 U2 D5
LED0
R6
680
35.0 35.0 35.0 LED-YELLOW

2 ADC2 2 ADC1 2 ADC0 D6


VOUT VOUT VOUT R7

VDD
VSS

VEE
LED1

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E
680
LED-YELLOW

0%
3 3 3

1
2
3

4
5
6

7
8
9
10
11
12
13
14
LM35 LM35 LM35

RV1 D7
1k BT0 LED3
R8
680
LED-YELLOW

BT1 D8
LED4
R9
U1 680
9 22 LED-YELLOW
RESET PC0/SCL
23
PC1/SDA
12 24 BT2
U5 XTAL1 PC2/TCK
1 13 25
XTAL2 PC3/TMS
26
PC4/TDO
ADC0 40 27
PA0/ADC0 PC5/TDI
ADC1 39 28
35.0 PA1/ADC1 PC6/TOSC1
ADC2 38 29 BT3
PA2/ADC2 PC7/TOSC2
ADC3 37
PA3/ADC3
2 ADC3 ADC4 36 14
VOUT PA4/ADC4 PD0/RXD
ADC5 35 15
PA5/ADC5 PD1/TXD RXD
ADC6 34 16 INT0
PA6/ADC6 PD2/INT0
ADC7 33 17 INT1
PA7/ADC7 PD3/INT1 TXD
3 LM35 18 LED0 BT4
PD4/OC1B
BT0 1 19 LED1
PB0/T0/XCK PD5/OC1A RTS
BT1 2 20 LED2
PB1/T1 PD6/ICP1
INT2 3 21 LED3
PB2/AIN0/INT2 PD7/OC2 CTS
BT3 4
PB3/AIN1/OC0
BT4 5 BT5
PB4/SS
BT5 6 INT0
PB5/MOSI
BT6 7 32
PB6/MISO AREF
BT7 8 30
PB7/SCK AVCC
ATMEGA32 BT6
1 U6 INT1

35.0
BT7
1 U7 1 U8 1 U9 INT2
2 ADC4
VOUT

35.0 35.0 35.0


3 LM35
2 ADC5 2 ADC6 2 ADC7
VOUT VOUT VOUT

3 LM35 3 LM35 3 LM35

Problem 1 : An embedded systems using ATMEGA32 (working at 7.372800Mhz) communicate with Hyperterminal by UART0 serial interface.
The system has 4 LEDs connected to PORTD (PD4.PD5,PD5,PD7). When a system receiving new character from the UART serial port the
system will display this character to PORTC, and toggle the relevant LED( for example the character received is ‘0’ LED0 will be toggled, the
character received is ‘1’ LED1 will be toggled and so on … the character received is ‘3’ LED3 will be toggled ). The system using Interupt
method and UART setting with baud rate 19200, 8 bit, 1 stop bit, no parity. Write C program to control the system.

#ifndef F_CPU

#define F_CPU 7372800UL // 8 MHz clock speed

#endif

#define D4 eS_PORTC4

#define D5 eS_PORTC5

#define D6 eS_PORTC6

#define D7 eS_PORTC7

#define RS eS_PORTC0

#define EN eS_PORTC2

#include <avr/io.h>

#include <util/delay.h>

#include <avr/interrupt.h>

#include <string.h>

#include <stdlib.h>
#include "lcd.h"

#define LED0 4

#define LED1 5

#define LED2 6

#define LED3 7

char ADC_string[]="0000 ";

unsigned int data;

char ADC_channel_select=0x40;

char uart_char_receive;

void Tx( unsigned char data ){

while ( !(UCSRA & (1<<UDRE)) ); // wait until UDR is empty

UDR = data;

ISR (USART_RXC_vect){

uart_char_receive = UDR;

Tx(uart_char_receive);

switch(uart_char_receive)

case '0':

Lcd4_Set_Cursor(1,1);

Lcd4_Write_String("Receive 0:");

PORTD ^= (1<<LED0);// toggle led0

break;

case '1':

Lcd4_Set_Cursor(1,1);

Lcd4_Write_String("Receive 1:");

PORTD ^= (1<<LED1);// toggle led1

break;

case '2':

Lcd4_Set_Cursor(1,1);

Lcd4_Write_String("Receive 2:");
PORTD ^= (1<<LED2);// toggle led2

break;

case '3':

Lcd4_Set_Cursor(1,1);

Lcd4_Write_String("Receive 3:");

PORTD ^= (1<<LED3);// toggle led3

break;

default :

break;

void uart_init(){

DDRD = DDRD & 0xFE;

UCSRB = (1<<RXEN )|(1<<TXEN )|(1<<RXCIE); // Rx, Tx and Receive Interrupt Enable

UCSRC = (1<<UCSZ1)|(1<<UCSZ0); // 8-bit data

UBRRH = 0; UBRRL = 23; // For 7.372800 MHz Crystal and 38400 baud rate

int main (void){

// init lcd

DDRC = 0xFF; // make Port C an output

Lcd4_Init();

uart_init();

DDRD |= (1<<4) | (1<<5) | (1<<6) | (1<<7);

sei();

while (1){

}
return 0;

Problem 2 : An embedded systems using ATMEGA32 (working at 11.0592Mhz) communicate with Hyperterminal by UART0 serial interface.
The system has 4 LEDs connected to PORTB (PB0.PB1,PB2,PB3). When a system receiving new character from the UART serial port the
system will display this character to PORTD, and toggle the relevant LED ( for example the character received is ‘0’ LED0 will be toggled, the
character received is ‘1’ LED1 will be toggled and so on … the character received is ‘3’ LED3 will be toggled ). The system using Interupt
method and UART setting with baud rate 38400, 8 bit, 1 stop bit, no parity. Write C program to control the system.

#ifndef F_CPU

#define F_CPU 7372800UL // 8 MHz clock speed

#endif

#define D4 eS_PORTC4

#define D5 eS_PORTC5

#define D6 eS_PORTC6

#define D7 eS_PORTC7

#define RS eS_PORTC0

#define EN eS_PORTC2

#include <avr/io.h>

#include <util/delay.h>

#include <avr/interrupt.h>

#include <string.h>

#include <stdlib.h>

#include "lcd.h"

#define LED0 0

#define LED1 1

#define LED2 2

#define LED3 3

char ADC_string[]="0000 ";

unsigned int data;

char ADC_channel_select=0x40;

char uart_char_receive;
void Tx( unsigned char data ){

while ( !(UCSRA & (1<<UDRE)) ); // wait until UDR is empty

UDR = data;

ISR (USART_RXC_vect){

uart_char_receive = UDR;

Tx(uart_char_receive);

switch(uart_char_receive)

case '0':

Lcd4_Set_Cursor(1,1);

Lcd4_Write_String("Receive 0:");

PORTB ^= (1<<LED0);// toggle led0

break;

case '1':

Lcd4_Set_Cursor(1,1);

Lcd4_Write_String("Receive 1:");

PORTB ^= (1<<LED1);// toggle led1

break;

case '2':

Lcd4_Set_Cursor(1,1);

Lcd4_Write_String("Receive 2:");

PORTB ^= (1<<LED2);// toggle led2

break;

case '3':

Lcd4_Set_Cursor(1,1);

Lcd4_Write_String("Receive 3:");

PORTB ^= (1<<LED3);// toggle led3

break;

default :

break;

}
void uart_init(){

DDRD = DDRD & 0xFE;

UCSRB = (1<<RXEN )|(1<<TXEN )|(1<<RXCIE); // Rx, Tx and Receive Interrupt Enable

UCSRC = (1<<UCSZ1)|(1<<UCSZ0); // 8-bit data

UBRRH = 0; UBRRL = 17; // For 7.372800 MHz Crystal and 38400 baud rate

int main (void){

// init lcd

DDRC = 0xFF; // make Port C an output

Lcd4_Init();

uart_init();

DDRB |= (1<<0) | (1<<1) | (1<<2) | (1<<3);

sei();

while (1){

return 0;

You might also like