Based on 51 single chip microcomputer 12864 LCD display digital function

Publisher:skyshoucangLatest update time:2015-10-28 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I often check out some technical articles, and I find that many people write them in the form of blogs, saying that they are beneficial to others and themselves. I think that makes sense.
Looking back, I thought that I have applied for Sina Weibo for many years, but I rarely post blogs. So today I will post my first technical blog.
I hope I can develop this habit.
 
Well, it took me two hours tonight to write a simple function, probably less than 40 lines. But I did encounter some problems, but I finally solved them.
The function is to use the 12864 LCD to display numbers of any digits. Why do I write this function? Because I often use some interface functions in the process of using 12864, which is very convenient. For example, write_char(); write_string(); write character and write string functions.
But if I want to write a number, I can't do it. I can split the digits one by one and use the character display function. But this is very troublesome. The most fatal point is that since the address of 12864 is a 2-byte unit (a little unprofessional here), probably for writing Chinese characters, the numbers cannot be close together, and there will be a space in the middle, which is very wasteful. So there is this function to write numbers.
void write_num(uchar x,uchar y,unsigned long int num)
{
uchar i=10;
switch (y)
{
case 0: Lcd_WriteCmd(0x80+x);break;
case 1: Lcd_WriteCmd(0x90+x);break;
case 2: Lcd_WriteCmd(0x88+x);break;
case 3: Lcd_WriteCmd(0x98+x);break;
default: ;
}
while(i>=1)
{
if((uchar)(num/pow(10,i-1))!=0)
break;
i--;
}
while(i)
{
Lcd_WriteData(num/(u32)(pow(10,i-1))+'0');
num=num%(u32)(pow(10,i-1));
 
i--;
}    
}
 
First of all, it is definitely not an arbitrary number of digits. After all, the display screen is so small, and the number of digits displayed must be limited. We noticed that in C language
The unsigned long int type is very large, 32 bits, 4 bytes, and the maximum value is 4294967295.
There are ten digits here, so our parameter is of type u32, and the XY parameters are the coordinate positions displayed on 12864.
The function first determines the coordinates to be written, then determines the number of digits, and then writes them one by one starting from the high digit.
Define a variable i; initialize bit 10.
It is worth mentioning that I don’t know if other people have encountered the same problem I encountered.
When using the pow function, since the library functions are defined as double type, an error will be reported when taking the remainder. I added a forced type conversion first. It didn't work, and many numbers were displayed incorrectly. After simulation debugging, I think the problem lies in the pow function.
So, I wrote the pow function myself, and used integer data types to avoid data loss during data type conversion.
After changing it like this, it will be fine.
Write the pow function I wrote, it's very simple.
u32 pow(uchar a,uchar b)
{
u32 result=1; //   initialize to 1
for(;b>0;b--)
{
result*=a;
}
return result;
}
 
Write a calculator program in two days...
Reference address:Based on 51 single chip microcomputer 12864 LCD display digital function

Previous article:Simple calculator based on 51 single chip microcomputer 12864
Next article:Single chip microcomputer controls high power LED lights (interpretation)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号