51 MCU game (Tetris)

Publisher:橙子1234Latest update time:2022-08-22 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Circuit Design

This circuit consists of AT89C51 minimum system, 12864 display module and independent buttons.

It can realize games similar to Tetris.


2. Operation Effect

insert image description here

3. Partial Code

/*Want more projects private wo!!!*/

#include

#include "pic.c"

#include

#define LCD_DATA P2

#define button_delay 150 //Button delay

#define button_acceleration 65 //Button acceleration threshold

#define GAME_LOCATION 30

sbit button_a = P3^4; //deformation

sbit button_b = P3^5; //Start

sbit up = P3^2;    //Pause start

sbit down = P3^0;

sbit left = P3^1;

sbit right = P3^3;

sbit speaker=P3^6; 


sbit LCD_RS=P1^0;

sbit LCD_RW=P1^1;

sbit LCD_E=P1^2;

sbit LCD_CS2=P1^4; //right screen selection (left and right screens are sometimes opposite)

sbit LCD_CS1=P1^3; //Left screen selection

sbit LCD_RST=P3^7;


unsigned int up_reg=button_delay; //button up accumulator

unsigned int down_reg=button_delay; //button down accumulator

unsigned int left_reg=button_delay; //button left accumulator

unsigned int right_reg=button_delay; //button right accumulator

unsigned int button_a_reg=button_delay; //button_a accumulator

unsigned int button_b_reg=button_delay; //button_b accumulator

unsigned int right_acceleration=0; //button right acceleration register

unsigned int left_acceleration=0; //Key left acceleration register


unsigned int idata Box_Ram[19]; ​​//define game dot cache 10*16

unsigned char box_down_reg; //Define the box down accumulation register

unsigned char time0_reg; //define timer 0 accumulation register

unsigned char next_mode; //Define the type of the next block

unsigned char next_shape; //define the shape of the next block

unsigned int destroy_row_num=0; //Define the number of rows to be destroyed

unsigned char speed_num=0; //Define the game speed level

unsigned char level_num; //Define the game difficulty level

bit game_over_flag; //Game over flag position 0 means the game is not over

bit pause_game_flag; //Game pause flag position 0 means the game is not paused


struct

{

unsigned char mode; //type

unsigned char shape; // shape

unsigned char x; //x coordinate

unsigned char y; //y coordinate

unsigned int box; //define the block cache

}s_box; //Define the box structure

//LCD detects busy status function

void LCD_check_busy()

{

unsigned char temp;

LCD_RS=0;

LCD_RW=1;

do

{

LCD_DATA=0xff;

LCD_E=1;

temp=LCD_DATA;

LCD_E=0;

}while((temp&0x80)==0x80);

}

//Write instruction code (cs is 0 to select the left screen, cs is 1 to select the right screen)

void LCD_W_code(unsigned char tpcode,bit cs)

{

LCD_RS=0;

LCD_RW=0;

LCD_CS2=~cs;

LCD_CS1=cs;

LCD_DATA=tpcode;

LCD_E=1;

_nop_();

LCD_E=0;

}

//Write display data (cs is 0 to select the left screen, cs is 1 to select the right screen)

void LCD_W_data(unsigned char tpdata,bit cs)

{

LCD_check_busy();

LCD_RS=1;

LCD_RW=0;

LCD_CS2=~cs;

LCD_CS1=cs;

LCD_DATA=tpdata;

LCD_E=1;

_nop_();

LCD_E=0;

}


//LCD initialization function

void LCD_initialize()

{

LCD_RST=0;

_nop_();

_nop_();

LCD_RST=1;

LCD_W_code(0x3f,0); //Open display settings

LCD_W_code(0xc0,0); //Set the display start line to the first line

LCD_W_code(0xb8,0); //page address setting

LCD_W_code(0x40,0); //Set the column address to 0

LCD_W_code(0x3f,1);

LCD_W_code(0xc0,1);

LCD_W_code(0xb8,1);

LCD_W_code(0x40,1);

}

//LCD clear screen function

void LCD_clear()

{

unsigned char i,j;

for(j=0;j<8;j++)

{

LCD_W_code(0xb8+j,0);

LCD_W_code(0x40,0);

LCD_W_code(0xb8+j,1);

LCD_W_code(0x40,1);

for(i=0;i<64;i++)

{

LCD_W_data(0x00,0);

LCD_W_data(0x00,1);

}

}

}

//LCD display string function (word represents the string to be displayed,

 //length indicates the width of the string to be displayed,

 //x represents the line number of the first character,

 //y represents the column number of the first character)

void LCD_display_word(unsigned char word[],

                      unsigned int length,

  unsigned char x,

  unsigned char y)

{

unsigned char i;

for(i=0;i {

LCD_W_code(0xb8+x,0);

LCD_W_code(0xb8+x,1);

if(y+i<64)

{

LCD_W_code(0x40+y+i,0);

LCD_W_data(word[i],0);

}

else

{

LCD_W_code(y+i,1);

LCD_W_data(word[i],1);

}

}

}

//LCD full screen drawing function

void LCD_full_draw(unsigned char word[])

{

unsigned char i,j;

for(i=0;i<8;i++)

{

LCD_W_code(0xb8+i,0);

LCD_W_code(0x40,0);

for(j=0;j<64;j++)

{

LCD_W_data(word[i*128+j],0);

}

LCD_W_code(0xb8+i,1);

LCD_W_code(0x40,1);

for(j=0;j<64;j++)

{

LCD_W_data(word[i*128+64+j],1);

}

}

}

//LCD displays a byte function (

  //x represents the x coordinate,

  //y represents the y coordinate,

  //tpdata indicates the data to be displayed)

void LCD_display_byte(unsigned char x,

  unsigned char y,

  unsigned char tpdata)

{

if(x<64)

{

LCD_W_code(0xb8+y,0);

LCD_W_code(0x40+x,0);

LCD_W_data(tpdata,0);

}

else

{

LCD_W_code(0xb8+y,1);

LCD_W_code(x,1);

LCD_W_data(tpdata,1);

}


void LCD_draw(unsigned char word[])

{

  unsigned char i,j;

  for(i=0;i<8;i++)

  {

    LCD_W_code(0xb8+i,1);

LCD_W_code(0x40+20,1);

for(j=0;j<44;j++)

{

  LCD_W_data(word[i*44+j],1);

}

  }

}

//Basic interface display function

void display_basic()

{

unsigned char i;

for(i=0;i<8;i++)

{

LCD_display_byte(GAME_LOCATION,i,0xff);

LCD_display_byte(GAME_LOCATION+41,i,0xff);

}

}

// Refresh the game area function

void refurbish_display()

{

unsigned char i,j,tpdata;

for(i=0;i<8;i++)

{

for(j=0;j<10;j++)

{

tpdata=0x00;

if( (Box_Ram[2*i]>>(12-j))&0x0001==1 )

{

tpdata=0x0f;

}

if( (Box_Ram[2*i+1]>>(12-j))&0x0001==1 )

{

tpdata|=0xf0;

}

LCD_display_byte(GAME_LOCATION+1+j*4,i,tpdata);

LCD_display_byte(GAME_LOCATION+2+j*4,i,0xbb&tpdata);

LCD_display_byte(GAME_LOCATION+3+j*4,i,0xdd&tpdata);

LCD_display_byte(GAME_LOCATION+4+j*4,i,tpdata);

}

}

}


Reference address:51 MCU game (Tetris)

Previous article:51 single-chip multi-channel temperature measurement alarm system (AT89C51, multiple DS18B20, LCD1602)
Next article:51 single chip game (pushing box)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号