Simple “video player” based on 51 single chip microcomputer

Time:2022-5-27

introduce

This paper introduces the use of oled12864 (ssd1306) to play video and buzzer to play music on 51 single chip microcomputer.

(because it’s GIF, it looks like it’s stuck, but there won’t be stuck. See the video link at the end of the article for the actual effect)

The final effect is as follows:

Effect of playing bad apple:

Effect of playing digital baby:

 

The main components used are as follows:

  • Domestic 51 single chip microcomputer: stc15f2k60s2

  • Display screen: ssd164, resolution: 306 * OLED

  • Passive buzzer, 8550 triode, etc

The schematic diagram is as follows:

Specific scheme

Because the video file is relatively large (MB level), and the flash of 51 single chip microcomputer is generally small (KB level), it is obviously impossible to store the video file directly in the single chip microcomputer. The video file can be stored in the SD card, and then the MCU can read the content in the SD card; Or the video file is directly stored on the computer, and then the computer sends the video data to the single chip microcomputer in real time through the serial port, and the single chip microcomputer displays the video picture in real time. The text adopts the latter scheme.

Oled12864 drawing

When we buy oled12864 (ssd1306) display screen, the general seller will provide the sample code of 51 single chip microcomputer, or we can find a lot of relevant codes on the Internet.

When using these codes to draw on the whole screen, it is found that the refresh rate is relatively low. Under the condition of 11.0592m clock frequency, the measured speed is only about 8.6fps. The test method is as follows:

void main() 
{ 
    for(;;) 
    { 
        p27 = ~p27; 
        oled_drawbmp(pic); 
    } 
}

oled_ Drawbmp is the drawing function provided by the seller. The screen is refreshed once every time, and the p27 IO port is flipped once. Use the logic analyzer to test the level change of p27 as follows. It can be seen that the frequency is about 4.3hz, and the refresh rate of the screen is about 8.6hz (FPS).

Because our purpose is to use MCU to play video on this display screen, and the frame rate of general video needs to be greater than 25FPS. If the frame rate is too low, there will be a feeling of jamming. Obviously, the 8.6hz refresh rate of the screen mentioned above is relatively low, so we need to do some optimization.

One of the more intuitive and easy optimization methods is to increase the clock frequency from 11.0592m to 24m or 27m.

The second optimization method is to optimize the drawing function.

Let’s take a look at the code of IIC start signal and end signal:

void iic_ Start() / / start IIC

It can be seen that there are some delay_5us. In fact, this is not necessary. Without this delay, iic can also communicate normally. Therefore, removing this delay can speed up the refresh rate of the display screen.

Secondly, let’s take a look at the functions provided by the seller in the drawing part:

/***********Function Description: display BMP picture 128 × 64 starting point coordinates (x, y)*****************/

And IIC_ The implementation of writedata is as follows:

void iic_ Writedata (unsigned char iic_data) / / write data

It can be seen that every time the image data BMP [J] is written, there will be an IIC start and end action, and two control commands (0x78, 0x40) will be sent first. In fact, this is not necessary. The optimized function is as follows:

//Quickly draw images

It can be seen that the above functions reduce the start IIC, end IIC and write control commands (0x78, 0x40). Using the same test method as before, after the above optimization, the final screen refresh rate is as follows:

The above result is that using 27m clock frequency and the optimization mentioned above, we can see that the final screen refresh rate is about 34.5 * 2 = 69hz (FPS), which has met the screen refresh rate we need to play video.

In addition, it can be further optimized to make the screen refresh rate reach more than 100fps. The test results are as follows. Sell here. If you are interested, you can go to station B and have a look. The video address is:51 single chip microcomputer playing video – principle introduction

Transcoding video into hexadecimal format

MCU plays video, we need to transcode the video into hexadecimal data that can be read by MCU.

First, we need to decompose the video into frame by frame images, and then we can use the following modeling software to obtain the hexadecimal font of the image.

However, due to the large number of video frames, it is obviously a tiring job for us to manually use the mold taking software to obtain the font frame by frame. Therefore, we can write a Python code to generate the hexadecimal data of each frame in batch. Python code is as follows:

import cv2

The code is relatively simple. Opencv library will be called to read the pixel values corresponding to the video and video picture.

Serial port transmission

After the video data is ready, we need to send the video data to the single chip microcomputer through the serial port. After the single chip microcomputer receives a complete frame of data (a frame of picture), it can start to display the picture. We can also write a Python code to send video data to the MCU. The code is as follows:

Import serial # import serial port related libraries

The code is relatively simple, in which the serial library related to the serial port will be called, and then one frame of data will be sent each time until all sending is completed.

Summary

Finally, summarize the general process of playing video in 51 single chip microcomputer:

  1. The video is decoded into frames of images, and then transcoded into hexadecimal format that can be displayed on the display screen (this step can be completed in advance)

  2. The computer sends the video data in hexadecimal format to the single chip microcomputer through the serial port

  3. After the single chip microcomputer receives a complete frame of data (an image), the display screen starts to display the picture

  4. At the same time, the buzzer plays music (optional)

Note:

If you want to know more about the details, you can go to station B to see the video I uploaded. The introduction is more detailed, and there is also the final effect demonstration.

51 single chip microcomputer playing video – principle introduction

Super simple video player based on 51 single chip microcomputer

Recommended Today

Network counting experiment I Division VLAN

Experiment 1  vlanCreation and division of 1、 Experiment purpose: 1. Understand the working principle of VLAN; 2. Learn the method of dividing VLANs based on ports; 3. Understand the communication between the same VLANs across switches; 4. Further learn the configuration commands of switch ports. 2、 Experimental principle: VLAN (virtual local area network), that is, […]