Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

uart program for LCD

Status
Not open for further replies.

Parth86

Member
I studied the basic of uart communication. Following is program for uart communication
C:
#include <reg51.h>        /* Include x51 header file */

void UART_Init()
{
    TMOD = 0x20;        /* Timer 1, 8-bit auto reload mode */
    TH1 = 0xFD;        /* Load value for 9600 baud rate */
    SCON = 0x50;        /* Mode 1, reception enable */
    TR1 = 1;        /* Start timer 1 */
}

void Transmit_data(char tx_data)
{
    SBUF = tx_data;        /* Load char in SBUF register */
    while (TI == 0);        /* Wait until stop bit transmit */
    TI = 0;            /* Clear TI flag */
}

void String(char *str)
{
    int i;
    for(i = 0; str[i] != 0; i++)    /* Send each char of string till the NULL */
    {
        Transmit_data(str[i]);    /* Call transmit data function */
    }
}

void main(void)
{
    UART_Init();        /* UART initialize function */
    String("hello");        /* Transmit 'test' */
    while(1);
}
8051 micro-controller, keil, LCD 16*2
Now I just want to send data from micro-controller to LCD but I am stuck here I don't know how to do it

I can also write code for following function
  • STEP1: Initialization of LCD.
  • STEP2: Sending command to LCD.
  • STEP3: Writing the data to LCD
Please tell me how to write program to send data from 8051 to LCD via uart?
 
Writing to the LCD is virtually the same as writing to the serial..

NOTE** try to make your identifiers unique... Your function "string(); " is a bad idea..

Lets assume you are ( trying ) to make a terminal... ie.. Read from PC then display on LCD.

You can do it character by character... But when polling is used its better line by line!

Case 1:-

main loop
is a character in the receive register
Yes!!! take character place it on screen.​
goto loop.
Case 2:-

main loop
is a character in the receive register
Yes!!!
while character in receive register isn't a carriage return place in buffer
if character in buffer is carriage return print line to screen​
goto loop.
We'll take case 1...
When the character appears in the receive register you decide a) print to LCD b) process it.
process!! ie.. There are 16 characters and 2 lines... When the character is a line feed you need to deal with it.

"Hello world!" arrives on the serial port BUT!! there are 14 characters needed 12 ascii and a carriage return and a null!
Obviously a null doesn't arrive on the serial port! you have to put it there..

I'll do a bit of code and get back to you.... Might be tonight when I get home..
 
NOTE** try to make your identifiers unique... Your function "string(); " is a bad idea..
I store letter like this in c programming (mingw compiler)
C:
#include<stdio.h>
int main (void)
{
 unsigned int i;
  char array[5]={'h','e','l','l','o'};
 
  for(i=0; i<5; i++)
 {
   printf("print letter = %c \n", array[i]);
 }
 return 0;
}
print letter = h
print letter = e
print letter = l
print letter = l
print letter = o

Print string
C:
#include<stdio.h>
int main (void)
{
 char array[]={"hello"};
 
 {
   printf("print string = %s \n", array);
 }
 return 0;
}
print string = hello
 
I studied the basic of uart communication. Following is program for uart communication
8051 micro-controller, keil, LCD 16*2
Now I just want to send data from micro-controller to LCD but I am stuck here I don't know how to do it

I can also write code for following function
  • STEP1: Initialization of LCD.
  • STEP2: Sending command to LCD.
  • STEP3: Writing the data to LCD
Please tell me how to write program to send data from 8051 to LCD via uart?

Just to clarify, most 1602 LCD displays are parallel communication devices (14 or 16 pin devices). Do you have one with an onboard shift register to allow serial communication (UART) - 8 or fewer pins.
 
I store letter like this in c programming (mingw compiler)
C:
#include<stdio.h>
int main (void)
{
 unsigned int i;
  char array[5]={'h','e','l','l','o'};
 
  for(i=0; i<5; i++)
 {
   printf("print letter = %c \n", array[i]);
 }
 return 0;
}
print letter = h
print letter = e
print letter = l
print letter = l
print letter = o
That is just as bad. Stop naming your variable names after their type. It's like calling naming your dog, Dog. It becomes very confusing once you have more than one. Dont name anything string, array, integer, character etc. Give them real unique names.
 
I don't usually like just doing it for you as you probably won't understand the functions...

However try and understand what I have done..

C:
/* Main.c file generated by Ian Rogers
 *
 * Created:   Wed Jan 24 2018
 * Processor: AT89C51
 * Compiler:  SDCC for 8051
 */
#include <mcs51reg.h>
#define LCDport P1           /* Data pins connected to port P1 */
#define RS  P3_5         /* RS pin connected to pin 5 of port P3 */
#define RW  P3_6           /* RW pin connected to pin 6 of port P3 */
#define EN  P3_7           /* EN pin connected to pin 7 of port P3 */
/* functions for delay */
 void delayUs(unsigned int wait)
   {
     wait >>= 3;
      while(wait--);
   }
  
 void delayMs(unsigned int wait)
   {
      while(wait--)
  delayUs(1000);
   }
/* Function to send command to LCD */
void LCD_Command(unsigned char cmd)
   {
    LCDport = cmd;
    RS = 0;
    RW = 0;
    EN = 1;
    delayMs(5);
    EN = 0;
   }
  
/*Function to send data to LCD */
void LCD_Data(unsigned char data)
   {
    LCDport = data;
    RS = 1;
    RW = 0;
    EN = 1;
    delayMs(5);
    EN = 0;
   }
/* Function to prepare the LCD */
void LCD_init(void)
   {
    delayMs(20);
    LCD_Command(0x33);  // Init 8 it
    delayMs(15);
    LCD_Command(0x38);  // Function set
    delayMs(15);
    LCD_Command(0x0C);  // display on
    delayMs(10);
    LCD_Command(0x06);  // direction
    delayMs(5);
    LCD_Command(0x01);  // home...
    delayMs(5);
   }
  
  /* Function to position cursor on the LCD */
void LCD_goto(int x, int y)
   {
      unsigned char addr = 0x80;      // default line
       if (y ==2 ) addr = 0xC0;  // 2nd line
      if (y ==3 ) addr = 0x90;     // 3rd line
      if (y ==4 ) addr = 0xD0;  // 4th line
      addr+=x;     // Column required
      LCD_Command(addr);
    }
   
  /* Function to print on the LCD */  
 void LCD_print(int x, int y, char* str)
      {
  LCD_goto(x,y);
  while(*str!=0)      // this is a ponter  This means...
     LCD_Data(*str++); // While the character pointed to isn't 0 print to LCD then increment..
      }
   
  /* Function to pset up Uart */     
void UART_Init()
   {
    TMOD = 0x20; /* Timer 1, 8-bit auto reload mode */
    TH1 = 0xFD; /* Load value for 9600 baud rate */
    SCON = 0x50; /* Mode 1, reception enable */
    TR1 = 1;  /* Start timer 1 */
   }
   
  /* Function to send a character */  
void putch(char tx_data)
   {
    SBUF = tx_data;  /* Load char in SBUF register */
    while (TI == 0); /* Wait until stop bit transmit */
    TI = 0;    /* Clear TI flag */
   }
   /* Function to fetch a character */  
 int getch(void)
   {
    while(!RI);        // Wait here until SBUF clear
    RI = 0;
    return SBUF;     
    }
  
  /* Function to send a line of characters untill a null*/  
void puts(char *str)
   {
   while(*str!=0) // while there is a valid character
      putch(*str++);   // send it on RS232
   }
void main(void)
   {
    char data;  // receive variable
    int pos = 0;  // where we are on screen
    UART_Init();        /* UART initialize function */
    LCD_init();  // set up LCD
    puts("hello");        /* Transmit 'test' */
    while(1)
       if(RI)   // If RI is set a character has arrived
   {
    data =  SBUF;   // Get data
     LCD_Data(data);  // put it in next cursor position
      pos++;
     if(pos == 16) LCD_goto(0,2); // wrap at end of line 1
     if(pos == 32) LCD_goto(0,3); // wrap at end of line 2
     if(pos == 48) LCD_goto(0,4); // wrap at end of line 3
     if(pos == 64) {LCD_Command(1); pos=0;} // wrap at end of line 4
    RI=0;        // Clear flag
    }
   }
 
I don't usually like just doing it for you as you probably won't understand the functions...

However try and understand what I have done..

C:
/* functions for delay */
 void delayUs(unsigned int wait)
   {
     wait >>= 3;
      while(wait--);
   }
 
 void delayMs(unsigned int wait)
   {
      while(wait--)
  delayUs(1000);
   }
Generally you are creating delays. why didn't create function like this
C:
int main(void)
{
   delay(1000); /* 1000 microseconds */
    delay(1);    /* 1 milliseconds */
}

void delay(unsigned int wait)
{
   unsigned int i;
 
   for (i = 0; i < wait; i++)
    {
      }
}
Does it not provide delay ?
 
Generally you are creating delays. why didn't create function like this
C:
int main(void)
{
   delay(1000); /* 1000 microseconds */
    delay(1);    /* 1 milliseconds */
}

void delay(unsigned int wait)
{
   unsigned int i;
 
   for (i = 0; i < wait; i++)
    {
      }
}
Does it not provide delay ?

Anything that eats up clock cycles and does nothing is a reasonable delay.
 
I have been programming micro's for some time.... I need delays for milli and micro seconds..

delay(1000) = should equal 1ms but no!!! for loop takes time and the initial call takes time..

for( i = 0; i<wait;i++) is normally around 11 clock cycles

so a delay(1000) will be around 12mS not 1mS... while(wait--) is much much faster!!
 
so a delay(1000) will be around 12mS not 1mS... while(wait--) is much much faster!!
I didn't understand the delay function you created
C:
#include<stdio.h>
void delayUs(unsigned int wait);
int main(void)
{
    delayUs(10);   
   printf("micro second delay %d",delayUs());
   return 0;
   
}
   
 void delayUs(unsigned int wait)
   {
     wait >>= 3;   //wait = wait >> 3 //
      while(wait--);
   }
error: too few arguments to function 'delayUs'
printf("micro second delay %d",delayUs());

ram.c:2:6: note: declared here
void delayUs(unsigned int wait);

what's wrong in this program
 
printf("micro second delay %d",delayUs());
You have to pass a delay variable...

I'm not getting into a debate!!! The code I posted was so you can see how to use LCD and RS232... I'm not going to sort out mundane stuff like delay functions.... Use your own if necessary... You asked about communication, not delays!!!!
 
You have to pass a delay variable...

I'm not getting into a debate!!! The code I posted was so you can see how to use LCD and RS232... I'm not going to sort out mundane stuff like delay functions.... Use your own if necessary... You asked about communication, not delays!!!!
Thanks.. Now I will take time and understand it
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top