need help on message display program

Status
Not open for further replies.

vead

Full Member level 5
Joined
Nov 27, 2011
Messages
285
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
india
Activity points
3,815
Hello
I have written this program to display the numbers on screen

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<reg51.h>
#define display_port P1      //Data pins connected to port 2 on microcontroller
sbit rs = P2^0;  //RS pin connected to pin 2 of port 3
sbit rw = P2^1;  // RW pin connected to pin 3 of port 3
sbit e =  P2^2;  //E pin connected to pin 4 of port 3
 
void main()
{
    unsigned int number[15]="012345";  .
    int l=0;
    lcd_init();
    while(number[l] != '\0')
    {
        lcd_data(number[l]);
        l++;
        msdelay(50);
    }
}
 
void msdelay(unsigned int time)  // Function for creating delay in milliseconds.
{
    unsigned i,j ;
    for(i=0;i<time;i++)    
    for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char command)  //Function to send command instruction to LCD
{
    display_port = command;
    rs= 0;
    rw=0;
    e=1;
    msdelay(1);
    e=0;
}
 
void lcd_data(unsigned char disp_data)  //Function to send display data to LCD
{
    display_port = disp_data;
    rs= 1;
    rw=0;
    e=1;
    msdelay(1);
    e=0;
}
 
 void lcd_init()    //Function to prepare the LCD  and get it ready
{
    lcd_cmd(0x38);  // for using 2 lines and 5X7 matrix of LCD
    msdelay(10);
    lcd_cmd(0x0c);  // turn display ON, cursor blinking 
    msdelay(10);
    lcd_cmd(0x01);  //clear screen
    msdelay(10);
    lcd_cmd(0x81);  // bring cursor to position 1 of line 1
    msdelay(10);
}



I don't understand this part of program. I don't understand what does program do in main function

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
void main()
{
    unsigned int number[15]="012345";  
    int l=0;
    lcd_init();
    while(number[l] != '\0')
    {
        lcd_data(number[l]);
        l++;
        msdelay(50);
    }
}



arrays are declared and initialize in the following form
Code:
unsigned int number[15]="012345";
I understand what is array
Code:
number[0] =  0
number[1]=  1
number[2] = 2
number[3] = 3
number[5] = 4
number[6] = 5
number[7]
number[8]
number[9]
number[10]
number[11]
number[12]
number[13]
number[14]
number[15]

I don't understand how does program work in main function. what is meaning of this line lcd_data(number[l]);?
 

void main() the function name
{
unsigned int number[15]="012345"; declare and initialize the array (note: could be char size instead of int)
int l=0; l is used as an index into the array
lcd_init(); configure the LCD so its ready to display things
while(number[l] != '\0') repeat the following until the end of string (end of array) is found
{
lcd_data(number[l]); display the array value indexed by l
l++; increment the index so it points to the next value in the array
msdelay(50); just a delay, presumably to allow the LCD time to display the character
}
}

Note that there is a serious flaw in the code. It assumes the final value in the array is zero. In 'C' a string is stored with a zero terminator at the end but because you load the values 'manually' in the array it may not be there and the LCD could try to continue displaying garbage until it finds a zero from somewhere else or 'l' overflows back to zero.
You can fix that in one of two ways, either make the last value in your array a zero (/0 rather than character zero) or you can declare the array as a character string instead of unsigned integers.

Brian.
 
Reactions: vead

    vead

    Points: 2
    Helpful Answer Positive Rating
I think vead is trying to make the following (corrected) statement:

 
Last edited:

I am confused on only two statement
1. while(number[l] != '\0')
2. lcd_data(number[l]);

what is number[l]) here number is name of variable what is [l]) ?
 

1. while(number[l] != '\0')

This is exactly the same as:

while(number[l] != 0) // loop through ASCII characters until terminator of zero is found

since '\0' is just a fancy way of indicating the number zero, but in ASCII character style.

2. lcd_data(number[l]);

this calls function lcd_data with a byte of data as the parameter given to the function.

number[l] is a byte of data (or an int of data if we take your original code). it fetches 0x30 on the first loop iteration where l is zero (because you initialised it to zero earlier), since number[0] contains 0x30
Since l is incremented each loop, successive ASCII characters are retrieved from array number[] until a zero is found
 
Reactions: vead

    vead

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…