I am doing a project on bluetooth based notice board. ?In this project, a user sends message using voice to text app. The message is then received by bluetooth module and sends to the 8051 which decodes it and sends to the LCD for display. .The LCD I am using is 20×4, so it can display at maximum 80 characters, but if it exceeds 80 characters the fourth line will start scrolling the message. Currently, the code I had provided can receive 9 characters. But how to modify this code for receiving different length strings?(as the input message the user provides each time will be of different length) The code is as follows:
#include <reg51.h> #include <string.h> #define LCD_Data_Bus P1 #define FirstLine 0x80 #define SecondLine 0xC0 #define ThirdLine 0x94 #define FourthLine 0xD4 #define MaxSupportedChars 20 #define MaxSupportedLines 4 #define BlankSpace ' ' unsigned int LcdTrackLineNum; //Variable to track the line numbers unsigned int LcdTrackCursorPos; //Variable to track the cursor unsigned int LcdLineNumAddress[]={0x80,0xc0,0x94,0xd4}; sbit _RS= P2^2; // Register select pin connected to P2.2 sbit _RW= P2^1; // Read/Write pin connected to P2.1 sbit _EN= P2^0; // Enable pin connected to P2.0 /* Function prototypes for UART */ void uart_init(); unsigned rx_data(); void delay_ms(unsigned int); /* Function prototypes for LCD */ void lcd_cmd(unsigned char cmd); void lcd_data(unsigned char ascii); void lcd_init(); void lcd_clear(); void LCD_DisplayChar(char LcdData); void lcd_msg(char *); void LCD_GoToNextLine(void); void main() { unsigned char read[10]; unsigned char i; lcd_init(); lcd_clear(); uart_init(); delay_ms(50); lcd_msg("n Bluetooth based n Notice Board "); delay_ms(2000); while(1) { lcd_clear(); lcd_cmd(0x80); for(i=0;i<10;i++) { read[i]=rx_data(); LCD_DisplayChar(read[i]); } } } void uart_init() // INITIALIZE SERIAL PORT { TMOD = 0x20; // Timer 1 IN MODE 2-AUTO RELOAD TO GENERATE BAUD RATE TH1 = 0xFD; // LOAD BAUDRATE TO TIMER REGISTER SCON = 0x50; // SERIAL MODE 1, 8-DATA BIT 1-START BIT, 1-STOP BIT, REN ENABLED TR1 = 1; // START TIMER } unsigned rx_data() { while(RI == 0); // WAIT UNTIL DATA IS RECEIVED RI = 0; // CLEAR FLAG return SBUF; // RETURN SERIAL DATA } void delay_ms(unsigned int time) { int i,j; for(i=0;i<=time;i++) for(j=0;j<=127;j++); } void lcd_cmd(unsigned char cmd) { _RS=0; // RS = 0 for Command. _RW=0; // RW = 0 for write. LCD_Data_Bus=cmd; // 8bit command to LCD. _EN=1; // Send a High-to-Low Pusle at Enable Pin. delay_ms(5); _EN=0; delay_ms(5); } void lcd_data(unsigned char ascii) { _RS=1; // RS = 1 for Data. _RW=0; // RW = 0 for write. LCD_Data_Bus=ascii; // 8bit command to LCD. _EN=1; // Send a High-to-Low Pusle at Enable Pin. delay_ms(2); _EN=0; delay_ms(2); } void lcd_init() { delay_ms(5); lcd_cmd(0x38); //Initilize the LCD in 8bit Mode lcd_cmd(0x0E); // Display ON cursor ON lcd_cmd(0x01); // Clear the LCD lcd_cmd(0x80); // Move the Cursor to First line First Position } void lcd_clear() { lcd_cmd(0x01); } void lcd_msg(char *ptr_str) { unsigned char i,j; int Size = strlen(ptr_str); if(Size > 80) { for(i=0;i<Size;i++) LCD_DisplayChar(ptr_str[i]); while(1) { lcd_cmd(0x0C); for(i=60;ptr_str[i];i++) { lcd_cmd(FourthLine); for(j=0;j<MaxSupportedChars && ptr_str[i+j];j++) lcd_data(ptr_str[i+j]); for(j=j;j<MaxSupportedChars;j++) lcd_data(BlankSpace); delay_ms(125); } lcd_cmd(0x0E); } } else { while((*ptr_str)!= 0) LCD_DisplayChar(*ptr_str++); } } void LCD_GoToNextLine(void) { LcdTrackLineNum++; LcdTrackCursorPos = 0x00; switch(LcdTrackLineNum) { case 1: lcd_cmd(SecondLine); break; case 2: lcd_cmd(ThirdLine); break; case 3: lcd_cmd(FourthLine); break; } } void LCD_DisplayChar(char LcdData) { if((LcdTrackCursorPos >= MaxSupportedChars-1 ) || (LcdData=='n')) { /* If the cursor has reached to end of line on page1 OR NewLine command is issued Then Move the cursor to next line */ LCD_GoToNextLine(); } if(LcdData != 'n') /* Display the character if its not newLine Char */ { lcd_data(LcdData); /* Display the data and keep track of cursor */ LcdTrackCursorPos++; } }