Tic-tac-toe Game Based on STM32

What are you developing?
Post Reply
jacobli
Posts: 42
Joined: Fri Jun 11, 2021 3:40 am

Tic-tac-toe Game Based on STM32

Post by jacobli »

Summary

Tic-tac-toe is a game played on a 3 by 3 grid, similar to gobang. It gets its name because the board generally does not draw borders and the grid lines are arranged into tic-tac-toe. The tools needed for the game are only paper and pen. Then, two players representing O and X take turns to leave marks on the grid. Any three marks form a straight line, which is the winner.
This article is to use microcontroller STM32 and STONE LCD display to develop a simple tic-tac-toe game.

Materials required for the experiment

STONE STWI070WT-01 TFT LCD display
STM32 board(the chip of the STM32 is CKS32F303)

The system principle

Specify the first tap on the screen must be O, the second tap on the screen is X, has been doing this cycle. Set 8 arrays to store the number of O and X in each row, column and diagonal of each grid, as long as there are three of the same mark is the victory, then there will be a flashing red box to prove the victory of the row, column or diagonal, and then tap reset to start the game again.

GUI design
图片1.png
图片1.png (90.72 KiB) Viewed 20211 times
Connection
STM32 connection diagram.JPG
STM32 connection diagram.JPG (66 KiB) Viewed 20211 times
jacobli
Posts: 42
Joined: Fri Jun 11, 2021 3:40 am

Re: Tic-tac-toe Game Based on STM32

Post by jacobli »

code sharing

Code: Select all

/* Includes ------------------------------------------------------------------*/ 
#include "stm32f0xx_hal.h" 
#include "Uart.h" 
#include "string.h" 
#include "ws2812.h" 
#include "IWDG.h" 
RGB_COLOR USER_RGB_COLOR; 
unsigned char TX_Mode = 1; //Transmission type flag bit 1:232 0:TTL 
unsigned char BLINK_2=0; 
unsigned char RX3_BUF[32]; //Customized three-level receive buffer 
#define BUTTON1 0x81 
#define BUTTON2 0x82 
#define BUTTON3 0x83 
#define BUTTON4 0x84 
#define BUTTON5 0x85 
#define BUTTON6 0x86 
#define BUTTON7 0x87 
#define BUTTON8 0x88 
#define BUTTON9 0x89 
#define BUTTON0 0x8A 
#define CLEAR 0x8E 
unsigned int r_flag1 = 0; 
unsigned int quan[4][2]={0}; 
unsigned int cha [4][2]={0}; 
unsigned int quan_hang1 = 0; 
unsigned int quan_hang2 = 0; 
unsigned int quan_hang3 = 0; 
unsigned int quan_lie1 = 0; 
unsigned int quan_lie2 = 0; 
unsigned int quan_lie3 = 0; 
unsigned int quan_zuoxia = 0; 
unsigned int quan_youxia = 0; 
unsigned int cha_hang1 = 0; 
unsigned int cha_hang2 = 0; 
unsigned int cha_hang3 = 0; 
unsigned int cha_lie1 = 0; 
unsigned int cha_lie2 = 0; 
unsigned int cha_lie3 = 0; 
unsigned int cha_zuoxia = 0; 
unsigned int cha_youxia = 0; //unsigned int r_flag10 = 0; 
void SystemClock_Config(void); 
void Error_Handler(void); 
static void MX_GPIO_Init(void); 
int main(void) 
{ 
uint8_t color_buf = 0; 
//Function Selection 
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */ 
HAL_Init(); 
/* Configure the system clock */ 
SystemClock_Config(); 
/* Initialize all configured peripherals */ 
MX_GPIO_Init(); 
TX_Mode = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4); 
if(TX_Mode) 
MX_USART1_UART_Init(); 
//232 Initialization 
else 
MX_USART1_UART_Init2(); 
//TTl Initialization 
while(1) 
{ 
if(TX_Mode != HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)) 
HAL_NVIC_SystemReset(); 
//Jumper reversal, reboot and reinitialization 
if(MNG_USART1.RX_OVER_FLG ==TRUE) 
// 
Serial data receive flag bit 
{ 
RX3_BUF[0]=MNG_USART1.RX_BUF[7]; 
//Method 2:Get fixed length control name 
RX3_BUF[1]=MNG_USART1.RX_BUF[8]; 
RX3_BUF[2]=MNG_USART1.RX_BUF[9]; 
RX3_BUF[3]=MNG_USART1.RX_BUF[10]; 
RX3_BUF[4]=MNG_USART1.RX_BUF[11]; 
RX3_BUF[5]=MNG_USART1.RX_BUF[12]; 
RX3_BUF[6]=MNG_USART1.RX_BUF[13]; 
//RX3_BUF[7]=MNG_USART1.RX_BUF[14]; 
if((strcmp("button1",(const char *)RX3_BUF))==0) 
//Select control
{ 
color_buf = BUTTON1; 
}
if((strcmp("button2",(const char *)RX3_BUF))==0) 
{ 
color_buf = BUTTON2; 
}
if((strcmp("button3",(const char *)RX3_BUF))==0) 
{ 
color_buf = BUTTON3; 
}
if((strcmp("button4",(const char *)RX3_BUF))==0) 
{ 
color_buf = BUTTON4; 
}
if((strcmp("button5",(const char *)RX3_BUF))==0) 
{ 
color_buf = BUTTON5; 
}
if((strcmp("button6",(const char *)RX3_BUF))==0) 
{ 
color_buf = BUTTON6; 
}
if((strcmp("button7",(const char *)RX3_BUF))==0) 
{ 
color_buf = BUTTON7; 
}
if((strcmp("button8",(const char *)RX3_BUF))==0) 
{ 
color_buf = BUTTON8; 
}
if((strcmp("button9",(const char *)RX3_BUF))==0) 
{ 
color_buf = BUTTON9; 
}
if((strcmp("button0",(const char *)RX3_BUF))==0) 
{ 
color_buf = BUTTON0; 
}
switch (color_buf) 
{ 
case BUTTON1: 
//if(r_flag1 == 0) 
if((r_flag1 == 0)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
r_flag1 ^= 1; 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image2\",\"image\":\"circle\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 ^= 1; 
quan_hang1++; 
quan_lie1++; 
quan_youxia++;  
//memset(RX3_BUF,0,7); 
//memset(MNG_USART1.RX_BUF,0,USART1_RX_LEN); 
color_buf = CLEAR; 
}
else if((r_flag1 == 1)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image2\",\"image\":\"x\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 0; 
cha_hang1++; 
cha_lie1++; 
cha_youxia++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
//r_flag1 = 1; 
break; 
case BUTTON2: 
if((r_flag1 == 0)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image3\",\"image\":\"circle\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 1; 
quan_hang1++; 
quan_lie2++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
else if((r_flag1 == 1)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image3\",\"image\":\"x\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 0; 
cha_hang1++; 
cha_lie2++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
break; 
case BUTTON3: 
if((r_flag1 == 0)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image4\",\"image\":\"circle\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 1; 
quan_hang1++; 
quan_lie3++; 
quan_zuoxia++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
else if((r_flag1 == 1)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image4\",\"image\":\"x\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 0; 
cha_hang1++; 
cha_lie3++; 
cha_zuoxia++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
break; 
case BUTTON4: 
if((r_flag1 == 0)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image5\",\"image\":\"circle\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 1; 
quan_hang2++; 
quan_lie1++; 
//memset(RX3_BUF,0,7); 
//r_flag10=1; 
color_buf = CLEAR; 
}
else if((r_flag1 == 1)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image5\",\"image\":\"x\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 0; 
cha_hang2++; 
cha_lie1++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
break; 
case BUTTON5: 
if((r_flag1 == 0)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image6\",\"image\":\"circle\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 1; 
quan_hang2++; 
quan_lie2++; 
quan_zuoxia++; 
quan_youxia++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
else if((r_flag1 == 1)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image6\",\"image\":\"x\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 0; 
cha_hang2++; 
cha_lie2++; 
cha_zuoxia++; 
cha_youxia++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
break; 
case BUTTON6: 
if((r_flag1 == 0)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image7\",\"image\":\"circle\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 1; 
quan_hang2++; 
quan_lie3++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
else if((r_flag1 == 1)&&(MNG_USART1.RX_BUF[14] == 0x02))
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image7\",\"image\":\"x\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 0; 
cha_hang2++; 
cha_lie3++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
break; 
case BUTTON7: 
if((r_flag1 == 0)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image8\",\"image\":\"circle\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 1; 
quan_hang3++; 
quan_lie1++; 
quan_zuoxia++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
else if((r_flag1 == 1)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image8\",\"image\":\"x\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 0; 
cha_hang3++; 
cha_lie1++; 
cha_zuoxia++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
break; 
case BUTTON8: 
if((r_flag1 == 0)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image9\",\"image\":\"circle\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 1; 
quan_hang3++; 
quan_lie2++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
else if((r_flag1 == 1)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image9\",\"image\":\"x\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 0; 
cha_hang3++; 
cha_lie2++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
break; 
case BUTTON9: 
if((r_flag1 == 0)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image10\",\"image\":\"circle\"}>ET");
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 1; 
quan_hang3++; 
quan_lie3++; 
quan_youxia++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
else if((r_flag1 == 1)&&(MNG_USART1.RX_BUF[14] == 0x02)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image10\",\"image\":\"x\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
r_flag1 = 0; 
cha_hang3++; 
cha_lie3++; 
cha_youxia++; 
//memset(RX3_BUF,0,7); 
color_buf = CLEAR; 
}
break; 
case BUTTON0: 
r_flag1 = 0; 
quan_hang1=quan_hang2=quan_hang3=cha_hang1=cha_hang2=cha_hang3=0; 
quan_lie1=quan_lie2=quan_lie3=cha_lie1=cha_lie2=cha_lie3=0; 
quan_zuoxia=quan_youxia=cha_zuoxia=cha_youxia=0; 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image2\",\"image\":\"bai\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image3\",\"image\":\"bai\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image4\",\"image\":\"bai\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image5\",\"image\":\"bai\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image6\",\"image\":\"bai\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image7\",\"image\":\"bai\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image8\",\"image\":\"bai\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image9\",\"image\":\"bai\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1);
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_image\",\"type\":\"image\",\"widget\":\"image10\",\"image\":\"bai\"}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif4\",\"visible\":false}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif5\",\"visible\":false}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif6\",\"visible\":false}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif7\",\"visible\":false}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif8\",\"visible\":false}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif9\",\"visible\":false}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif10\",\"visible\":false}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif11\",\"visible\":false}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
//memset(RX3_BUF,0,7); 
memset(MNG_USART1.RX_BUF,0,USART1_RX_LEN); 
break; 
default: 
MNG_USART1.RX_OVER_FLG = FALSE; 
break; 
}
//////////////////////////////////////////////////////////////////// 
if((quan_hang1==3)||(cha_hang1==3)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif4\",\"visible\":true}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
quan_hang1=quan_hang2=quan_hang3=cha_hang1=cha_hang2=cha_hang3=0; 
quan_lie1=quan_lie2=quan_lie3=cha_lie1=cha_lie2=cha_lie3=0; 
quan_zuoxia=quan_youxia=cha_zuoxia=cha_youxia=0; 
}
else if((quan_hang2==3)||(cha_hang2==3)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif5\",\"visible\":true}>ET");
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
quan_hang1=quan_hang2=quan_hang3=cha_hang1=cha_hang2=cha_hang3=0; 
quan_lie1=quan_lie2=quan_lie3=cha_lie1=cha_lie2=cha_lie3=0; 
quan_zuoxia=quan_youxia=cha_zuoxia=cha_youxia=0; 
}
else if((quan_hang3==3)||(cha_hang3==3)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif6\",\"visible\":true}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
quan_hang1=quan_hang2=quan_hang3=cha_hang1=cha_hang2=cha_hang3=0; 
quan_lie1=quan_lie2=quan_lie3=cha_lie1=cha_lie2=cha_lie3=0; 
quan_zuoxia=quan_youxia=cha_zuoxia=cha_youxia=0; 
}
else if((quan_lie1==3)||(cha_lie1==3)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif7\",\"visible\":true}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
quan_hang1=quan_hang2=quan_hang3=cha_hang1=cha_hang2=cha_hang3=0; 
quan_lie1=quan_lie2=quan_lie3=cha_lie1=cha_lie2=cha_lie3=0; 
quan_zuoxia=quan_youxia=cha_zuoxia=cha_youxia=0; 
}
else if((quan_lie2==3)||(cha_lie2==3)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif8\",\"visible\":true}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
quan_hang1=quan_hang2=quan_hang3=cha_hang1=cha_hang2=cha_hang3=0; 
quan_lie1=quan_lie2=quan_lie3=cha_lie1=cha_lie2=cha_lie3=0; 
quan_zuoxia=quan_youxia=cha_zuoxia=cha_youxia=0; 
}
else if((quan_lie3==3)||(cha_lie3==3)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif9\",\"visible\":true}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
quan_hang1=quan_hang2=quan_hang3=cha_hang1=cha_hang2=cha_hang3=0; 
quan_lie1=quan_lie2=quan_lie3=cha_lie1=cha_lie2=cha_lie3=0; 
quan_zuoxia=quan_youxia=cha_zuoxia=cha_youxia=0; 
}
else if((quan_zuoxia==3)||(cha_zuoxia==3)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif11\",\"visible\":true}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
quan_hang1=quan_hang2=quan_hang3=cha_hang1=cha_hang2=cha_hang3=0; 
quan_lie1=quan_lie2=quan_lie3=cha_lie1=cha_lie2=cha_lie3=0; 
quan_zuoxia=quan_youxia=cha_zuoxia=cha_youxia=0; 
}
else if((quan_youxia==3)||(cha_youxia==3)) 
{ 
memset(MNG_USART1.TX_BUF,0,USART1_TX_LEN); 
sprintf((char *)MNG_USART1.TX_BUF, 
"ST<{\"cmd_code\":\"set_visible\",\"type\":\"widget\",\"widget\":\"gif10\",\"visible\":true}>ET"); 
MNG_USART1.TX_Byte_Num = strlen((const char *)MNG_USART1.TX_BUF); 
USART_TX_Data(MNG_USART1); 
quan_hang1=quan_hang2=quan_hang3=cha_hang1=cha_hang2=cha_hang3=0; 
quan_lie1=quan_lie2=quan_lie3=cha_lie1=cha_lie2=cha_lie3=0; 
quan_zuoxia=quan_youxia=cha_zuoxia=cha_youxia=0; 
} 
MNG_USART1.RX_OVER_FLG = FALSE; 
} 
} 
}
/** System Clock Configuration 
*/
void SystemClock_Config(void) 
{ 
RCC_OscInitTypeDef RCC_OscInitStruct; 
RCC_ClkInitTypeDef RCC_ClkInitStruct; 
RCC_PeriphCLKInitTypeDef PeriphClkInit; 
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 
RCC_OscInitStruct.HSIState = RCC_HSI_ON; 
RCC_OscInitStruct.HSICalibrationValue = 16; 
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; 
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; 
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12; 
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1; 
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 
{ 
Error_Handler(); 
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 
|RCC_CLOCKTYPE_PCLK1; 
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; 
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; 
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) 
{ 
Error_Handler(); 
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1; 
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1; 
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) 
{ 
Error_Handler(); 
}
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); 
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); 
/* SysTick_IRQn interrupt configuration */ 
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); 
}
/** Pinout Configuration 
*/
static void MX_GPIO_Init(void) 
{ 
GPIO_InitTypeDef GPIO_InitStruct; 
/* GPIO Ports Clock Enable */ 
__HAL_RCC_GPIOA_CLK_ENABLE(); 
GPIO_InitStruct.Pin = GPIO_PIN_7; 
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP ; 
GPIO_InitStruct.Pull = GPIO_PULLDOWN; 
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 
GPIO_InitStruct.Pin = GPIO_PIN_4; 
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 
GPIO_InitStruct.Pull = GPIO_PULLUP; 
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; 
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */ 
/**
* @brief This function is executed in case of error occurrence. 
* @param None 
* @retval None 
*/ 
void Error_Handler(void) 
{ 
/* USER CODE BEGIN Error_Handler */ 
/* User can add his own implementation to report the HAL error return state */ 
while(1) 
{
}
/* USER CODE END Error_Handler */ 
}
#ifdef USE_FULL_ASSERT 
/**
* @brief Reports the name of the source file and the source line number 
* where the assert_param error has occurred. 
* @param file: pointer to the source file name 
* @param line: assert_param error line source number 
* @retval None 
*/ 
void assert_failed(uint8_t* file, uint32_t line) 
{ 
/* USER CODE BEGIN 6 */ 
/* User can add his own implementation to report the file name and line number, 
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 
/* USER CODE END 6 */ 
}
#endif 
/**
* @} 
*/ 
/**
* @} 
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
jacobli
Posts: 42
Joined: Fri Jun 11, 2021 3:40 am

Re: Tic-tac-toe Game Based on STM32

Post by jacobli »

jacobli
Posts: 42
Joined: Fri Jun 11, 2021 3:40 am

Re: Tic-tac-toe Game Based on STM32

Post by jacobli »

I don't think the code is too boring like this.
brooklynntgordon
Posts: 2
Joined: Sun Feb 26, 2023 2:03 am

Re: Tic-tac-toe Game Based on STM32

Post by brooklynntgordon »

That's pretty cool! I've always been interested in experimenting with microcontrollers, and your project seems like a fun way to learn. Your explanation made it easier for someone like me, who might not be a pro at this, to understand the concept. Though I prefer board games with dice roller online, I'll check this one out, too. Thanks!
Freddiebest
Posts: 3
Joined: Wed Jan 25, 2023 3:52 pm

Re: Tic-tac-toe Game Based on STM32

Post by Freddiebest »

Your project is fascinating! It's impressive to see how you've combined technology like the STM32 microcontroller and STONE LCD display to create a modern twist on the classic tic-tac-toe game. Exploring new ways to bring traditional games into the digital realm is always exciting.
While you're delving into game development, you might find it enjoyable to try out some gaming apps during your leisure time. Best Apps To Play Games offers a diverse selection of games to help you relax and unwind after your coding sessions.
Keep up the fantastic work with your projects, and don't hesitate to share any future developments with the community!
Post Reply

Return to “Projects”