Friday, 25 October 2013

NotePad Design with Atmega16/32 on 16x2 LCD with UART


Here in this program we are designing the notepad by using Atmega16/32 as a controller and LCD for displaying the typed that we will be sending from the help of  UART. In Proteus, with the help of the Virtual Terminal , we will send the data to the uC and the same data will be displayed on the 16x2 LCD in the same way we type in the Notepad. Once you had written the max 32 character, Lcd will get clear and go back again to home position to provide you the space of the further character you will be sending from the UART.


Hardware Connection : -


***************************PROGRAM*****************************


//Program to interface the LCD, Uart and Keyboard

#include<avr/io.h>
#define F_CPU 1000000ul
#include<util/delay.h>
#include<stdlib.h>

//Flag to moniter no of bit received
char flag=0;


//Setting Fuse Bit

#define FUSE_CKSEL0 0
#define FUSE_CKSEL1 0
#define FUSE_CKSEL2 1
#define FUSE_CKSEL3 0

//**********************************LCD START************************************
/* LCD bit connection

Lcd Interfacing in 8-bit mode
PA0 --> D0
PA1 --> D1
PA2 --> D2
PA3 --> D3
PA4 --> D4
PA5 --> D5
PA6 --> D6
PA7 --> D7

PB0 --> RS
PB1 --> RW
PB2 --> EN

*/

//Macros For Lcd
#define LCD_PORT PORTA
#define rs (1<<0)
#define rw (1<<1)
#define en (1<<2)


//function Declaration
void init_lcd(void);
void cmd(unsigned char cmd_data);
void data(unsigned char l_data);
void enable(void);
void display(char *lcd_data);
void lcdgoto(unsigned char x,unsigned char y);
void display_value(long int x);
void lcd_clr(void);
//Function Definition for LCD
void init_lcd(void)
{
DDRA=0xFF;
DDRB=0xFF;
_delay_us(2);
cmd(0x38);
cmd(0x0E);
cmd(0x06);
cmd(0x01);
cmd(0x80);

}
void cmd(unsigned char cmd_data)
{
PORTB&=~rs;
PORTB&=~rw;
LCD_PORT=cmd_data;
enable();

}
void data(unsigned char x)
{
PORTB|=rs;
PORTB&=~rw;
LCD_PORT=x;
enable();

}
void enable(void)
{
PORTB|=en;
_delay_us(2000);
PORTB&=~en;
_delay_us(2000);
}
void display(char *lcd_data)
{
while(*lcd_data!='\0')
{
data(*lcd_data);
lcd_data++;
}
}

void lcdgoto(unsigned char x,unsigned char y)
{
if(x==1&&(y>=0&&y<=15))
{
cmd(0x7F+y);
}
if(x==2&&(y>=0&&y<=15))
{
cmd(0xBF+y);
}
}

void display_value(long int x)
{
unsigned char buf[8];
sprintf(buf,"%ld",x);
data(buf);
_delay_ms(2);

}
void lcd_clr(void)
{
cmd(0x01);
}
//*******************************************LCD ENDS********************

//******************************************UART START*******************

//Function Defination
void init_UART(int baud)
{
UCSRB|=(1<<RXEN)|(1<<TXEN);
UCSRC|=(1<<UCSZ1)|(1<<UCSZ0);
UBRRL=(F_CPU/(16*baud))-1;
}
void tx(char x)
{
while(!(UCSRA&(1<<UDRE)));
UDR=x;
}
char rx(void)
{
while(!(UCSRA&(1<<RXC)));
flag++;
return UDR;
}
void txfull(char *str)
{
while(*str!='\0')
{
tx(*str);
str++;
}
}

//*******************************************UART END***********************



// Main Function**************************************************************
int main()
{
init_lcd();
char f0=0,f1=0,f2=0;
init_UART(1200);
lcd_clr();
char ffla=0,fflb=1;
char buff;
while(1)
{
buff=rx();

if(flag>16&&ffla==0)
{
ffla=1;
fflb=0;
lcdgoto(2,1);
}
if(flag>32&&fflb==0)
{
ffla=0;
fflb=1;
lcdgoto(1,1);
lcd_clr();
flag=0;
}

data(buff);
tx(buff);
}
}

0 comments:

Post a Comment

Blogroll

Copyright © 2013 RAJ PRAJAPATI (raj@electronicsrj.com)

Comments

About Projects

We also Provide support in making Electronics Projects at different level at Cheapest price. Contact Us for the further help.