1. Simple display test program – to help understand

Modulo characters using PCtoLCD

Any screen is made up of pixels, so displaying the English character ‘A’ works like this:

To display a character, determine the height and width of the character:

  • The width of a pixel is half the height;
  • The height is 16/24/32 pixels, called the font size (16 pixels in the image above)

Take mold software using PCtoLCD2002 perfect version, attached download link.

The Settings in the mold taking software are as follows:

  • Generated data: C51 format:
  • Dot matrix format: negative code (negative code is 1 display, 0 do not display);
  • Take mold way: line by line
  • Mold taking direction: along
  • Other Settings: default



Here I’m using a screen resolution of 240240, so we choose to display a 240120 character ‘A’, set the size in the mold software:

There are many generated data, click Save to file:

Change the file name to font_a. h and then change the file format to the following for subsequent use:

const char CH_A[] = 
{
    // Generated character data
};
Copy the code

Write programs to display characters

This array has 225 rows of 16 bytes of data per row. Each byte corresponds to whether the 8 pixels are displayed or not. The program then determines whether to write color to the pixels based on this array.

The simple test procedure is as follows:

void LCD_ShowChar(uint16_t x, uint16_t y, char ch, uint16_t back_color, uint16_t font_color, 	uint8_t font_size)
{
	int i = 0, j = 0;
	uint8_t temp = 0;
	
	LCD_Address_Set(x, y, x + font_size/2 - 1, y + font_size - 1);//(x,y,x+8-1,y+16-1)
	
	for(i = 0; i <225*16; i++)
	{
		temp = CH_A[i];
		for(j = 0; j < 8; j++) {// Check whether the highest bit of temp is 1
			if(temp & 0x80)
			{
				// Display the dot with the highest bit of 1 and set the color to font color
				LCD_Write_2Byte(font_color);
			}
			else
			{
				// The highest bit is 0, the point is not displayed, set the point as the background color
				LCD_Write_2Byte(back_color);
			}
			temp <<= 1; }}}Copy the code

Called in mian.c as follows:

int main(void)
{
	HAL_Init();
	SystemClock_Config();
	LCD_Init();
	
	LCD_ShowChar(60 ,0.'A',BLACK,YELLOW,240);
	
	while (1);
}
Copy the code

According to the results

2. Use the English font file “font. H”

The font. H content

When applications need to display only a few characters, you can use the modulus, also very save memory, but when we need to use the uncertainty in the procedure or when a large number of characters, the characters one by one by means of modulus obviously feasible, need to use a has common characters commonly used size pieces to complete the font file, namely the font. J h, See my Github for more details.

Arbitrary character display function based on font. H

First include the character file:

#include "font.h"
Copy the code

Then improve on the function you wrote earlier:

/** * @brief Displays an ASCII character * @param x,y displays the starting coordinate * @param ch Displays the character to be displayed * @param size Font size (16/24/32 font supported) * @return None * @note requires font. H library file support */
void LCD_ShowChar(uint16_t x, uint16_t y, char ch, uint16_t back_color, uint16_t font_color, uint8_t font_size)
{
	int i = 0, j = 0;
	uint8_t temp = 0;
	uint8_t size = 0;
	uint8_t t = 0;
	
	/* Check to see if the boundary is crossed */
	 if((x > (LCD_Width - font_size / 2)) || (y > (LCD_Height - font_size)))	
		 return;
	
	/* Set the memory operation area according to the character size */
	LCD_Address_Set(x, y, x + font_size/2 - 1, y + font_size - 1);
	 
	 /* Computes the offset of a character in the font */
	 ch = ch - ' ';
	 
	 /* Display 16 /32 font */
	 if((font_size == 16) || (font_size == 32)) {/* Count the number of bytes in the bitmap of a font character */
 			size = (font_size / 8 + ((font_size % 8)?1 : 0)) * (font_size / 2);

			for(i = 0; i < size; i++)
			{
					if(font_size == 16)
							temp = asc2_1608[ch][i];	// Call the 1608 font
					else if(font_size == 32)
							temp = asc2_3216[ch][i];	// Call the 3216 font
					else 
							return;			// There is no font library

					for(j = 0; j < 8; j++)
					{
							if(temp & 0x80)
								LCD_Write_2Byte(font_color);
							else 
								LCD_Write_2Byte(back_color);

							temp <<= 1; }}}/* Displays a 12-point font */
	 else if(font_size == 12)
	 {
		  /* Count the number of bytes in the bitmap of a font character */
 			size = (font_size / 8 + ((font_size % 8)?1 : 0)) * (font_size / 2);

			for(i = 0; i < size; i++)
			{
				  temp = asc2_1206[ch][i];

					for(j = 0; j < 6; j++)
					{
							if(temp & 0x80)
								LCD_Write_2Byte(font_color);
							else 
								LCD_Write_2Byte(back_color);

							temp <<= 1; }}}/* Displays a 24-point font */
	 else if(font_size == 24)
	 {
		  /* Count the number of bytes in the bitmap of a font character */
 			size = (font_size * 16) / 8;

			for(i = 0; i < size; i++)
			{
				  temp = asc2_2412[ch][i];
					if(i % 2= =0)
							t = 8;
					else
							t = 4;
					for(j = 0; j < t; j++)
					{
							if(temp & 0x80)
								LCD_Write_2Byte(font_color);
							else 
								LCD_Write_2Byte(back_color);

							temp <<= 1; }}}/* Other fonts */
	 else
		 return;
}
Copy the code

Test the display of four fonts

int main(void)
{
	HAL_Init();
	SystemClock_Config();
	LCD_Init();
	
	LCD_ShowChar(0.0.'A',BLACK,BLUE,12);
	LCD_ShowChar(6.12.'B',BLACK,YELLOW,16);
	LCD_ShowChar(14.28.'C',BLACK,GREEN,24);
	LCD_ShowChar(26.52.'D',BLACK,PINK,32);
	
	while (1);
}
Copy the code

The test results

3. String display function

/** * @brief Displays an ASCII character string * @param x,y displays the starting coordinate * @param STR Displays the character string * @param size Font size (16/24/32 font supported) * @return None * @note 1. 2. Do not display characters that exceed the specified width */
void LCD_ShowCharStr(uint16_t x, uint16_t y, uint8_t max_width, char* str, uint16_t back_color, uint16_t font_color, uint8_t font_size)
{

	max_width += x;
	
	while((*str <= '~') && (*str >= ' '))	// Check for illegal characters
	{
			if(x >= max_width)
			{
					// X direction out of bounds, end
					break;
			}
			
			LCD_ShowChar(x,y,*str,back_color, font_color,font_size);
			x += font_size / 2; str++; }}Copy the code

Test code:

int main(void)
{
	HAL_Init();
	SystemClock_Config();
	LCD_Init();
	
	LCD_ShowChar(0.0.'A',BLACK,BLUE,12);
	LCD_ShowChar(6.12.'B',BLACK,YELLOW,16);
	LCD_ShowChar(14.28.'C',BLACK,GREEN,24);
	LCD_ShowChar(26.52.'D',BLACK,PINK,32);

	LCD_ShowCharStr(0.240- 32.240."Mculover666",BLACK,BLUE,32);
	
	while (1);
}
Copy the code

Test results: