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 / Replies / use this codingit may help

use this codingit may help

|

Microcontroller › 8051 › Serial communication in lcd › use this codingit may help

December 28, 2011 at 4:14 am #6955
Dexter
Participant

use this coding

it may help u

re check according to ur circuit

 

#include <reg51.H>
#include <stdio.h>
#include <string.h>

#define LCD_PORT P1            //lcd data port

/*        PIN CONNECTIONS            */
sbit    EN=P3^2;        //enable pin for LCD
sbit    RW=P3^3;        //write pin for LCD         
sbit    RS=P3^4;        //register select pin of LCD

/*        Declarations         */
//  DELAYS
void del_1ms        (void);                    //1 milli second delay
void del_30ms        (void);                    //30 milli second delay
void del_1s            (void);                    //delay of 1 second
void del_3ms        (void);                    //delay of 3 milli seconds

//  LCD
void lcd_init        (void);                //routine to initialise LCD
void printLcd        (unsigned char *);        //prints a line on LCD
void printLcd2        (unsigned char *);        //prints a line on LCD
void clear_display    (void);                    //clears LCD screen
void display_data    (unsigned char);        //displays one char
void set_add        (unsigned char );        //sets the display position
void write_in        (unsigned char );        //writes data in to LCD as instruction
void write_data        (unsigned char );        //writes one byte into LCD as data
void write            (unsigned char );        //writes one byte into LCD
void lcd_cmdwrt(unsigned char );
void lcd_datawrt(unsigned char *);

//  SERIAL COMMUNICATION
void serial_init        (void);                        //initialise serial communication
unsigned char recv_byte    (void);                        //recieves th ASCII value
void send_byte            (unsigned char);            //sends ASCII value to PC or microcontroller
void sendString            (unsigned char *str);
void sendString1        (unsigned char *str);
unsigned char volatile cha,i=0;

void main()
{
    unsigned char volatile no;
    lcd_init();
    serial_init();                 //initialise lcd display
    clear_display();
    printLcd(“SYSTEM INSTALING”);
    printLcd2(“..”);      

    while(1)
    {
        run1=recv_byte();
            if(isalpha(run1) != 0)  display_data(run1);
            else if(isdigit(run1) !=0) break;
    }
    display_data(run1);
    del_1s();
  
  }

void lcd_cmdwrt(unsigned char cmd)
{

      P1=cmd;
      RS=0;
    RW=0;
      EN=1;
      _nop_();
      _nop_();
      _nop_();
      EN=0;
      for(i=0;i<200;i++);    
      for(i=0;i<200;i++);
}

void lcd_datawrt(unsigned char *dat)
{
     while(*dat)
     {
           P1=*dat;
           RS=1;
        RW=0;
           EN=1;
           _nop_();
        _nop_();
        _nop_();
        EN=0;
        for(i=0;i<200;i++);
          for(i=0;i<200;i++);
           dat++;
     }
}
//LCD Routines
//clears the entire display area of LCD

void clear_display(void)
{
    write_in(0x01);            //clears the lcd display
    write_in(0x80);            //write code to blink at first line first coloum
}

    
//routine to display the value in the accumulator in LCD

void display_data(unsigned char ch)
{
    unsigned char dummy;

    dummy = ch;            //backup in variable
    write_data(ch);        //writes the value to LCD module
}
    
//routine to display a string on first line
//this routine will display a string stored at address pointed by dptr

void printLcd(unsigned char *str)
{
    unsigned char len,i;
    
    len = strlen(str);
    set_add(0x80);                //sets display position in first line
    for(i=0;i<len;i++)
    {
        display_data(*str);        //displays the character on LCD
        str++;
    }
}

void printLcd2(unsigned char *str)
{
    unsigned char len,i;
    
    len = strlen(str);
    set_add(0xc0);        //sets display position in second  line
    
    for(i=0;i<len;i++)
    {
        display_data(*str);    //displays the character on LCD
        str++;
    }
}

//routine to set the display position
    
void set_add(unsigned char ch)
{
    unsigned char dummy;
    
    dummy = ch;            //backup in variable
    write_in(ch);        //writes it as instruction
}

//routine to write an instruction to LCD

void write_in(unsigned char ch)
{
    RS=0;                //rs =0 for instr
    RW=0;            
    write(ch);            //routine to transfer 8 bits to LCD
}

//routine to write a data to LCD

void write_data(unsigned char ch)
{
    RS = 1;
    RW=0;                //rs = 1 for data
    write(ch);            //transfers 8 bits to lcd module    
}

//routine to transfer 8 bits to LCD module
    
void write(unsigned char ch)
{
    LCD_PORT = ch;        //preserves the old value of port
    EN = 1;                //enable pin high
    del_1ms();
    EN = 0;                //enable = 0
    del_1ms();
}

//routine to initialise the lcd

void lcd_init(void)
{
    del_30ms();            
    write_in(0x38);          //to make lcd
    del_3ms();
    write_in(0x0f);          //to make lcd to select 5×7 matrix display
    del_3ms();
     write_in(0x01);          //to make lcd to clear the display
    del_3ms();
    write_in(0x80);          //to make lcd cursor to bigining of first line
    del_3ms();
}

//SERIAL ROUTINES

void serial_init()
{
    SCON=0x50;                        //mode 1 serial communication
    TMOD=0x20;                        //timer 1 auto reload mode
    TH1=0xfd;                        //9600 baudrate at 11.0592 MHz
    TR1=1;
    EA=1;
    EX0=1;
    IT0=1;
    IP=0x10;
}

unsigned char recv_byte()
{
    unsigned char c;
    while(!RI);                        //waits till a byte is recieved
    RI = 0;
    c = SBUF;
    return(c);
}

void send_byte(unsigned char ch)
{
    TI = 0;
    SBUF = ch;
    while(!TI);
    del_1ms();
}

void sendString(unsigned char *str)
{
    while(*str != ‘’)
        send_byte(*str++);
}

//DELAY Routines

//Delay: 3 milli seconds
 
void del_3ms(void)
{
    unsigned char i,j;
    for(i=0;i<15;i++)
        for(j=0;j<98;j++);
}

//Delay: 30 milli seconds    
    
void del_30ms()
{
    unsigned char j;
    for(j=0;j<10;j++)
        del_3ms();
}

void del_1s()
{
    unsigned char i;
    for(i=0;i < 60;i++)
        del_30ms();
}

void del_1ms(void)
{
    unsigned char j;
    for(j=0;j<100;j++);
}

}

 

RSS Recent Posts

  • restarting this Christmas project November 14, 2025
  • desoldering November 13, 2025
  • Unknown, to me, electric motor fitting November 13, 2025
  • Can a small solar panel safely trickle-charge old NiMH AA batteries? November 13, 2025
  • KiCad custom symbol definition correct approach November 13, 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