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

International Islamic University Islamabad

Microcontroller Based System Design

EXPERIMENT # 05: USART Interfacing and Programming


Name of Student: ..
Roll No.:
Date of Experiment: ..
Report submitted on: ..

Marks obtained:
Remarks:
Instructors Signature: ...

LABTASK:
Through serial port, transmit your name from the serial port to AVR and AVR should return
back first four digits of your registration number. Use crystal oscillator of 1MHz. Set the
baud rate to the nearest available range as per the following formula:
(Goupr No.) x (1700) = Baud Rate

PROGRAM FOR TRANSMISSION


#define F_CPU 1000000
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
//Macros Definition
#define BitSet(p,m)
#define BitClr(p, m)
#define Bit(x)

((p) |= (m))
((p) &= ~(m))
(0x01 << (x))

unsigned char z;
unsigned char name []="Zuha";
int main(void)
{
UBRRL = 0x24; // = 53 ~ 53.0833 = (1000000/(16*1200))-1
BitSet(UCSRB, Bit(5));
BitSet(UCSRB, Bit(3));
// OR UCSRB = 0x28;
BitClr(UCSRB, Bit(2));
BitSet(UCSRC, Bit(1));
BitSet(UCSRC, Bit(2));
BitSet(UCSRC, Bit(7));
// OR UCSRC = 0x86;
will be updated
sei();
while(1);
}
//ISR for Transmit interrupt
ISR (USART_UDRE_vect)
when transmission is complete
{
for(z=0; name[z]; z++)
{
UDR=name[z];
_delay_ms(5000);

//enable Data Register Empty Interrupt


//enable Transmission
//enables 8 bit character size
//enables 8 bit character size
//enables 8 bit character size
//writes on UCSRC register if this bit is 1
//when clear, UBRRH value
//stay here forever

//This interrupt will trigger

}
}
PROGRAM FOR RECEPTION
#define F_CPU 1000000UL
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
//Macros Definition
#define BitSet(p,m)
((p) |= (m))
#define BitClr(p, m)
((p) &= ~(m))
#define Bit(x)
(0x01 << (x))
void LCD_init();
void cmd_4b(unsigned char);
void LCDcmd(unsigned char);
void data_4b(unsigned char);
void LCDdata(unsigned char);
unsigned char x, i, check=0, c=0;
unsigned char RollNo[]="2145";
unsigned char myName[]="Zuha";
int main(void)
{
DDRB=0xFF;
DDRA = 0xFF;
UBRRL = 0x24;
(1000000/(16*1700))-1
BitSet(UCSRB, Bit(7));
BitSet(UCSRB, Bit(4));
BitClr(UCSRB, Bit(2));
BitSet(UCSRC, Bit(1));
BitSet(UCSRC, Bit(2));
BitSet(UCSRC, Bit(7));
LCD_init();
sei();
global interrupt
while(1);
forever
}

//PortA as output
// = 35.78 ~ 36 =
//enable Receive Complete Interrupt
//enable Receiving
//enables 8 bit character size
//enables 8 bit character size
//enables 8 bit character size
//writes on UCSRC register if this bit is 1

void LCDcmd(unsigned char cmdout)


{
PORTA = cmdout;
BitClr(PORTA, Bit(0));
//Clear RS
BitClr(PORTA, Bit(1));
//Clear RW
BitSet(PORTA, Bit(2));
//Set Enable

//enable
//stay here

_delay_us(200);
BitClr(PORTA, Bit(2));
}
void cmd_4b(unsigned char Cmd)
{
char Cmd1;
Cmd1 = Cmd & 0xF0;
LCDcmd(Cmd1);
Cmd = Cmd << 4;
LCDcmd(Cmd);
}
void LCD_init()
{
cmd_4b(0x02);
cmd_4b(0x08);
cmd_4b(0x0E);
}
void LCDdata(unsigned char dataout)
{
PORTA = dataout;
BitSet(PORTA, Bit(0));
//Set RS
BitClr(PORTA, Bit(1));
//Clear RW
BitSet(PORTA, Bit(2));
//Set Enable
_delay_us(200);
BitClr(PORTA, Bit(2));
}
void data_4b(unsigned char data_value)
{
char data_value1;
data_value1 = data_value & 0xF0;
LCDdata(data_value1);
data_value1 = data_value << 4;
LCDdata(data_value1);
}
//ISR for Receive interrupt
ISR (USART_RXC_vect)
{
if(UDR==myName[c])
{
check=check+1;
PORTB=check;

}
c++;
if(check==0b00000100)
{
cmd_4b(0x85);
while(RollNo[i]!='\0')
{
data_4b(RollNo[i]);
_delay_ms(50);
i++;
}
}
}

SIMULATION

You might also like