Friday, 25 October 2013

KEYPAD...UART (Universal Asynchronous Receiver Transmitter)...AND LCD (Liquid crystal display) Interfacing with Atmaga16/32


Following code will be defining the processing of the UART (checked in Proteus) with baud rate of 1200 bits/second with internal crystal of the ATmega16.


Here we are connecting the Keypad in the PORTC of the uC , LCD on the PORTB and the Virtual terminal in the Proteus to summarize the  exact working of the Uart.

 We had connected 4x3 LCD Matrix. 


//Program to interface the LCD, Uart and Keyboard


#include<avr/io.h>

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

//Setting Fuse Bit


#define FUSE_CKSEL0 0

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


CONNECTION IN PROTEUS :



//**********************************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)));
return UDR;
}
void txfull(char *str)
{
while(*str!='\0')
{
tx(*str);
str++;
}
}

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


//***************************************Keypad start **********************


//Macros

#define r0 (1<<0)
#define r1 (1<<1)
#define r2 (1<<2)
#define r3 (1<<3)

#define c0 (1<<4)

#define c1 (1<<5)
#define c2 (1<<6)


//Initialization of the Keypad

void init_keypad(void)
{
DDRC=0x0F;
PORTC=0xF0;
}
//For colm 1
void c0_func(void)
{
//CLEAR LCD 
//lcd_clr();
//row 1 colm 1
PORTC|=r0|r1|r2|r3;
PORTC&=~r0;
if(!(PINC&c0))
{
display("A");
txfull("A");
while(!(PINC&c0));
}


//Row 2 colm 1
PORTC|=r0;
PORTC&=~r1;
if(!(PINC&c0))
{
display("B");
txfull("B");
while(!(PINC&c0));
}


//Row 3 colm 1
PORTC|=r1;
PORTC&=~r2;
if(!(PINC&c0))
{
display("C");
txfull("C");
while(!(PINC&c0));
}


//Row 4 colm 1
PORTC|=r2;
PORTC&=~r3;
if(!(PINC&c0))
{
display("D");
txfull("D");
while(!(PINC&c0));
}
PORTC&=r0&r1&r2&r3;
}



//For colm 2

void c1_func(void)
{
//CLEAR LCD 
//lcd_clr();
//row 1 colm 2
PORTC|=r0|r1|r2|r3;
PORTC&=~r0;
if(!(PINC&c1))
{
display("E");
txfull("E");
}while(!(PINC&c1));

//Row 2 colm 2
PORTC=255;
PORTC&=~r1;
if(!(PINC&c1))
{
display("F");
txfull("F");
}while(!(PINC&c1));

//Row 3 colm 2
PORTC=255;
PORTC&=~r2;
if(!(PINC&c1))
{
display("G");
txfull("G");
}while(!(PINC&c1));

//Row 4 colm 2
PORTC=255;
PORTC&=~r3;
if(!(PINC&c1))
{
display("H");
txfull("H");
}while(!(PINC&c1));
PORTC&=r0&r1&r2&r3;
}




//For colm 3

void c2_func(void)
{
//CLEAR LCD 
//lcd_clr();
//row 1 colm 3
PORTC|=r0|r1|r2|r3;
PORTC&=~r0;
if(!(PINC&c2))
{
display("I");
txfull("I");
}while(!(PINC&c2));

//Row 2 colm 3
PORTC=255;
PORTC&=~r1;
if(!(PINC&c2))
{
display("J");
txfull("J");
}while(!(PINC&c2));

//Row 3 colm 3
PORTC=255;
PORTC&=~r2;
if(!(PINC&c2))
{
display("K");
txfull("K");
}while(!(PINC&c2));

//Row 4 colm 3
PORTC=255;
PORTC&=~r3;
if(!(PINC&c2))
{
display("L");
txfull("L");
}while(!(PINC&c2));
PORTC&=r0&r1&r2&r3;
}




// Main Function**************************************************************

int main()
{
init_lcd();
char f0=0,f1=0,f2=0;
init_UART(1200);
init_keypad();
lcd_clr();
while(1)
{

if((!(PINC&c0))&&(f0==0))
{
f0=1;
c0_func();

}
if((!(PINC&c1))&&(f1==0))
{
f1=1;
c1_func();
}
if((!(PINC&c2))&&(f2==0))
{
f2=1;
c2_func();
}



//to check the button is released

if((PINC&c0))
{
f0=0;
}
if((PINC&c1))
{
f1=0;
}
if((PINC&c2))
{
f2=0;
}

}
}

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.