100% found this document useful (1 vote)
165 views

LCD Code For Avr

This document describes code for interfacing with an LCD display using an AVR microcontroller. It defines macros for the LCD data and command ports and pins. It includes functions for sending commands and data to the LCD via 4-bit mode, initializing the LCD, moving the cursor, and printing strings. The main function initializes the LCD, moves the cursor, and prints strings to demonstrate the LCD interfacing code.

Uploaded by

Aadhya Suman
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
165 views

LCD Code For Avr

This document describes code for interfacing with an LCD display using an AVR microcontroller. It defines macros for the LCD data and command ports and pins. It includes functions for sending commands and data to the LCD via 4-bit mode, initializing the LCD, moving the cursor, and printing strings. The main function initializes the LCD, moves the cursor, and prints strings to demonstrate the LCD interfacing code.

Uploaded by

Aadhya Suman
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <avr/io.h> //standard AVR header #include <util/delay.

h> //delay header #define LCD_DPRT PORTA //LCD DATA PORT #define LCD_DDDR DDRA //LCD DATA DDR #define LCD_DPIN PINA //LCD DATA PIN #define LCD_CPRT PORTB //LCD COMMANDS PORT #define LCD_CDDR DDRB //LCD COMMANDS DDR #define LCD_CPIN PINB //LCD COMMANDS PIN #define LCD_RS 0 //LCD RS #define LCD_RW 1 //LCD RW #define LCD_EN 2 //LCD EN //******************************************************* void delay_us(unsigned int d) { _delay_us(d); } //******************************************************* void lcdCommand( unsigned char cmnd ) { LCD_DPRT = cmnd; // send cmnd to data port LCD_CPRT &= ~(1<<LCD_RS); // RS =0 for command LCD_CPRT &= ~(1<<LCD_RW); //RW = 0 for write LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse delay_us(1); //wait to make enable wide LCD_CPRT &= ~ (1<<LCD_EN); //EN = 0 for H-to-L pulse delay_us(100); //wait to make enable wide } //******************************************************* void lcdData( unsigned char data ) { LCD_DPRT = data; //send data to data port LCD_CPRT |= (1<<LCD_RS); //RS = I for data LCD_CPRT &= ~ (1<<LCD_RW); //RW = 0 for write LCD_CPRT |= (1<<LCD_EN); //EN = 1 for H-to-L pulse delay_us(1); //wait to make enable wide LCD_CPRT &= ~ (1<<LCD_EN); //EN = 0 for H-to-L pulse delay_us(100); //wait to make enable wide } //******************************************************* void lcd_init() { LCD_DDDR = OxFF; LCD_CDDR = OxFF; LCD_CPRT &= ~ (1<<LCD_EN); delay_us(2000); lcdCommand(0x38); lcdCommand(0x0E); lcdCommand(0x01); delay_us(2000); lcdCommand(0x06); //LCD_EN = 0 //wait for init. //init. LCD 2 line, 5 x 7 matrix //display on, cursor on //clear LCD //wait //shift cursor right

} //******************************************************* void lcd_gotoxy(unsigned char x, unsigned char y) { unsigned char firstCharAdr[]={0x80,0xC0,0x94,0xD4};//Table 12-5

lcdCommand(firstCharAdr[y-1] + x - 1); delay_us(100); } //******************************************************* void lcd_print( char *str ) { unsigned char i = 0; while(str[i] !=0) { lcdData(str[i]); i++ ; } } //******************************************************* int main (void) { lcd_init(); lcd_gotoxy(1,1); lcd_print("The world is but"); lcd_gotoxy(1,2); lcd_print("one country"); while(1); //stay here forever return 0; } /////////////////////////////////////////////////////// /* Program 12-5: Communicating with LCD Using 8-bit Data in C CHAPTER 12: LCD AND KEYBOARD INTERFACING 445 Program 12-6 shows how to use 4-bit data to interface an LCD to the AVR in C lan guage. #include <avrao.h> //standard AVR header #include <util/delay.h> //delay header #define LCD_DPRT PORTA //LCD DATA PORT #define LCD_DDDR DDRA //LCD. DATA DDR #define LCD_DPIN PINA //LCD DATA PIN #define LCD_CPRT PORTB //LCD COMMANDS PORT #define LCD_CDDR DDRB //LCD COMMANDS DDR #define LCD_CPIN PINB //LCD COMMANDS PIN #define LCD_RS 0 //LCD RS #define LCD_RW 1 //LCD RW #define LCD EN 2 //LCD EN void delay us(int d) { _delay_us(d); } void lcdCommand( unsigned char cmnd ) { LCD_DPRT = cmnd & OxFO; //send high nibble to D4-D7 LCD_CPRT &= (1<<LCD_RS);//RS = 0 for command LCD_CPRT &= (1<<LCD_RW); //RW = 0 for write LCD_CPRT 1= (1<<LCD_EN); //EN = 1 for H-to-L pulse delay_us(1);//make EN pulse wider LCD_CPRT &= (1<<LCD_EN); //EN =0 for H-to-L pulse delay_us(100); //wait LCD_DPRT = cmnd<<4; //send low nibble to D4-D7

LCD_CPRT 1= (1<<LCD_EN);//EN = 1 for H-to-L pulse delay_us(1); //make EN pulse wider LCD_CPRT &= (1<<LCD_EN); //EN = 0 for H-to-L pulse delay_us(100); //wait } void lcdData( unsigned char data ) { LCD_DPRT = data & OxFO; //send high nibble to D4-D7 LCD_CPRT 1= (1<<LCD_RS);//RS= 1 for data LCD_CPRT &= (1<<LCD_RW); //RW = 0 for write LCD_CPRT 1= (1<<LCD_EN); //EN = 1 for H-to-L pulse delay_us(1); //make EN pulse wider LCD_CPRT &= (1<<LCD_EN);//EN = 0 for H-to-L pulse LCD_DPRT = data<<4;//send low nibble to D4-D7 LCD_CPRT 1= (1<<LCD EN); //EN = 1 for H-to-L pulse Program 12-6: Communicating with LCD Using 4-bit Data in C (continued on next pa ge) 446 delay_us(1); //make EN pulse wider LCD_CPRT &= (1<<LCD_EN); //EN = 0 for H-to-L pulse delay_us(100); //wait void lcd_init() LCD_DDDR = OxFF; LCD_CDDR = OxFF; LCD_CPRT &=-(1<<LCD_EN);//LCD _EN = 0 lcdCommand(0x33); //send $33 for init. lcdCommand(0x32); //send $32 for init. lcdCommand(0x28); //init. LCD 2 line,5x7 matrix lcdCommand(Ox0e); //display on, cursor on lcdCommand(0x01); //clear LCD delay_us(2000); lcdCommand(0x06); //shift cursor right void lcd_gotoxy(unsigned char x, unsigned char y) unsigned char firstCharAdr[]={0x80,0xCO30x94,0xD4}; lcdCommand(firstCharAdr[y-1] + x - 1); delay_us(100); void lcd_print(char * str ) { unsigned char i = 0; while(str[i] !=0) lcdData (strf ) ; i++ ; } int main (void) lcd_init(); lcd_gotoxy(1,1); lcd_print("The world is but") lcd_gotoxy(1,2); lcd_print("one country"); while (1); //stay here forever return 0; Program 12-6: Communicating with LCD Using 4-bit Data in C CHAPTER 12: LCD AND KEYBOARD INTERFACING 447 Program 12-7 shows how to use 4-bit data to interface an LCD to the AVR in C lan

guage. It uses only a single port. Also there are some useful functions to print a string (array of chars) or to move the cursor to a specific location. #include <avr/io.h> //standard AVR header #include <utilidelay.h> //delay header #define LCD_PRT PORTA //LCD DATA PORT #define LCD_DDR DDRA //LCD DATA DDR #define LCD PIN PINA //LCD DATA PIN #define LCD_RS 0 //LCD RS #define LCD_RW 1 //LCD RW #define LCD_EN 2 //LCD EN void delay_us(int d) { _delay_us(d); } void delay_ms(int d) { _delay ms(d); } void lcdCommand( unsigned char cmnd) LCD_PRT = (LCD_PRT & Ox0F)| (cmnd & OxF0) LCD_PRT &= (1<<LCD_RS); //RS = 0 for command LCD_PRT &= (1<<LCD_RW); //RW = 0 for write LCD_PRT 1= (1<<LCD_EN); //EN = 1 for H-to-L delay_us(1); //wait to make EN wider LCD_PRT &= (1<<LCD_EN); //EN = 0 for H-to-L delay us(20); //wait LCD_PRT = (LCD_PRT & Ox0F) | (cmnd << 4); LCD_PRT 1= (1<<LCD EN); //EN = 1 for H-to-L delay_us(1); //wait to make EN wider LCD_PRT &= (1<<LCD_EN); EN = 0 for H-to-L void lcdData( unsigned char data ){ LCD_PRT = (LCD_PRT & 0x0F) | (data & OxF0); LCD_PRT |=(1<<LCD_RS); LCD_PRT &= //RS = 1 for data LCD PRT 1=(1<<LCD_RS); //RW= 0 for write LCD_PRT |=(1<<LCD_EN); //EN = 1 for H-to-L Program 12-7: Communicating with LCD Using 4-bit Data in C (continued on next pa ge) 448 */

You might also like