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 / Interfacing LCD using AVRatmega32 with 4 bit mode

Interfacing LCD using AVRatmega32 with 4 bit mode

|

Microcontroller › AVR › Interfacing LCD using AVRatmega32 with 4 bit mode

  • This topic has 2 replies, 2 voices, and was last updated 12 years, 5 months ago by BhanuKiran Chaluvadi.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • December 17, 2012 at 4:14 am #2024
    BhanuKiran Chaluvadi
    Participant

     

    Hai ,

    I am using  PD3 for RS Register

                      PD6       R/W

                      PB4       Enable    and PB0-PB3 for Data Transfer. 

    I am using the same code given in this website making few changes and the code is as follows. Nothing is being displayed on the LCD . Is it because the way i am transfering 4 bits of data .. Do i need to change masking because my Data Transfer ports are PB0 to PB3 not PB4 to PB7.

     

     

    #include<avr/io.h>
    #include<util/delay.h>
    #include<inttypes.h>
     
    #define rs PD3
    #define rw PD6
    #define en PB4
     
    void lcd_init(void);
    void dis_cmd(char);
    void dis_data(char);
    void lcdcmd(char);
    void lcddata(char);
     
    int main(void)
    {
    unsigned char data0[11]=”ENGINEERS”;
    unsigned char data1[10]=”GARAGE”;
     
    int i=0;
    DDRB=0xFF;
    DDRD=0xFF;
    lcd_init(); 
     
    while(data0!=’’)
    {
    dis_data(data0);
    _delay_ms(200);
    i++;
    }
     
    dis_cmd(0xC5);
     
    i=0;
    while(data1!=’’)
    {
    dis_data(data1);
    _delay_ms(200);
    i++;
    }
     
    while(1);
    }
     
     
     
    void lcd_init() // fuction for intialize 
    {
    dis_cmd(0x02); // to initialize LCD in 4-bit mode.
    dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
    dis_cmd(0x0C);
    dis_cmd(0x06);
    dis_cmd(0x83);
    }
     
    void dis_cmd(char cmd_value)
    {
    char cmd_value1;
     
    cmd_value1 = cmd_value & 0xF0; //mask lower nibble because PA4-PA7 pins are used. 
    lcdcmd(cmd_value1); // send to LCD
     
    cmd_value1 = ((cmd_value<<4) & 0xF0); //shift 4-bit and mask
    lcdcmd(cmd_value1); // send to LCD
    }
     
     
    void dis_data(char data_value)
    {
    char data_value1;
     
    data_value1=data_value&0xF0;
    lcddata(data_value1);
     
    data_value1=((data_value<<4)&0xF0);
    lcddata(data_value1);
    }
     
    void lcdcmd(char cmdout)
    {
    PORTB=cmdout;
    PORTD&=~(1<<rs);
    PORTD&=~(1<<rw);
    PORTB|=(1<<en);
    _delay_ms(1);
    PORTB&=~(1<<en);
    }
     
    void lcddata(char dataout)
    {
    PORTB=dataout;
    PORTD|=(1<<rs);
    PORTD&=~(1<<rw);
    PORTB|=(1<<en);
    _delay_ms(1);
    PORTB&=~(1<<en);
    }
     
    December 17, 2012 at 5:55 am #8840
    AJISH ALFRED
    Participant

    Hi,

    If your Data Transfer ports are PB0 to PB3 then mask the higher nibbles when writing, not the lower nibbles.In this case there is no left shifting of data is required, simply write it to port B with higher nibbles masked.

    December 22, 2012 at 12:54 pm #8873
    BhanuKiran Chaluvadi
    Participant

    Thank u Sir…

    This is the final code and its working…

     

     

    // Program to interface LCD in 4 bit mode with AVR microcontroller
    //#define F_CPU 12000000UL
    #include<avr/io.h>
    #include<util/delay.h>
    #include<inttypes.h>
     
    #define rs PD3
    #define rw PD6
    #define en PB4
     
    void lcd_init(void);
    void dis_cmd(char);
    void dis_data(char);
    void lcdcmd(char);
    void lcddata(char);
     
    int main(void)
    {
    unsigned char data0[11]=”JAI”;
    unsigned char data1[10]=”BALAYYA”;
     
    int i=0;
    DDRA=0xFF;
    DDRD=0xFF; //modified
    DDRB=0xFF; //modified
     
    lcd_init();
     
    while(data0!=’’)
    {
    dis_data(data0);
    _delay_ms(200);
    i++;
    }
     
    dis_cmd(0xC5);
     
    i=0;
    while(data1!=’’)
    {
    dis_data(data1);
    _delay_ms(200);
    i++;
    }
     
    while(1);
    }
     
     
     
    void lcd_init() // fuction for intialize 
    {
    dis_cmd(0x02); // to initialize LCD in 4-bit mode.
    dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
    dis_cmd(0x0C);
    dis_cmd(0x06);
    dis_cmd(0x83);
    }
     
    void dis_cmd(char cmd_value)
    {
    char cmd_value1;
     
    cmd_value1 = (cmd_value>>4) & 0x0F; //mask lower nibble because PA4-PA7 pins are used. 
    lcdcmd(cmd_value1); // send to LCD
     
    cmd_value1 = ((cmd_value) & 0x0F); //shift 4-bit and mask
    lcdcmd(cmd_value1); // send to LCD
    }
     
     
    void dis_data(char data_value)
    {
    char data_value1;
     
    data_value1=(data_value>>4)&0x0F;
    lcddata(data_value1);
     
    data_value1=((data_value)&0x0F);
    lcddata(data_value1);
    }
     
    void lcdcmd(char cmdout)
    {
    PORTB=cmdout;
    PORTD&=~(1<<rs);
    PORTD&=~(1<<rw);
    PORTB|=(1<<en);
    _delay_ms(1);
    PORTB&=~(1<<en);
    }
     
    void lcddata(char dataout)
    {
    PORTB=dataout;
    PORTD|=(1<<rs);
    PORTD&=~(1<<rw);
    PORTB|=(1<<en);
    _delay_ms(1);
    PORTB&=~(1<<en);
    }
     
     
  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Chinese Tarrifs – 104%!?! May 21, 2025
  • An Update On Tarrifs May 21, 2025
  • Tariff Updates from JLCPCB as of Today May 21, 2025
  • Solar lighting motion detector May 21, 2025
  • Telegram Based Alarm - Sensor cable protection May 20, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 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