Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

b. [10] What interrupt handler(s) do you have to write to implement the reply fu

ID: 3551834 • Letter: B

Question

b. [10] What interrupt handler(s) do you have to write to implement the reply functionality?

You will have to write two interrupt handlers, one for USART0_RXC_vect and another for USART1_RXC_vect which is the name of the interrupt vector for the ATMega128

Then, write the interrupt handler(s). You code should check for communication overflow: if USART0 receives a character but USART1 is not ready for transmitting, then the received character should be discarded. In other word, your code should check the UDRE flag in UCSR1A before it writes to UDR1. You need to find out the interrupt vector code, which is available in the ATmega128 datasheet.

You are asked to program an ATmega128 so that it plays as a one-way USART reply node from USART0 to USART1: Any character received by USART0 should be transmitted through USART1 to the outside. The program must be interrupt-based, so the microcontroller can do other work.

a. [10] Assume the ATmega128 runs at 16MHz. Write a USART_init() function to initialize both USART units as follows:

You may initialize unrelated control bits as you wish.

Explanation / Answer

/*****************************************************
Chip type : ATmega128
Program type : Application
Clock frequency : 8,000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 1024
*****************************************************/

#include <mega128.h>
#include <stdio.h>
#include <delay.h>

#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// Write a character to the USART1 Transmitter
#pragma used+
void putchar1(char c)
{
while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
UDR1=c;
}
#pragma used-

void putsf1(char flash *str)
{
int k;
while (k=*str++) putchar1(k);
}

void main(void)
{

// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: Off
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 9600
UCSR0A=0x00;
UCSR0B=0x08;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x33;

// USART1 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART1 Receiver: Off
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
// USART1 Baud Rate: 9600
UCSR1A=0x00;
UCSR1B=0x08;
UCSR1C=0x06;
UBRR1H=0x00;
UBRR1L=0x33;

while (1)
{
putsf("usart 0");
delay_ms(3000);
putsf1("usart 1");
delay_ms(3000);

};
}