LCD Display with 16F877a

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

LCD Display with 16F877a

Postby yuanzai » Sun Nov 09, 2014 9:13 pm

Sorry, I am still a beginner in C programming but I have some experience in C++ programming. I have encountered this code shown below and I have no idea how it works. How does it convert the values to BCD values and why is it necessary to be shown on the LCD display? Thank you for everyone who is helping.....

unsigned int ui_decimal[5] ={ 0 };
//extract 5 single digit from ui_number
ui_decimal[4] = ui_number/10000; // obtain the largest single digit, digit4
ui_decimal[3] = ui_number%10000; // obtain the remainder
ui_decimal[2] = ui_decimal[3]%1000;
ui_decimal[3] = ui_decimal[3]/1000; // obtain the 2nd largest single digit, digit3
ui_decimal[1] = ui_decimal[2]%100;
ui_decimal[2] = ui_decimal[2]/100; // obtain the 3rd largest single digit, digit2
ui_decimal[0] = ui_decimal[1]%10; // obtain the smallest single digit, digit0
ui_decimal[1] = ui_decimal[1]/10; // obtain the 4th largest single digit, digit1

if (uc_digit > 5) uc_digit = 5; // limit to 5 digits only
for( ; uc_digit > 0; uc_digit--)
{
lcd_putchar(ui_decimal[uc_digit - 1] + 0x30);
}
}
yuanzai
Greenhorn
 
Posts: 3
Joined: Thu Oct 23, 2014 10:34 pm

Re: LCD Display with 16F877a

Postby yonghui » Sun Nov 09, 2014 11:46 pm

those code are simple to convert a value to BCD for display number with fixed number of characters. for exmple if u want to display number 123 (one hundred twenty three).

to get the most significant number i.e. 1 : you divide the number 123 with 100 to get 1.
to get the 2nd digit u first get the remainder of 123 with divide by 100, so 123%100 =23.
then get the 2nd digit by dividing it with 10. you get 23/10 = 2.
then get the remainder and the least significant digit i.e. 23% 10 =3.

so now you can display 1 as the first digit, 2 as second digit and 3 as 3rd digit.

similar every numbe in range of hundred.
for example 234
234/100=2
234%100=34
34/10=3
34%10=4
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: LCD Display with 16F877a

Postby yuanzai » Mon Nov 10, 2014 8:34 am

So does it mean that the numbers are kept in an array form to be displayed on the LCD display? What abt the if statements at the bottom? Thankyou for ur help.
yuanzai
Greenhorn
 
Posts: 3
Joined: Thu Oct 23, 2014 10:34 pm

Re: LCD Display with 16F877a

Postby ober » Mon Nov 10, 2014 9:25 am

Yes, the number in ASCII format is store in array of ui_decimal and it will be displayed on to LCD a digit by digit.
if (uc_digit > 5) uc_digit = 5; // limit to 5 digits only
for( ; uc_digit > 0; uc_digit--)
{
lcd_putchar(ui_decimal[uc_digit - 1] + 0x30);
}
}


As stated at the end of the code, the comment: limit to 5 digits only.

the if statement checks if code that call this function wanted to display more than 5 digits and limit it to 5 digit only.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: LCD Display with 16F877a

Postby yuanzai » Mon Nov 10, 2014 10:41 am

The 5 digits is only for integer or it will show the numbers in decimal? For instance: will the number be shown as 123.45 or 123.4?
And what the +0x30 at the if statement? It is an address?
yuanzai
Greenhorn
 
Posts: 3
Joined: Thu Oct 23, 2014 10:34 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 3 guests

cron