EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / 4 bit LCD interfacing with ATmega32

4 bit LCD interfacing with ATmega32

|

Microcontroller › AVR › 4 bit LCD interfacing with ATmega32

  • This topic has 7 replies, 6 voices, and was last updated 13 years, 6 months ago by AJISH ALFRED.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • April 25, 2011 at 4:32 am #881
    anjum
    Participant

    please help me regarding 4 bit LCD intrfacing with atmega 32. my hardware description and code is given below:

     

    H/W

    DB7-DB4 of lcd connected to PORTD5 to PORTD2

    RS in connected to PORTD7

    E is connected to PORTD6

    RW is connected to ground

     

    code:

     

     

    // Program to interface LCD in 4 bit mode with AVR microcontroller
    #define F_CPU 11059200UL
    #include<avr/io.h>
    #include<util/delay.h>
    #include<inttypes.h>
     
    #define en 6
    #define RS 7
     
    #define set_command PORTD&=~(1<<RS);
    #define set_data PORTD|=(1<<RS);
    #define set_enable PORTD|=(1<<en);
    #define reset_enable PORTD&=~(1<<en);
     
     
    void lcd_init();
    void lcd_init_write(unsigned char);
    void dis_data(unsigned char);
    void lcd_com(unsigned char);
    void lcddata(unsigned char);
     
    int main(void)
    {
    unsigned char data0[11]=”aaaaa”;
    unsigned char data1[10]=”aaaaa”;
     
    int i=0;
    DDRD=0xFF;
    lcd_init();
     
    while(data0!=’’)
    {
    dis_data(data0);
    _delay_ms(200);
    i++;
    }
     
    lcd_com(0xC5);
     
    i=0;
    while(data1!=’’)
    {
    dis_data(data1);
    _delay_ms(200);
    i++;
    }
     
    while(1);
    }
     
     
     
    void lcd_init() // fuction for intialize 
     
    {
                    _delay_ms(40); ;
                    lcd_init_write(0x30);   //Special Sequence:Write Function Set.
                    _delay_us(4100); 
                    lcd_init_write(0x30);              //Special Sequence:Write Function Set.
                    _delay_us(100); 
                   lcd_init_write(0x30);              //Special Sequence:Write Function Set.
                    _delay_us(40); 
                    lcd_init_write(0x20);   // 0x20 for 4-bit
                    _delay_us(40); 
                    lcd_com(0x28);         //Display Off, Cursor Off, Blink Off
                      _delay_us(40); 
                    lcd_com(0x08);                                                // Clear Screen & Returns the Cursor Home
                      _delay_us(40); 
                    lcd_com(0x01);       
                 _delay_us(1600);       
                    lcd_com(0x06);           //Inc cursor to the right when writing and don’t shift screen
                     _delay_us(40); 
                    lcd_com(0x0c);
                      _delay_us(40); 
    }
     
    void lcd_init_write(unsigned char a)
    {
                    set_command;   //RS=0;
                    PORTD=(a>>2); //DB7-DB4 connected to PORTD5-PORTD2
                    set_enable; //E=1;
                    _delay_ms(10); //(lcd_delay);
                    reset_enable;//E=0;
    _delay_us(40);
    }
     
    void lcd_com(unsigned char a)
    {
                    unsigned char temp;
                
                    set_command; //RS=0;
                   
                                                                                     
                    temp=a;                                                                   
                    temp&=0xf0;
    temp=(temp>>2);                                                          // Mask Lower 4 Bits
                   PORTD&=0xc3; 
                                                 // Make No Affect on 0ther Port Pins
           PORTD|=temp;                                 // Send Higher Nibble to LCDPORT
                    set_enable;//E=1;
                    _delay_ms(10);                   //Send Enable Signal to LCD
                    reset_enable;//E=0;                                                        
                    temp=a<<4;                                                            //Left Shift Byte Four Times
                    temp&=0xf0;                                                          // Mask Higher 4 Bits
                   temp=(temp>>2);                                                          // Mask Lower 4 Bits
                   PORTD&=0xc3;            // Make No Affect on 0ther Port Pins
                    PORTD|=temp;                                 // Send Lower Nibble to LCDPORT
                    set_enable;//E=1;
                    _delay_ms(10);                     // Send Enable Signal to LCD
                    reset_enable;//E=0;
    _delay_us(40);
    }
     
     
     
    void dis_data(unsigned char data_value)
    {
    unsigned char data_value1;
    data_value1=data_value;
     
    data_value1 &=0xF0;
     
    data_value1=(data_value1>>2);
     
    lcddata(data_value1);
     
    data_value1=((data_value<<4)&0xF0);
    data_value1=(data_value1>>2);
    lcddata(data_value1);
    }
     
     
     
    void lcddata(unsigned char dataout)
    {
    set_data;
    PORTD=dataout;
    set_enable;
    _delay_ms(1);
    reset_enable;
    }
     
    June 20, 2011 at 3:37 am #6363
    kari
    Participant

    And what is problem????

    November 19, 2011 at 7:22 am #6853
    Tuhin
    Participant

    you just reffer \extremeelectronics.co.in\ and read the AVR lcd interfaceing tutorial. you may  download 

    the lcd.c , lcd.h, and myutils.h file and add source and header file to your main program and then you can easily use lcd as per your requirement.

     

     

    But remember if you are going to use this source file keep one thing in mind you have to connect the datalines of lcd DB4-DB7 with 0-3 no. pins of any port. you have to use the first 4 bits of any portof mcu

     

        k..

    August 7, 2012 at 10:57 am #8421
    a
    Participant

    can any one send me code for display characters on lcd interfacing with atmega 32. i am beginner so i am finding many problems . i am using code vision avr c compiler. my email id is [email protected]

    August 10, 2012 at 6:29 am #8431
    Amrith
    Participant

    Dear Friend,

    Posting a working code is not the solution. You need proper & working hardware & details of LCD port connections e.t.c. Share you connection decription so that one can understand where exactly you connected the LCD & other connections. This will help you out with expected result.

    August 10, 2012 at 6:22 pm #8441
    AJISH ALFRED
    Participant

    Hi Anjum,

    Without knowing what issue are you facing with the project, how can someone help you with it?

    August 13, 2012 at 11:44 am #8449
    a
    Participant

    i wana display msg on lcd interfaced with atmega 32. i want to use PB.0-rs, PB.1-rw, PB.2- en , and want to use data pins of same port i.e PB.4 TO PB.7. Here is my code…..cn ny 1 help me to solv out the prblm. wn i burn this code nthing is display on lcd.

     

     

    //Program to Display string on LCD using AVR Microcontroller (ATmega16)
    /*
    LCD DATA port—-PORT B
    signal port


    PORT D
        rs


    PD0
        rw


    PD1
        en


    PD2
    */
     
    #include<mega32.h>
    #include<delay.h>
     
    #define LCD_DATA PORTB        //LCD data port
     
    #define ctrl PORTB
    #define en PORTB2        // enable signal
    #define rw PORTB1        // read/write signal
    #define rs PORTB0        // register select signal
     
    void LCD_cmd(unsigned char cmd);
    void init_LCD(void);
    void LCD_write(unsigned char data);
    void LCD_write_string(unsigned char *str) ;
     
     
    void  main()
    {
        DDRB=0xff;        
        PORTB=0xf0;        
        init_LCD();        // initialization of LCD
        delay_ms(50);        // delay of 50 mili seconds
        LCD_write_string(“sushilprajapati”);    // function to print string on LCD
        
    }
     
    void init_LCD(void)
    {
        LCD_cmd(0x38);        // initialization of 16X2 LCD in 8bit mode
        delay_ms(1);
     
        LCD_cmd(0x01);        // clear LCD
        delay_ms(1);
     
        LCD_cmd(0x0E);        // cursor ON
        delay_ms(1);
     
        LCD_cmd(0x80);        // —8 go to first line and –0 is for 0th position
        delay_ms(1);
        return;
    }
     
    void LCD_cmd(unsigned char cmd)
    {
        LCD_DATA=cmd;
        ctrl =(0<<rs)|(0<<rw)|(1<<en);    
        delay_ms(1);
        ctrl =(0<<rs)|(0<<rw)|(0<<en);    
        delay_ms(50);
        return;
    }
     
    void LCD_write(unsigned char data)
    {
        LCD_DATA= data;
        ctrl = (1<<rs)|(0<<rw)|(1<<en);    
        delay_ms(1);
        ctrl = (1<<rs)|(0<<rw)|(0<<en);    
        delay_ms(50);                
        return ;
    }
     
    void LCD_write_string(unsigned char *str)    //store address value of the string in pointer *str
    {
        int i=0;
        while(str!=’’)                // loop will go on till the NULL character in the string 
        {
            LCD_write(str);                // sending data on LCD byte by byte
            i++;
         }
        return;
    }
     
    August 13, 2012 at 4:45 pm #8453
    AJISH ALFRED
    Participant

    Hi,

    If everything else is properly coded, then I can see the same thing with which I’ve struggled a lot few years back. And here is my workaround.

    Just change the parameter of the function LCD_write_string to const char *str, in both decleration and defenition, like the following

     

    void LCD_write_string(const char *str)

  • Author
    Posts
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Supply vs performance query February 7, 2026
  • wall transformer polarity February 7, 2026
  • Do i need a buffer? February 7, 2026
  • BPF February 7, 2026
  • Figgie International intercom 1998 era February 7, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise