If you don’t want to miss my updates, remember to check the public account in the upper right corner and set it as a star. Send a star to me!



Here are two photos from the game:
System Hardware:The system consists of the ZhiZhe development board and a 4.2-inch UART HMI serial screen. It could actually be done with the smallest core board, as the microcontroller peripherals only use the serial port. However, since a development board is required, it feels a bit wasteful.
Software Part:The software part is the core of this project.
Initially, I referenced some Minesweeper code online. There are many differences on the microcontroller, but the general idea is the same. The main challenge is the communication protocol with the serial screen. Here’s a simple piece of communication code.
Specific functionalities completed:
1. Basic Minesweeper functionality
2. When all surrounding mines are marked, open the remaining unmarked squares
// Serial receive interrupt void USART1_IRQHandler(void) { if(huart1.Instance->SR&(1<<5)) // If data is received { buf=huart1.Instance->DR; // Receiving selected square serial communication protocol if(id_flag==1) { if(buf==0xff) { id_flag=0; count=0; select_id=buff[0]; select_flag=1; } else { buff[count++]=buf; } } if(buf==0xaa) { id_flag=1; } } // Receiving selected square serial communication protocol // Opening a safe square serial communication protocol if(buf==0xbb) { safe_flag=1; } // Opening a safe square serial communication protocol // Restarting serial communication protocol if(buf==0xcc) { refreshall_flag=1; } // Restarting safe square serial communication protocol // Marking bomb serial communication protocol if(buf==0xdd) { zhadan_flag=~zhadan_flag; // Clicking once on the screen marks the bomb, clicking again cancels the marking } // Marking bomb safe square serial communication protocol }
The communication protocol with the serial screen uses the HAL library, with CubeMX assisting in development.There is dedicated software for the serial screen called UART HMI.Here’s another piece of serial screen communication code I wrote (suitable for HAL library).
/* UART HMI serial screen communication function HMISendnum1, 2, 3 are different strings and numbers concatenated. The following parameters are the serial port number, suitable for HAL library */ void HMISendnum1(char *str1,int nums,UART_HandleTypeDef uart) { HAL_StatusTypeDef Flag; uint8_t b=0xff,i; char buf1[10]; sprintf(buf1, "%s%d", str1, nums); Flag=HAL_UART_Transmit(&uart,(uint8_t*)buf1,strlen(buf1),0xffffffff); while(Flag!=HAL_OK); for(i=0;i<3;i++) { if(b!=0) { HAL_UART_Transmit(&uart,&b,1,0xff); while(Flag!=HAL_OK); } else return; } } void HMISendnum2(char *str1,int nums,char *str2,UART_HandleTypeDef uart) { HAL_StatusTypeDef Flag; uint8_t b=0xff,i; char buf1[10]; sprintf(buf1, "%s%d%s", str1, nums,str2); Flag=HAL_UART_Transmit(&uart,(uint8_t*)buf1,strlen(buf1),0xffffffff); while(Flag!=HAL_OK); for(i=0;i<3;i++) { if(b!=0) { HAL_UART_Transmit(&uart,&b,1,0xff); while(Flag!=HAL_OK); } else return; } } void HMISendnum3(char *str1,int nums1,char *str2,int nums2,UART_HandleTypeDef uart) { HAL_StatusTypeDef Flag; uint8_t b=0xff,i; char buf1[10]; sprintf(buf1, "%s%d%s%d", str1, nums1,str2,nums2); Flag=HAL_UART_Transmit(&uart,(uint8_t*)buf1,strlen(buf1),0xffffffff); while(Flag!=HAL_OK); for(i=0;i<3;i++) { if(b!=0) { HAL_UART_Transmit(&uart,&b,1,0xff); while(Flag!=HAL_OK); } else return; } } // Control the text control of the serial screen, input string commands, followed by the communication serial port number void HMISendtxt(char *str,UART_HandleTypeDef uart) { HAL_StatusTypeDef Flag; uint8_t b=0xff,i; Flag=HAL_UART_Transmit(&uart,(uint8_t*)str,strlen(str),0xff); while(Flag!=HAL_OK); for(i=0;i<3;i++) { if(b!=0) { HAL_UART_Transmit(&uart,&b,1,0xffffffff); while(Flag!=HAL_OK); } else return; } }
Other specific codes are not pasted for now. If you are interested in this project, you can add QQ: 943874487 to discuss together. You can also check young0219’s blog:
https://blog.csdn.net/weixin_43793057/article/details/106158063
Sharing a point about why the time function cannot be used in Keil:
Keil does not support the time() function, which troubled me for a long time. Since generating mines requires a random function, a random seed must be used, which requires the time() function. After many attempts, I still couldn’t use this function, so I asked a very skilled senior, who told me that Keil does not support the time function (if anyone can use it, please let me know, thanks). At the same time, he proposed another solution, using the timer value as the random seed. Initially, it didn’t work because the mines were initialized at the same time, resulting in the same random seed value. However, after adding the interface, the time entering the game became random, which can be considered a successful solution to this problem.

Graduation Project Series: