1. Overview of LED dot matrix display principle
Figure 1-1 shows the internal equivalent circuit diagram of an 8X8 LED dot matrix monochrome line common Yang module. For red LED, its working forward voltage is about 1.8V, its continuous working forward current is generally about 10mA, and the peak current can be greater. As shown in the figure below, when a row line is high and a column line is low, the intersection points of its rows and columns are lit. When a row line is low, regardless of the column line, all the points of the corresponding row are dark. LED dot matrix screen display is to scan line by line through a certain frequency, and the data end continuously inputs data for display. As long as the scanning frequency is high enough, due to the visual residual effect of human eyes, you can see the complete text or pattern information. Generally, there are 4, 8 and 16 line scanning modes. The fewer scanning lines, the better the display brightness of the dot matrix, but the corresponding hardware data register needs more.
Figure 1-1 internal schematic diagram of dot matrix
2. Hardware design
The IO port of the microcontroller cannot flow excessive current. When the LED is lit, there is about 10ms of current. Therefore, the LED dot matrix pin should not be directly connected to the IO port of the MCU, but should first pass through a buffer 74hc573. The IO port of single chip microcomputer only needs a small current to control 74hc573, which can indirectly control a row (or a column) of LED dot matrix, and the output of 74hc573 can also load about 10ms of current. Set the LED driving current at each point as id = 15mA, which has good lighting degree and certain margin. Even if the power output voltage is high, the LED will not be burned, and the current limiting resistance value will not be changed
VCC is powered by 5V, VCE is the saturation voltage between triode C and E, which is estimated to be 0.2V, Vol is 74hc573, and the output low level voltage is different for different perfusion currents, which is estimated to be 0.2V. For details, see the specification. VLED is the red light driving voltage, which is estimated to be 1.7V. According to the above formula, the current limiting resistance can be calculated as R = 200R.
The LED dot matrix screen needs to receive scanning signals one by one and scan to the corresponding column (or row). The data of the corresponding column (or row) is valid, that is, the information of this column (or row) is displayed. Generally, it is necessary to use a special decoder to generate the scanning signal, such as the three wire eight wire decoder 74HC138. In this way, the hardware can ensure that only one column (or row) is scanning at any time, and the occupation of IO port of microcontroller can be reduced. The 51 development board on the market basically does not use a decoder for the design of LED dot matrix screen. It directly uses MCU IO to generate scanning signal. For compatible software, the author does not add a decoder here. The software ensures that the IO port generates corresponding scanning signal.
When the LED points of a column (or row) are on, the current of about 15max8 = 90mA flows through the common end of this column (or row). The IO port of the microcontroller cannot directly drive this current, so it needs to be driven by a triode. Because the low-level perfusion current of 51 single chip microcomputer is large, it is suitable to use PNP triode as the drive. When the base current of the triode is set to 2mA, the triode can be saturated, and the maximum driving current is much greater than 90mA. Base bias resistance
VCC is powered by 5V, veb is the conduction voltage between triode E and B of 0.7V, Vol is the low level voltage output by IO port of single chip microcomputer, which can be estimated as 0.2V according to the specification, so RB = 2K.
Figure 2-1 schematic diagram of 8x8 common cathode LED dot matrix
3. Driver implementation
The LED dot matrix data port is connected to port P0, and the scanning selection line is connected to bits 0 7 of port P2. For dynamic scanning, there is a scanning frequency. The lower limit of LED screen scanning frequency is 50Hz. If it is lower than a certain scanning frequency, the display will flash. If the frequency is too high, the brightness is poor and CPU resources are occupied. Generally, the scanning time of the whole screen is about 10ms (i.e. the scanning frequency is 100Hz). We adopt the 8-line scanning mode. The lighting time of each line is 1.5ms and the scanning time is 12ms. To ensure this refresh rate, the dot matrix screen is usually refreshed periodically through a timer.
The display screen often involves complex operations such as drawing points, lines and drawings. Changing the information of the screen only needs to process the data in the video memory. Therefore, for the display screen, it is necessary to open up a memory space for video memory. Each point of 8x8 dot matrix can be represented by 1 bit, 1 byte per line and 8 bytes of video memory. Because there are too few pixels on the dot matrix screen, it is not necessary to realize complex operations such as line drawing and drawing. Here, the author only realizes the code implementation of dot matrix screen drawing and text moving up, down, left and right.
The matrix. C file of dot matrix screen dynamic display function module is as follows:
//Each LED point needs 1 bit to save, and 8x8 dot matrix needs 8 bytes of video memory
//The external module obtains the memory location of video memory through this function for processing
//Dot matrix refresh ensures that the refresh is called in a certain cycle
Static unsigned char select = 0; / / record the scanned selection line
//Column data output to lattice data port
//The scanning signal is output to the dot matrix scanning selection port
Select ; / / enter the next line of scanning
Select = 0; / / all lines have been scanned. Return to the first line and start scanning again
//LED dot matrix screen dotting function to light and turn off the (x, y) position, and the state is reversed
If (x "7" | Y "7) / / ensure that the position is within the dot matrix screen area
Case set: / / (x, y) position is set, and the lamp is off
The case clear: / / (x, y) position is cleared and the light is on
The case negate: / / (x, y) position is reversed, and the lamp state changes
//The LED dot matrix screen is clear, the display memory corresponds to the position of 1, the light is off, and the corresponding light of 0 is on
unsigned char i;
//Lattice translation, translation 1 in four directions up, down, left and right, and the translation vacancy position is filled with data filling
unsigned char i;
Switch (direction) / / judge the direction of balance
Case move_up: / / shift 1 upward, shift the 7th bit to the 6th bit of each column of data, and so on
Case move_down: / / shift down by 1, shift the 0 of each column of data to the 1, and so on
Case move_left: / / shift 1 to the left, move the data in the right column to the current column, and so on
Case move_right: / / shift 1 to the right, move the data in the left column to the current column, and so on
We implement the macro definition and interface access macro implementation of the module in the dot matrix screen module header file matrix. H, so that it is convenient to transplant and modify the interface configuration. The module header file also leads to the interface function of the module. For example, matrixscan() is the dot matrix screen refresh function, which needs to be called periodically to refresh the dot matrix screen display. The contents of the dot matrix screen dynamic display function module file matrix. H are as follows:
#Define set 0x1 / / set to 1
#Define clear 0x2 / / clear 0
#Define negate 0x3 / / reverse operation
#Definemove_up 0x1 / / PAN up 1
#Definemove_down 0x2 / / pan down 1
#Definemove_left 0x3 / / PAN left 1
#Definemove_right 0x4 / / PAN right 1
//Output column data to port P0
//The P2 port outputs the scanning selection line of the corresponding column, and the low is valid
The external application calls the lattice screen driving function by introducing the lattice screen module header file matrix. H. The simple test call (random translation of heart shape in the lattice screen) is realized as follows:
//Cardioid coordinate data