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..
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.
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'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!!!!
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!!!!