In this article we will discussed about how to print a big number on lcd using 8051 microcontroller. Writing a string on lcd to microcontroller is an easy task but writing a big number is a bit difficult task. To write a big number on lcd we have to separate it into single digits but this process has to be done from MSB (left) to LSB (right) which is difficult. This can be done using recursion method but it won't work always due to overwriting. Thus for this we have to create our own stack and maintain it. This seems difficult. Hence we will be using a different method.
We will convert our big number into string. and then we can easily print it like a boss on lcd. Follow the code given to understand how to print a big number on lcd.
void write_number_on_lcd(unsigned int digit)
{
char count;
unsigned long int constant=1;
unsigned char array[10]={'\0','\0','\0','\0','\0'};
if(digit==0)
{
wrcmd(48,1);
return;
}
for(;digit>=constant;constant*=10)
count++;
count--;
for(;count>=0;count--)
{
array[count]=digit%10+48;
digit/=10;
}
write_to_LCD(array);
}
- We created an array on 5 characters with writing null in all places as we are considering printing of integer numbers, so they cannot be bigger than 65535.
- Along with it we have declared a long integer variable named constant which is further used to find how many digits are present in number to be printed.
- for printing 0, we have a special if case for other numbers till 65535, for loop will execute.
- we increment value of constant by the multiplication factor of 10. So it will take values 1, 10, 100, 1000, ... when digit to be printed is less than constant then we will get out of for loop and the number of time it was executed will be our count that is the number of digits in number.
- though count will be starting from 1 for one digit, we access array from 0, hence total count will be decremented by 1.
- it is a lot easier to find the MSB than LSB using %10 then /10 method. We save MSB in count numbered positing in array and perform this method until we save last digit.
- Finally the big number is converted into an array. Thus we can now print our array using our previous functions.
Simulation :
e.g. LCD interfacing on proteus |
Follow this link for understanding how to write string on lcd
Dowonload program and simulation files through this link.
No comments:
Post a Comment