0% found this document useful (0 votes)
83 views2 pages

LCD - Command: Void Unsigned Char

This document contains code for interfacing an LCD 16x2 display with an AVR ATmega16 microcontroller. It defines pin connections and macros for the data and command ports. Functions are included to initialize the LCD, write commands and characters, display strings on one or both lines, and clear the display. The main function calls LCD_Init() followed by LCD_String() twice to display example text on both lines of the LCD.

Uploaded by

saleh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views2 pages

LCD - Command: Void Unsigned Char

This document contains code for interfacing an LCD 16x2 display with an AVR ATmega16 microcontroller. It defines pin connections and macros for the data and command ports. Functions are included to initialize the LCD, write commands and characters, display strings on one or both lines, and clear the display. The main function calls LCD_Init() followed by LCD_String() twice to display example text on both lines of the LCD.

Uploaded by

saleh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

LCD16x2 8 bit AVR ATmega16 interface


http://www.electronicwings.com
*/

#define F_CPU 8000000UL /* Define CPU Frequency e.g. here 8MHz */


#include <avr/io.h> /* Include AVR std. library file */
#include <util/delay.h> /* Include inbuilt defined Delay header file */

#define LCD_Data_Dir DDRB /* Define LCD data port direction */


#define LCD_Command_Dir DDRC /* Define LCD command port direction register */
#define LCD_Data_Port PORTB /* Define LCD data port */
#define LCD_Command_Port PORTC /* Define LCD data port */
#define RS PC0 /* Define Register Select (data/command reg.)pin */
#define RW PC1 /* Define Read/Write signal pin */
#define EN PC2 /* Define Enable signal pin */

void LCD_Command(unsigned char cmnd)


{
LCD_Data_Port= cmnd;
LCD_Command_Port &= ~(1<<RS); /* RS=0 command reg. */
LCD_Command_Port &= ~(1<<RW); /* RW=0 Write operation */
LCD_Command_Port |= (1<<EN); /* Enable pulse */
_delay_us(1);
LCD_Command_Port &= ~(1<<EN);
_delay_ms(3);
}

void LCD_Char (unsigned char char_data) /* LCD data write function */


{
LCD_Data_Port= char_data;
LCD_Command_Port |= (1<<RS); /* RS=1 Data reg. */
LCD_Command_Port &= ~(1<<RW); /* RW=0 write operation */
LCD_Command_Port |= (1<<EN); /* Enable Pulse */
_delay_us(1);
LCD_Command_Port &= ~(1<<EN);
_delay_ms(1);
}

void LCD_Init (void) /* LCD Initialize function */


{
LCD_Command_Dir = 0xFF; /* Make LCD command port direction as o/p */
LCD_Data_Dir = 0xFF; /* Make LCD data port direction as o/p */
_delay_ms(20); /* LCD Power ON delay always >15ms */

LCD_Command (0x38); /* Initialization of 16X2 LCD in 8bit mode */


LCD_Command (0x0C); /* Display ON Cursor OFF */
LCD_Command (0x06); /* Auto Increment cursor */
LCD_Command (0x01); /* Clear display */
LCD_Command (0x80); /* Cursor at home position */
}

void LCD_String (char *str) /* Send string to LCD function */


{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
LCD_Char (str[i]);
}
}

void LCD_String_xy (char row, char pos, char *str)/* Send string to LCD with xy position */
{
if (row == 0 && pos<16)
LCD_Command((pos & 0x0F)|0x80); /* Command of first row and required position<16
*/
else if (row == 1 && pos<16)
LCD_Command((pos & 0x0F)|0xC0); /* Command of first row and required position<16
*/
LCD_String(str); /* Call LCD string function */
}

void LCD_Clear()
{
LCD_Command (0x01); /* clear display */
LCD_Command (0x80); /* cursor at home position */
}

int main()
{

LCD_Init(); /* Initialize LCD */

LCD_String("ElectronicWINGS"); /* write string on 1st line of LCD*/


LCD_Command(0xC0); /* Go to 2nd line*/
LCD_String("Hello World"); /* Write string on 2nd line*/

return 0;
}

You might also like