[STC MCU learning] Lesson 8: LED dot matrix of MCU

[Mr. Zhu's course summary intrusion]

The first part, chapter list

1.8.1. Introduction to LED dot matrix

1.8.2. Schematic analysis

1.8.3. LED dot matrix programming practice 1

1.8.4. LED dot matrix programming practice 2

1.8.5. LED dot matrix programming practice 3

1.8.6. Font introduction

1.8.7. Display function of horizontal modulo 1

1.8.8. Display function for horizontal modulo 2

1.8.9. Display function for longitudinal modulus


The second part, chapter introduction


1.8.1. Introduction to LED dot matrix
    This section comprehensively introduces LED dot matrix from the perspectives of appearance, function, construction and principle, and analyzes the two circuit connection methods for driving LED dot matrix.
1.8.2. Schematic diagram analysis
    This section analyzes the connection mode of the LED dot matrix in the development board, and the focus is on the connection of 4 74HC595.
1.8.3.
    LED dot matrix programming practice 1 This section starts the LED dot matrix programming work, focusing on analyzing the sequence of 74HC595 and considering how to convert the sequence into code.
1.8.4. LED dot matrix programming practice 2
    This section actually write 74HC595 driver code, and try to light up the entire dot matrix screen for practical testing.
1.8.5. LED dot matrix programming practice 3
    This section further transmits the corresponding data to the dot matrix to explore the lighting law of the LED dot matrix, and pave the way for writing the text display function later.
1.8.6.
    Introduction to fonts This section introduces the concept of fonts, and explains the usage and acquisition methods of fonts.
1.8.7. Display function of horizontal modulus 1
    This section analyzes how to display text on the dot matrix screen by analyzing the modulo method of font and the single-line display function of LED dot matrix described in the previous chapter.
1.8.8. Display function for horizontal modulus 2    
    This section continues from the previous section to display text, and encapsulates the display part of the text into a function for displaying text.
1.8.9. Display function of vertical modulus
    This section uses vertical modulus to obtain fonts, and then modify the drive function to achieve text display. The purpose is to let everyone further grasp the idea of ​​writing dot matrix text display functions through comparison.

    
Part Three, Classroom Record


1.8.1. Introduction to LED dot matrix

1.8.1.1. What is LED dot
matrix ? Dot matrix display is the basis of the display behind it. This lesson is very helpful for the understanding of the display!
(1) Appearance
View source imageView source image

What is it on the board? Each dot is an LED light! The display method is very similar to the digital tube, you need to make a broken code table in advance!

4*16*16 dot matrix led image result

(2) Function
Display text, number, display icon
(3) Schematic diagram of internal structure. \02. Microcontroller complete learning series of courses\Course supporting ARM3.0 development board CD material\Step 3 51 routines\9, LED dot matrix\datasheet-related chip manual
16*16 LED monochrome light!
The advantage of the dot matrix is ​​that 16 (J34+J28) IO ports independently control 8*8 LEDs on and off
(4) How does the LED dot matrix work?
Lighting: The vertical and horizontal combination of
8*8 LED dot matrix can be connected in parallel/series Into a larger LED dot matrix!


1.8.1.2. How to drive LED dot matrix
(1) Direct drive of MCU port. To drive an 8*8 dot matrix requires 2 IO ports ( 16 IO ports ), and to drive a 16*16 dot matrix requires 4 IO ports ( 32 IO ports ). It can be used, but the IO port is used too much!

(2) Use serial and shift latch drive. ( Common way ) Look at the schematic diagram!

(1) 串转并
SER 10101100 同一个引脚不同时间送进来,串行输入

QA 1
QB 0
QC 1         不同引脚同一时间送出去,并行输出
QD 0
QE 1
QF 1
QG 0
QH 0

从SER口一个一个走进去,然后通过Qx同时走出去!
(2) 移位
串行输入移进锁存器
(3) 锁存(Latch)
等到SER依次输入,等到输入完成,一瞬间锁存在锁存器

锁存之前,QA-QF的值是上一次锁存的结果!且不会改变!SER输入的二进制可以改变!
锁存之后,锁存器内的内容就会更新!

To drive a 16*16 dot matrix, only 4 74HC595 + 3 IO ports (data port, SCLK, RCLK) are needed to replace 32 IO ports, which is very cost-effective! . SCLK and RCLK are clocks! See 1.8.2 for specific implementation as shown in the figure

J24 controls four 74HC595 chips, and then the four chips together control the on and off of the LED dot matrix!

1.8.2. Schematic analysis

1.8.2.1. POS is Positive and NEG is Negative.

(1) POS1-16 and NEG1-16 are respectively connected to the parallel output terminal of the shift latch to
see the picture analysis


 If the LED dot matrix is ​​on, it is necessary to control the input of both POS and NEG! A, B of 74HC595 connect to POS, C, D connect to NEG

1.8.2.2 Connection analysis of 74HC595
(1) POS: J27 of chip A is connected to J28 of LED dot matrix, J32 of chip B is connected to J34 of LED dot matrix   
(2) NEG: QA-QH of chips C and D It has been connected to the negative pole of the LED dot matrix through the circuit, no wiring is required!
(3) The QH' serial output port is connected to the serial input SER of the next 74HC595 (the serial sequence is in accordance with ABCD) and no wiring is required.
(4) SER serial input connection: the first 595 SER is connected to P0.4 through the DuPont line, Each subsequent SER is connected to the previous QH' ( it has been connected internally ). This constitutes the ABCD 4 595 chips in series in sequence. Therefore, the serial data of the entire 4 74HC595 will come out from P0.4 when programming in the future.
(4) SCLK (SRCLK) is connected to P0.5
(5) RCLK is connected to P0.6
Summary:
(1) SCLK and RCLK are connected in the same way, both are connected to P0.5 and P0.6.
(2) There are 3 IO ports involved: SE: P0.4, SC: P0.5, RC: P0.6
(3) Key points of external wiring: 2 8pin Dupont lines J27-J28, J32-J34


1.8.3. LED dot matrix programming practice 1

This lesson will study how to make the LED dot matrix light up.
SER is responsible for data access, SRCLL and RCLK are responsible for timing!

1.8.3.1 Timing analysis of 74HC595
(1) The communication between chip and chip is carried out according to a certain timing.
(2) Timing: the sequence of level changes on the communication pins between the chip (STC89C) and the chip (74HC595) based on the time axis.
(3) The timing is defined in advance by both parties . The sender of the communication must send meaningful information in accordance with the timing. The receiver of the communication receives and analyzes the level change information from the sender in accordance with the timing, and then knows the sender. I'm going to send something.
(4) When we are programming: the sender is a microcontroller, and the receiver is 74HC595. Because the 595 chip itself cannot be programmed, his timing chip has been set at the factory. Therefore, our microcontroller must accommodate the 595 chip and send him information according to the 595 chip's timing.
(5) So we must first figure out the timing rules of 74HC595 . According to the timing description (timing diagram) on the 74HC595 chip manual, you can understand the timing rules of the 595 chip, and then express it in a programming language to be a program. (You can refer to other people's code! )

Look at the manual! Reference link: 74HC595 Chinese data    /   74HC595 Chinese data parameter.pdf (recommended)
(6) The timing key of 74HC595 is: SCLK and RCLK.
SCLK is the shift clock
. The 595 chip will sample the SER pin once on the rising edge of each SCLK (the level of SER), Then 1 bit is input to the 595, so 8-bit binary is input after 8 cycles. 74HC595 has two outputs: 1 is QA-QH (parallel output), 2 is QH' (serial output);
RCLK is the latched clock
, and the 8-bit parallel output signal of QA-QH is latched once on the rising edge of RCLK Update.

(7) The key to understanding the communication timing of 74HC595 chip : SER carries on the serial input of data, SCLK provides the shift clock, RCLK provides the latch clock.

Truth table:

Timing diagram:


Every time a rising edge of SRCLK is reached, a data is input from SER. When the rising edge of the next SRCLK is reached, the data is output to the next chip through QH'.
When it reaches a rising edge of RCLK, put the data in QA-QH.    Now I don’t understand it, it’s okay, just wait to see the two programs to understand!

1.8.4. LED dot matrix programming practice 2

1.8.4.1. The
general idea of how to program the chip : 1. d1, d2, d3, and d4 are respectively input to the four 595 chips of DCBA, and then control the pin input value of NEG1-16 and POS1-16, and then control the LED point matrix display
                  2. how do you enter it? Obtain each bit value of dx (x=1, 2, 3, 4) in turn through SER, and then uniformly latch to the QA-QH terminals of the four chips! The specific correspondence is shown in Figure


1.8.4.2, full-screen lighting test

#include<reg52.h>

sbit SER = P0^4;	//SER串行输入端
sbit SC = P0^5;  //SRCLK 移位时钟
sbit RC = P0^6;	 //RCLK	锁存时钟

void main()
{
	unsigned char i = 0;
	unsigned char d1,d2,d3,d4; //要个给4个595并行输出端输出的值
	SC = 0;
	RC = 0;
	
	d1 = 0;
	d2 = 0;
	d3 = 0xff;
	d4 = 0xff;
	
	for(i = 0;i<8;i++)
	{ 
		SER = d1 >> 7; 	//将d1的最高bit给SER
		SC = 0;
		SC = 1;				//2步制造了一个SCLK的上升沿。
        d1 <<= 1;
	}
	//至此已经在8个SCLK的上升沿全部发出去了
	//但是还没有进行锁存,所以QA-QH还没东西
	for(i = 0;i<8;i++)
	{ 
		SER = d2 >> (7-i); 	//将d1的最高bit给SER
		SC = 0;
		SC = 1;				//2步制造了一个SCLK的上升沿。
        d2 <<= 1;
	}
	//至此已经把d1和d2都发出去了,并且d2已经把d1挤到下一个595芯片了
	//但是还没有进行锁存,所以QA-QH还没东西
	for(i = 0;i<8;i++)
	{ 
		SER = d3 >> (7-i); 	//将d1的最高bit给SER
		SC = 0;
		SC = 1;				//2步制造了一个SCLK的上升沿。
        d3 <<= 1;
	}
	//至此已经把d1、d2、d3都发出去了,d1在C、d2在B、d3在A
	//但是还没有进行锁存,所以QA-QH还没东西
	for(i = 0;i<8;i++)
	{ 
		SER = d4 >> (7-i); 	//将d1的最高bit给SER
		SC = 0;
		SC = 1;				//2步制造了一个SCLK的上升沿。
        d4 <<= 1;
	}
	//至此已经把d1、d2、d3、d4都发出去了,d1在D、d2在C、d3在B、d4在A
	//但是还没有进行锁存,所以QA-QH还没东西
	
	//截止这里,4个字节的数据d1、d2、d3、d4已经顺着74HC595的SER->QH'的串行输入
	//串行输出路线,已经爬满了4个74HC595(最先送出的在D)
	//但是目前4个595的QA-QH都还没有输出呢,点阵自然不会亮
	
	//现在进行一次锁存,4个595芯片同时进行锁存,各自锁存住自己的数据
	RC = 0;
	RC = 1;
	//这时候会完成锁存,d1-d4会分别进入ABCD的QA-QH端,进而决定LED点阵的状态
	//d3-POS1-8,d4-POS9-16,d1-NEG1-8,d2-NEG9-16
	
}

Summary:
(1) When writing hardware control code, timing understanding is the key . As long as the timing is understood correctly and the code is written in accordance with the timing requirements, there is no problem.
(2) As long as the code of the timing operation part is written and correct, the next part will be no problem, it is very simple, because it is rigid and unchanging.
Course requirements:
1. generation of
code improvements!  
Tip: Use arrays for d1, d2, d3, and d4!

#include<reg52.h>

typedef unsigned char u8;

sbit SER = P0^4;	//SER串行输入端
sbit SC = P0^5;  //SRCLK 移位时钟
sbit RC = P0^6;	 //RCLK	锁存时钟

void main()
{
	u8 i = 0;
	u8 j = 1;
	u8  d[] = {0,0,0xff,0xff};
	SC = 0;
	RC = 0;
	
	/*d1 = 0;
	d2 = 0;
	d3 = 0xff;
	d4 = 0xff;*/
	for(j = 0;j<4;j++)
	{
		for(i = 0;i<8;i++)
		{ 
			SER = d[j] >> 7; 	//将d1的最高bit给SER
			SC = 0;
			SC = 1;				//2步制造了一个SCLK的上升沿。
                        d[j] <<= 1;
		}
	}
	RC = 0;          //制造RCLK上升沿
	RC = 1;
}

2. Package the program into a function!
Write the shift register transfer function SendData()

#include<reg52.h>

typedef unsigned char u8;

sbit SER = P0^4;	//SER串行输入端
sbit SC = P0^5;  //SRCLK 移位时钟
sbit RC = P0^6;	 //RCLK	锁存时钟


void SendData(u8 d[])
{
	u8 i = 0;
	u8 j = 0;
	SC = 0;
	RC = 0;
	for(j = 0;j<4;j++)
	{
		for(i = 0;i<8;i++)
		{ 
			SER = d[j] >> 7; 	//将d1的最高bit给SER
			SC = 0;
			SC = 1;				//2步制造了一个SCLK的上升沿。
                       d[j] <<= 1;
		}
	}

	RC = 0;
	RC = 1;
}

void main()
{
		u8 d[] = {0,0,0xff,0xff};
		SendData(d);
}

1.8.5. LED dot matrix programming practice 3

1.8.5.1. The exploration of the dot matrix lighting law (control d1-d4) has nothing to do with the function!

d1 and d2 are negative electrodes, 1 is off and 0 is on; d3 and d4 are positive, 1 is on and 0 is off, and the low bit corresponds to the minimum value of POSx/NEG, for example: the lowest bit of d1 controls NEG9
(1) Programming to light the top 8 rows
(2) Programming lights up the bottom 8 rows
(3) Programming lights up the leftmost 8 columns
(4) Programming lights up the top 1 row
(5) Programming lights up the bottom 1 row
(6) Programming lights up one in the upper left corner
(7) Programming 4
reference answers on the lighted corner

1.8.5.2. Thinking: How to display text? Please tell me! Here are the tips!
View source image
Starting from the next section, let's start displaying numbers/Chinese characters! Later, we will use special font software to extract Chinese characters!

1.8.6. Font introduction

1.8.6.1. What is a font
Open font software!

(1) Typeface:
  Record the LED dot matrix lighting information that composes the character (16*16 dot matrix has 256 dots in total. To display a specific character, some of them need to be lit while others are not lit. How to record which ones are lit and which are not Bright? Use fonts )
(2) How do fonts work?
  256 points are represented by 256 binary bits, 1 means this is on, 0 means not. 256 points are 256 binary bits, which is 256/8=32 bytes . Therefore , the font size of a 16*16 word is 32 bytes in size . Therefore, the representation of the font is 32 unsigned char data .
(2) How to obtain fonts. It is usually extracted with special font extraction software. The function of this kind of software is to automatically get the 32-byte font code corresponding to a word . Like the char code {...}
(3) above, the result of the font is not unique, it is related to the way you extract the font. (To distinguish between horizontal and vertical, top to bottom, etc.) There is no standard when extracting fonts. How to do it is right or wrong. The key is the way you extract the font and the way you use it to display the font on the dot matrix. The function must correspond.

1.8.6.2. Use of font extraction software
(1) How to use: In the
    first step , select the font type (the size of the dot matrix on the actual development board is selected); in the
    second step, select the appropriate font, font size, etc.; the
    third step Select the encoding method and the direction of modulo;
    D0->D7, vertical
    
    D0->D7, horizontal
    
    D7->D0, vertical
    
    D7->D0, horizontal The
    
    fourth step directly copy the obtained array
1.8.6.3, manual analysis of fonts Verification
(1) Manually compare the content of the font and the screen display to confirm the method of taking the model
(2) Combine the previous lessons and think about how to display it d1-d4


1.8.7. Display function 1 of horizontal modulus (D0-D7, horizontal)

1.8.7.1. Project establishment
If we display the word "Feng". Copy his code to main.c!

Everyone paste the following codes in main.c:
 

#include <reg51.h>

typedef unsigned char u8;

sbit SER  = P0^4;			// 74HC595的串行输入端
sbit SC = P0^5;			// 移位时钟
sbit RC = P0^6;			// 锁存时钟

void SendData(u8 d1, u8 d2, u8 d3, u8 d4)
{
	unsigned char i = 0;	

	SC = 0;
	RC = 0;

	for (i=0; i<8; i++)
	{	
		SER = d1 >> 7;	 		
		SC = 0;	
		SC = 1;  				
		d1 = d1 << 1;
	}

	for (i=0; i<8; i++)
	{
		SER = d2 >> 7;	 		
		SC = 0;
		SC = 1;  				
		d2 = d2 << 1;
	}

	for (i=0; i<8; i++)
	{
		SER = d3 >> 7;	 		
		SC = 0;
		SC = 1;  				
		d3 = d3 << 1;
	}

	for (i=0; i<8; i++)
	{
		SER = d4 >> 7;	 	
		SC = 0;
		SC = 1;  			
		d4 = d4 << 1;
	}

	RC = 0;
	RC = 1;
}


void main(void)
{


}

1.8.7.2. Display the second line first.
Think about it, can the number in feng[] be directly placed in SendData()? What operation is needed.

feng[] = {0,0,228,15,8,8,72,8,64,8,64,8,192,63,8,32,8,32,228,47,4,32,2,32,2,32,0,24,0,0,0,0};

For example, to display the second line, '228' corresponds to d2, and '15' corresponds to d1, and the values ​​of d1 and d2 must be converted from positive logic to negative logic!

//就变成了
SendData(~15,~228,0x00,0x02);//显示第二行

Display result: it is the second line of the word'Feng'!


1.8.7.3. Display 2 more lines to explore the pattern.
Get the 8th line and see the pattern!

	 SendData(~0, ~0, 0x00, 0x01);		// 显示第1行
	 SendData(~15, ~228, 0x00, 0x02);	// 显示第2行
	 SendData(~8, ~8, 0x00, 0x04);		// 显示第3行
	 SendData(~8, ~72, 0x00, 0x08);		// 显示第4行
	 SendData(~8, ~64, 0x00, 0x10);		// 显示第5行
	 SendData(~8, ~64, 0x00, 0x20);		// 显示第6行
	 SendData(~63, ~192, 0x00, 0x40);	// 显示第7行
	 SendData(~32, ~8, 0x00, 0x80);		// 显示第8行

1.8.7.4. Law
(1) Law 1: d1 and d2 are filled with fonts, and should be reversed when filling
(2) Law 2: d3 and d4 are used to select which row is lit, and d1 and d2 select which row in this row One column is lit.
(3) Rule 3: SendData sends 16 LED on/off information (2 bytes) at a time, so the SendData function must be called 256/16=16 times to light up the entire dot matrix.
(4) Law 4: Every time SendData is called, the changes of d1-d4 are regular, so it is hoped that SendData can be called through a loop instead of manually calling 16 times.
How can this be done? Think about it! (2min)


1.8.8. Display function for horizontal modulo 2

Realize the law.
1.8.8.1, define the row selection array

//第一种
u8 code hang[32] = 
{
0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08,
0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80,
0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00,
0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80, 0x00, 
};

//第二种
u8 code hang[] = 
{
0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
};

1.8.8.2, use for loop to display

//第一种
	
for(i = 0;i<16;i++)
{
	SendData(~feng[2*i+1],~feng[2*i],hang[2*i],hang[2*i+1]);
}

//第二种
/*
for(i = 0;i<8;i++)
{
	SendData(~feng[2*i+1],~feng[2*i],hang[0],hang[i+1]);
}
for(i = 8;i<16;i++)
{
	SendData(~feng[2*i+1],~feng[2*i],hang[i-8],hang[i+1-8]);
}
*/

There is a problem. It is okay to display one word, but multiple words are troublesome, so there must be a function to display words!
1.8.8.3. Write the dot matrix display function Display() and
try to implement it yourself


1.8.9. Display function of longitudinal modulus (D0-D7, longitudinal)

1.8.9.1, first observe and summarize the modulus law of longitudinal modulus

1.8.9.2, display the second column
1.8.9.3, display more columns to find the law
or display 8 columns to try!
1.8.9.4, write into a function and then test
 

#include <reg51.h>

typedef unsigned char u8;

sbit SER  = P0^4;			// 74HC595的串行输入端
sbit SC = P0^5;			// 移位时钟
sbit RC = P0^6;			// 锁存时钟

void SendData(u8 d1, u8 d2, u8 d3, u8 d4)
{
	unsigned char i = 0;	

	SC = 0;
	RC = 0;

	for (i=0; i<8; i++)
	{	
		SER = d1 >> 7;	 		
		SC = 0;	
		SC = 1;  				
		d1 = d1 << 1;
	}

	for (i=0; i<8; i++)
	{
		SER = d2 >> 7;	 		
		SC = 0;
		SC = 1;  				
		d2 = d2 << 1;
	}

	for (i=0; i<8; i++)
	{
		SER = d3 >> 7;	 		
		SC = 0;
		SC = 1;  				
		d3 = d3 << 1;
	}

	for (i=0; i<8; i++)
	{
		SER = d4 >> 7;	 	
		SC = 0;
		SC = 1;  			
		d4 = d4 << 1;
	}

	RC = 0;
	RC = 1;
}
void Display(u8 shumo[],u8 hang[])
{
	u8 i = 0;
	for(i = 0;i<16;i++)
	{
		SendData(~hang[2*i],~hang[2*i+1],shumo[i+16],shumo[i]);
	}
}
u8 code hang[] =
{
	0x00,0x01,0x00,0x02,0x00,0x04,0x00,0x08,
	0x00,0x10,0x00,0x20,0x00,0x40,0x00,0x80,
	0x01,0x00,0x02,0x00,0x04,0x00,0x08,0x00,
	0x10,0x00,0x20,0x00,0x40,0x00,0x80,0x00
};

u8 code feng[] = 
{
0,0,2,140,0,2,122,66,66,66,66,126,64,192,0,0,
0,24,6,1,0,2,2,2,2,2,2,34,32,31,0,0
};
void main()
{
	Display(feng,hang);
	
}

Course egg:

download link of this lesson program: LED dot matrix

This lesson is over!

Guess you like

Origin blog.csdn.net/qq_27148893/article/details/109511407