2134 views|2 replies

6551

Posts

0

Resources
The OP
 

MSP430 MCU Tetris Game Programming [Copy link]

MSP430 MCU game program, Tetris, uses TI's official MSP430F5529 development board. TI's official development board, schematics are available online, and TI's official website also has


Source code:
/*Tetris game program based on MSP430F5529*/
//Author: Zhan Lei
//Function: Tetris game program based on MSP430F5529
//Description: Operating environment: TI official MSP-EXP5529 development board (external 4 independent buttons with pull-up resistors)
// Not original, the idea is transplanted from the 51 single-chip microcomputer
// The underlying LCD driver is the LCD API officially provided by TI
// The underlying program of the game algorithm part has been modified in many places, which will be quite different from the original version
//Version: Beta version V1.0 (basically it can run smoothly, but needs further beautification and optimization)
#include "MSP430F5529.h"
#include "LCD_102x64.h"
#include "main.h"
#include "Key.h"
#define MAXHANG 20 //Number of game display lines
#define MAXLIE 16 //Number of columns for game display
#define MapWide 48 //Map width
#define MapHigh 60 //Map height
#define ON 0
#define OFF 1

#define BIT(n) (1<<(n))
volatile uint cubeMap[MAXHANG]; //Map display storage
typedef struct{
uchar const * box; //Cube graphic font data pointer
uchar cube : 4; //Cube shape (occupies 4 bits)
uchar state : 4; //Cube state (occupies 4 bits)
char row; //The row where the cube is located
char column; //The column where the cube is located
} block;
block this;
uchar liang=1;
uchar Speed=1,Score=0;
uint timeA=0;
uchar downflag=0;
uchar randomNumber=0;
const uchar cube[]=
{
/* ■
■■■
*/
0,4,0xe,0, 0,2,6,2, 0,7,2,0, 4,6,4,0,

/*■
■■■
*/
0,8,0xe,0, 0,4,4,0xc, 0,0,0xe,2, 0,6,4,4,
/*■■■

*/
0, 0xe,8,0, 0,4,4,6, 0,1,7,0, 6,2,2,0,
/*■■
■■
*/
0,0xc,6,0, 0,2, 6,4, 0,6,3,0, 2,6,4,0,
/* ■■
■■
*/
0,6,0xc,0, 0,4,6,2, 0,3,6, 0, 4,6,2,0,
/*■■■■
*/
0,0xf,0,0, 4,4,4,4, 0,0,0xf,0, 2,2,2,2,
/*■■
■■
*/
0,6,6,0, 0,6,6,0, 0,6,6,0, 0,6,6,0
};
void delay_ms(uint z)//millisecond delay function
{
uint i, m;
for(i=z;i>0;i--)
for(m=4500;m>0;m--)
{
_NOP();
}
}
//----------- --------------------------------//
//Function name: 8-bit LED light control function
//Entry: Number :Number which digit is one then which LED lights up
//Export: Void
//Function:
//--------------------------- ----------------//
void DigitalLedControl(uchar Number)
{
if(Number&BIT7)
P1OUT|=BIT0;
else
P1OUT&=~BIT0;
if(Number&BIT6)
P8OUT|=BIT1;
else
P8OUT&=~BIT1;
if(Number&BIT5)
P8OUT|=BIT2;
else
P8OUT&=~BIT2;
if(Number&BIT4)
P1OUT|=BIT1;
else
P1OUT&=~BIT1;
if(Number&BIT3)
P1OUT|=BIT2;
else
P1OUT&=~BIT2;
if(Number&BIT2)
P1OUT|=BIT3;
else
P1OUT&=~BIT3;
if(Number&BIT1)
P1OUT|=BIT4;
else
P1OUT&=~BIT4;
if(Number&BIT0)
P1OUT|=BIT5;
else
P1OUT&=~BIT5;
}
//----------- --------------------------------//
//Function name: Video memory clear function
//Entry: Void
//Exit :Void
//Function: Clear video memory
//---------------------------------------- ---//
void DisplayRamClear()
{
uchar i;
for(i=0;i<20;i++)
{
cubeMap=0x0;
}
}
//----------------------------------------- -----//
//Function name: Two-digit decimal number display function
//Entry: Row: the number of rows to be displayed
// Column: the number of columns to be displayed
// Number: the number to be displayed <100 && >=0
//Exit: Void
//Function:
//-------------------------------------------//
void Number2BitDisplay(uchar Row,uchar Column,uchar Number)
{
uchar tempShi,tempGe;
tempShi=Number/10;
tempGe=Number%10;
Dogs102x6_charDraw(Row, Column,tempShi+'0',ON);
Dogs102x6_charDraw(Row, Column+6,tempGe+'0',ON);
}
//-------------------------------------------//
//Function name: Game background function
//Entry: Void
//Exit: Void
//Function: Draw the game background map
//-------------------------------------------//
void GameBackground()
{
uchar i;
for(i=0;i<MapHigh+1;i++)
{
Dogs102x6_pixelDraw(0, i, ON);
Dogs102x6_pixelDraw(MapWide+1, i, ON);
}
for(i=0;i<MapWide+2;i++)
{
Dogs102x6_pixelDraw(i, 0, ON);
Dogs102x6_pixelDraw(i, MapHigh+1, ON);
}
DisplayRamClear();
Dogs102x6 _stringDraw(3, 10, "START",ON);
Dogs102x6_stringDraw(1, 51, "NEXT:",ON);
Dogs102x6_stringDraw(3, 51, "SPEED:--",ON);
Dogs102x6_stringDraw(5, 51, "SCORE:--",ON);
Number2BitDisplay(3,87,Speed);
Number2BitDisplay(5,87,Score);
}
//-------------------------------------------//
//Function name: Game Map clear
//Entry: Void
//Exit: Void
//Function: Clear the game background map (do not clear the video memory)
//-------------------------------------------//
void GameMapClear()
{
uchar i,j;
for(i=0;i<MapHigh;i++)
{
for(j=0;j<MapWide;j++)
{
Dogs102x6_pixelDraw(j+1, i+1, OFF);
}
}
}
//-------------------------------------------//
//Function name: System initialization
//Entry: Void
//Exit: Void
//Function:
//-------------------------------------------//
void SystemInit()
{
WDTCTL = WDTPW + WDTHOLD;
P8DIR|=BIT1+BIT2;
P8OUT|=BIT1+BIT2;
P1DIR|=BIT0+BIT1+BIT2+BIT3+BIT4+BIT5;
P1OUT|=BIT0+BIT1+BIT2+BIT3+BIT4+BIT5;
InitKey();

UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO

__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
UCSCTL1 = DCORSEL_7; // Select DCO range 50MHz operation
UCSCTL2 = FLLD_1 + 609; // Set DCO Multiplier for 25MHz
// (N + 1) * FLLRef = Fdco
// (762 + 1) * 32768 = 25MHz
// Set FLL Div = fDCOCLK/2
__bic_SR_register(SCG0); // Enable the FLL control loop
TA1CCTL0 = CCIE; // CCR0 interrupt enabled
TA1CCR0 = 328-1;
TA1CTL = TASSEL_1 + MC_1 + TACLR; // ACLK, upmode, clear TAR
_BIS_SR(GIE);
Dogs102x6_init();
Dogs102x6_clearScreen();
Dogs102x6_backlightInit();
Dogs102x6_setContrast(11);
Dogs102x6_setBacklight(liang);
}
//-------------------------------------------//
//Function name: 3x3 dot display function
//Entry: x: x-axis coordinate
// y: y-axis coordinate
// mode: ON dot; OFF dot elimination
//Exit: Void
//Function:
//-------------------------------------------//
void Display3x3Pixels(uchar x,uchar y,uchar mode)
{
x=3*x+1;
y=3*y+1;
Dogs102x6_pixelDraw(x, y, mode);
Dogs102x6_pixelDraw(x+1, y, mode);
: Dogs102x6_pixelDraw(x+2, y, mode);
Dogs102x6_pixelDraw(x, y+1, mode);
Dogs102x6_pixelDraw(x+1, y+1, mode);
Dogs102x6_pixelDraw(x+2, y+1, mode);
Dogs102x6_pixelDraw(x, y+2, mode);
Dogs102x6_pixelDraw(x+1, y+2, mode);
Dogs102x6_pixelDraw(x+2, y+2, mode);
}
//-------------------------------------------//
//Function name: Display pixel change function
//Entry: Row: number of rows
// Column: number of columns
// Mode: ON dot; OFF dot elimination
//Exit: Void
//Function:
//-------------------------------------------//
void ChangeRamBit(uchar Row,uchar Column,uchar mode)
{
uint tempData;
tempData=(0x8000>>(Column%16));
if(mode==ON)
cubeMap[Row]|=tempData;
else
cubeMap[Row]&=~tempData;
}
//------------------------------------------------//
//Function name: Read a certain display pixel point value function
//Entry: Row: row number
//Column: column number
//Exit: uchar 1: selected point 1, 0: selected point 0
//Function:
//-------------------------------------------//
uchar ReadDisplayRamBit(uchar Row,uchar Column)
{
uint tempData;
tempData=(0x8000>>(Column%16));
if(cubeMap[Row]&tempData)
return 1;
else
return 0;
}
//-------------------------------------------//
//Function name: Next cube display function
//Entry: x: x-axis coordinate
// y: y-axis coordinate
// *p: graphic font data pointer
//Exit: Void
//Function:
//-------------------------------------------//
void showNextCube(uchar const * p,uchar x,uchar y)
#define NexPoinX 27 //The X coordinate of the next graphic display
#define NexPoinY 2 //The Y coordinate of the next graphic display
{
uchar i,j,temp;
for(i=0;i<4;i++)
{
temp=0x08;
for(j=0;j<4;j++)
{
if(p & temp)
Display3x3Pixels(x+j,y+i,ON);
else
Display3x3Pixels(x+j,y+i,OFF);
temp>>=1;
}
}
}
//-------------------------------------------//
//Function name: Display function
//Entry: Void
//Exit: Void
//Function: Write the contents of the video memory to the display for display
//------------------------------------------//
void showCubeMap(void)
{
uchar hang,lie;
for(hang=0;hang<MAXHANG;hang++)
{
if(cubeMap[hang]!=0) //Skip a row where all zerosfor
(lie=0;lie<MAXLIE;lie++)
{
if(cubeMap[hang]&(0x8000>>(lie%16)))
Display3x3Pixels(lie,hang,ON);
}
}
}
//-------------------------------------------//
//Function name: Generate new cube function
//Entrance: Void
//Exit: Void
//Function:
//-------------------------------------------//
void createCube(void)
{
static uchar next=0;
this.cube=next;
next=randomNumber%7; //Generate random number
this.row=0; //Initial row is 0
this.column=6; //Initial column is 6
this.state=0; //The initial state is 0
this.box=cube+16*this.cube; //Set the starting address of the cube pointer
showNextCube(cube+16*next,NexPoinX,NexPoinY); //The prompt area displays the next cube
timeA=0;
downflag=0;
}
//-------------------------------------------//
//Function name: Write video memory function
//Entry: mode:ON dot; OFF eliminate dot
//Exit: Void
//Function: Write data to video memory and write to the display at the same time
//-------------------------------------------//
void writeCubeToMap(uchar mode)
{
uchar tempData,tempRow,tempColumn,i,j,k=0;
for(i=0;i<4;i++)
{
tempData=this.box;
if(tempData==0)
continue;
for(j=0;j<4;j++)
{
if(tempData&0x08)
{
tempRow=this.row+k;
tempColumn=this.column+j;
ChangeRamBit(tempRow,tempColumn,mode);
Display3x3Pixels(tempColumn,tempRow,mode);
}
tempData<<=1;
}
k++;
}
}
//-------------------------------------------//
//Function name: Block edge detection function
//Entry: Void
//Exit: uchar 0: Can continue to move 1: At the end, stop moving
//Function:
//-------------------------------------------//
uchar checkClask(void)
{
uchar tempData,tempRow,tempColumn,i,j,k=0;
for(i=0;i<4;i++)
{
tempData=this.box;
if(tempData==0) //Skip the row with all zeroscontinue
;
for(j=0;j<4;j++)
{
if(tempData&0x08)
{
tempRow=this.row+k;
tempColumn=this.column+j;
if (
ReadDisplayRamBit(tempRow,tempColumn) //Contact with the block on the side
||(tempRow>=MAXHANG) //Reached the bottom
||(tempColumn>=MAXLIE) //Reached the rightmost side
||(tempColumn==255) //reached the far left
)
return 1; //return 1, stop moving
}
tempData<<=1;
}
k++;
}
return 0; //return 0, you can continue moving
}
//-------------------------------------------//
//Function name: map detection function
//Entry: Void
//Exit: Void
//Function: Check whether there are full lines in the map. If so, eliminate the line and add points. After a certain number of points, speed up and refresh the screen at the same time
//-------------------------------------------//
void CheckMap()
{
uchar fullFlag=0;
static uchar tempScore=0,tempSpeed=0;
char i,j;
for(i=MAXHANG-1;i>0;i--)
{
if(cubeMap==0xffff)
{
fullFlag++;
for(j=i;j>0;j--)
{
if(j>0)
cubeMap[j]=cubeMap[j-1];
else
cubeMap[j]=0;
}
i++;
}
}
tempScore=Score;
tempSpeed=Speed;
switch(fullFlag)
{
case 1:Score++;break;
case 2:Score+=2;break;
case 3:Score+=4;break;
case 4:Score+=6;break;
default:break;
}
if(Score>99)
{
Speed++;
if(Speed>9)
Speed=1;
Score=0;
}
if(tempScore!=Score)
Number2 BitDisplay(5,87,Score);
if(tempSpeed!=Speed)
Number2BitDisplay(3,87,Speed);
if(fullFlag)
{
GameMapClear();
showCubeMap();
}
}
//---------------------------------------------//
//Function name: Block left movement function
//Entry: Void
//Exit: Void
//Function:
//------------------------------------------------//
void moveLeft(void)
{
writeCubeToMap(OFF);
this.column--;
if(checkClask())
this.column++;
writeCubeToMap(ON);
}
//-------------------------------------------//
//Function name: Cube right move function
//Entry: Void
//Exit: Void
//Function:
//-------------------------------------------//
void moveRigh(void)
{
writeCubeToMap(OFF);
this.column++;
if(checkClask())
this.column--;
writeCubeToMap(ON);
}
//-------------------------------------------//
//Function name: Cube down move function
//Entry: Void
//Exit: uchar Returns 1 in the end, otherwise returns 0;
//Function:
//-------------------------------------------//
uchar moveDown(void)
{
writeCubeToMap(OFF);
this.row++;
if(checkClask()) //In the end
{
this.row--;
writeCubeToMap(ON);
CheckMap(); //Whether to delete the row
return 1;
}
else
{
writeCubeToMap(ON);
return 0;
}
}
//-------------------------------------------//
//Function name: cube flip function
//Entrance: Void
//Exit: Void
//Function:
//------------------------------//
void cubeRotation(void)
{
uchar temp;
temp=this.state;
writeCubeToMap(OFF);
this.state=++this.state%4;
this.box=cube+16*this.cube+4*this.state;
if(checkClask())
{
this.state=temp;
this.box=cube+16*this.cube+4*this.state;
}
writeCubeToMap(ON);
}
//-------------------------------------------//
//Function name: Tetris end interface function
//Entrance: Void
//Exit: Void
//Function:
//------------------------------------------//
void TetrisGameOver()
{
DisplayRamClear();
GameMapClear();
Score=0;
Speed=1;
Number2BitDisplay(5,87,Score);
Number2BitDisplay(3,87,Speed);
Dogs102x6_stringDraw(3, 13, "GAME",ON);
Dogs102x6_stringDraw(4, 13, "OVER",ON);
}
//-------------------------------------------//
//Function name: Tetris game running function
//Entry: Void
//Exit: Void
//Function: Do not block CPU
//-------------------------------------------//
void TetrisGame()
{
uchar temp=0;
static uchar Flag=0;
if(Flag==0)
{
createCube();
if(checkClask())
{
TetrisGameOver();
delay_ms(1000);
GameMapClear();
}
else
writeCubeToMap(ON);
Flag=1;
}
if(downflag&&Flag)
{
downflag=0;
if(moveDown())
{
Flag=0;
}
}
temp=GetKey();
switch(temp)
{
case KeyUP:cubeRotation();DigitalLedControl(0);
break ;
case KeyDOWN:while(moveDown()==0);Flag=0;DigitalLedControl(1);
break;
case KeyLEFT:moveLeft();DigitalLedControl(2);
break;
case KeyRIGHT:moveRigh();DigitalLedControl(3);
break;
case KeyBACK:DigitalLedControl(4);liang=++liang%12;Dogs102x6_setBacklight(liang);
break;
case KeyMEU:DigitalLedControl(5);
break;
case 0:
break;
default:
break;
}
}
void main(void)
{
SystemInit();
GameBackground();
DisplayRamClear();
GameMapClear();
showCubeMap();
while(1)
{
TetrisGame();
}
}
#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR(void)
{
randomNumber=++randomNumber%100;
if(downflag==0)
timeA ++;
if(timeA==(10-Speed)*10)
{
timeA=0;
downflag=1;
}
}

This post is from Microcontroller MCU

Latest reply

The program routine is well shared, thank you. I will learn from it.   Details Published on 2020-5-22 11:10
 

1w

Posts

204

Resources
2
 

Thanks for sharing, it looks good

This post is from Microcontroller MCU
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
Personal signature

玩板看这里:

http://en.eeworld.com/bbs/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 

2618

Posts

0

Resources
3
 

The program routine is well shared, thank you. I will learn from it.

This post is from Microcontroller MCU
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list