Fat16 file system learning based on stm32f103zet6 4 (managing SD card, reading pictures, Font Library)

Source: Internet
Author: User
Tags bmp image

Code used in this blog: http://download.csdn.net/detail/king_bingge/5739167

I haven't written a blog for several days, and I 've been busy with the opening Cup for the past few days. Due to some inharmonious factors, I started preparing for today's opening Cup reply yesterday. Although I don't know what the result is, however, I personally feel that there are still some innovative ideas, that is, the primary judges and teachers are dragging on to our consumption object and no longer think about it. I don't know if I can enter the provincial competition, however, we still need to continue learning. The experiment we summarized today is to read bmp and bin images from the SD card, and store the words I made in the SD card, reading the required Chinese characters from the image is a long time ago when reading bmp images and font, but it has always plagued me with reading bin images, it took some time this afternoon to finally get it out. The image is faster than the bmp block, but it is not much faster, and it is faster than 1/5 !! But I know how to read the content of the binfile.

Okay. The experiment starts ..

The preliminary knowledge is to understand the Chinese character display principle, the relationship between location codes, and the display principle of bmp images. I have read this part of knowledge for a few hours. Now I want to summarize some important points.

1. display Chinese GBK Codes

1. The storage of Chinese Characters in various files is not stored in the form of dot matrix data (otherwise, the occupied space will be too large), but stored in the form of codes, GB2312, GBK, and BIG5 are among the following types. Each Chinese Character corresponds to an internal code. After knowing the internal code, you can search for the dot matrix data of this Chinese Character in the font, then it is displayed on the LCD. We can't see this process, but the computer is going to execute it.

2. Process for displaying Chinese characters through stm32: Chinese character internal code (GBK/GB2312) ----> Search for Dot Matrix library -----> parse -------> display

So I think this should not be the content we are discussing about Chinese characters. I recommend a blog post that is easy to understand and reprinted on the Internet. You can refer to this article for more information.

Http://blog.csdn.net/king_bingge/article/details/8780202

3. How do I find the dot matrix font? Let's take a look at the following piece of code and analyze the code to reduce the difficulty of understanding.

Let's look at the code in the main program first.

Int main (void) {delay_init (72); usart+init (); spix_init (); // initialize spilcd_ili9325_init (); // The LCD Driver initializes printf ("\ r \ n (" _ date _ "-" _ time _ ") \ r \ n "); LCD _clearscreen (0xa451); LCD _show_bmp (0, 0, "/You you.bmp"); LCD _clearscreen (black); LCD _show_bin (0, 0, "/you. bin "); LCD _clearscreen (red); LCD _str_ch (20,300," experiment on reading Chinese characters from the memory card ", 0, blue); While (1 );}

The main program is a few screen-flushing functions, and then read the font function first.

4. The Code is as follows:

Void LCD _str_ch! = '\ 0') {If (x> (240-16) {/* line feed */x = 0; y + = 16 ;} if (Y> (320-16) {/* reset to zero */y = 0; X = 0;} LCD _char_ch (X, Y, STR, color, bkcolor ); STR + = 2; x + = 16 ;}}

The first is simple format processing, and then it is important that this function LCD _char_ch (X, Y, STR, color, bkcolor); be tracked in

Void LCD _char_ch; getgbkcode_from_sd (buffer, STR);/* modulus data */for (I = 0; I <16; I ++) {tmp_char = buffer [I * 2]; tmp_char = (tmp_char <8); tmp_char | = buffer [2 * I + 1]; for (j = 0; j <16; j ++) {If (tmp_char> 15-j) & 0x01 = 0x01) {LCD _disponepixel (x + J, Y + I, color );} else {LCD _disponepixel (x + J, Y + I, bkcolor );}}}}

Everyone can understand these codes. So what is the function of getgbkcode_from_sd (buffer, STR);/* modulo Data Acquisition?

Int getgbkcode_from_sd (unsigned char * pbuffer, const unsigned char * c) {unsigned char high8bit, low8bit; unsigned int Pos; high8bit = * C; /* Get high 8-Bit Data */low8bit = * (C + 1);/* Get low 8-Bit Data */Pos = (High8bit-0xb0) * 94 + Low8bit-0xa1) * 2*16; // Pos = (High8bit-0xa0) * 94 + Low8bit-0xa0) * 2*16; f_mount (0, & FS); Res = f_open (& FSRC, "0:/Chinese characters. bin ", fa_open_existing | fa_read); If (RES = fr_ OK) {f_lseek (& FSRC, POS); // pointer offset res = f_read (& FSRC, pbuffer, 32, & Br); // the font of Chinese characters in the size of 16*16 occupies 16*2 bytes f_close (& FSRC); Return 0;} else return-1 ;}

In fact, I still don't quite understand here, it is the offset calculation problem, which is depressing, but it seems that I have found a reasonable explanation on the Internet. I sincerely hope someone can help me solve this problem, this problem has also plagued me for a long time. I did not find the answer through printing information, single-step debugging, winhex observation content, and I do not know whether there is a head code or something. Let's put it aside!

This is the use of the file system to open the font stored on the SD card. The knowledge of the file system has been summarized before. The focus is to pay attention to this positioning function. I like this function. Hey hey!

This function is very simple. It is used to extract the dot matrix information from the font library to pbuffer. Note that there is a Chinese character section. Return to the previous function to print the dot matrix information.

2. the bmp image is displayed.

A special feature of bmp is that a 54-byte header information can be extracted from this header information. Of course, you can also directly read the information after 54 bytes, so that it can be displayed normally. Note that the bmp format is displayed !!!! He has an image. That is to say, when we read the content in the file in sequence, the image is displayed on the TFT, but it is pretty good! So if you need to display it properly, you have to modify it as needed!

Read a piece of code:

For (I = y; I 

Because the bitmap is 24 bits, the bytes need to be converted. That is to say, for our screen is 240*320, 240 is converted to 720, this macro is used to convert three consecutive bytes into 16-bit color data.

# Define RGB24TORGB16 (R, G, B) (unsigned short int) (R)> 3) <11) | (G)> 2) <5) | (B)> 3 )))

24 to 16 bits, simply put, it is to discard the low position.

In this way, there is no problem. The bmp format file is here. For more detailed explanations of the bmp bitmap, refer to this library. I personally think the analysis is more detailed!

Http://wenku.baidu.com/view/95fd4484b9d528ea81c779b3.html

3. The next step is to write an image that reads the binfile format. The code needs to be optimized and the Code must be optimized.

Check the code first. This is the code after I read the corresponding file !!

For (j = 0; j <300; j ++) // 300 indicates that an image contains x512 bytes of information {tmp_pdata = NULL; f_lseek (& bmp fsrc, (512 * j); f_read (& bmp fsrc, pdata, 512, & read_num); tmp_pdata = pdata; for (k = 0; k <512; k ++) printf ("% x", pdata [k]); // print MBR sector data for (I = 0; I <256; I ++) // then write it to the LCD screen, it can display 256 pixels. Each pixel has 16 bytes ({data = LD_WORD (tmp_pdata); LCD _DispOnePixel (x1, y1, data); tmp_pdata = tmp_pdata + 2; x1 ++; if (x1 = 240) // checks whether the image is written to the edge of the screen. 240x320 {y1 ++; x1 = 0; if (y1 = 320) y1 = 0 ;}}}

The difference between this and bmp files is that I use 16 bits for this binfile, so I only need to combine the corresponding high 8 bits and low 8 bits, to program a color data of the WORD type, what macro is used to implement this conversion? I borrowed a macro from the FAT file system!

# Define LD_WORD (ptr) (WORD) * (BYTE *) (ptr) + 1) <8) | (WORD) * (BYTE *) (ptr ))

Next, let's try to use FSMC + DMA to drive TFT. I don't support the SD mode in this module. This is the most annoying thing. Use it first! Remember: Remember to comment out the printed information during running; otherwise, it is really a turtle !!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.