#include <reg52.h>
// 点阵寄存器定义
sbit LOAD = P1 ^ 0;
sbit CLK = P1 ^ 1;
sbit DATA = P1 ^ 2;
sbit mKey4 = P3 ^ 4;
sbit mKey5 = P3 ^ 5;
sbit mKey6 = P3 ^ 6;
sbit mKey7 = P3 ^ 7;
// 数码管寄存器定义
sbit DLE = P2 ^ 6;
sbit WLE = P2 ^ 7;
unsigned long timeNumber = 0;
unsigned char digits[] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66,
0x6d, 0x7d, 0x07, 0x7f, 0x6f };
void digitInit() {
P0 = 0x00;
DLE = 1;
DLE = 0;
P0 = 0xff;
WLE = 1;
WLE = 0;
}
void showNumber(unsigned long number) {
unsigned char dTem;
if(number == 0) {
P0 = digits[0];
DLE = 1;
DLE = 0;
P0 = ~0x20;
WLE = 1;
WLE = 0;
}
else {
for (dTem = 0x20; (dTem != 0) && (number != 0); dTem >>= 1) {
P0 = digits[number % 10];
DLE = 1;
DLE = 0;
P0 = ~dTem;
WLE = 1;
WLE = 0;
number /= 10;
}
}
}
void showDigit(unsigned char digit, unsigned char position) {
P0 = digits[digit];
DLE = 1;
DLE = 0;
P0 = ~(0x01 << position);
WLE = 1;
WLE = 0;
}
// 将数据写入点阵中一行
#define LATTICE_LINE_WRITE(DATA, LINE) latticeWriteData(~(DATA), 0x0001 << (LINE))
// 将数据写入点阵中一列
#define LATTICE_LIST_WRITE(DATA, LIST) latticeWriteData(~(0x0001 << (LIST)), DATA)
/* 功能 :将数据写入点阵寄存器 参数 :<unsigned int>列寄存器<unsigned int>行寄存器 返回值 :<void> 详细信息:列寄存器将被先写入,行寄存器随后 */
void latticeWriteData(unsigned int lineData, unsigned int listData) {
unsigned char i;
for (i = 0; i < 16; i++) {
DATA = lineData & 0x8000;
lineData <<= 1;
CLK = 1;
CLK = 0;
}
for (i = 0; i < 16; i++) {
DATA = listData & 0x8000;
listData <<= 1;
CLK = 1;
CLK = 0;
}
LOAD = 1;
LOAD = 0;
}
unsigned int temFunction(char tData[16]) {
unsigned int result = 0;
char i;
for(i = 15; i >= 0; i--) {
result <<= 1;
result |= (tData[i] > 0) | (tData[i] == -1);
}
return result;
}
/* 功能 :主函数 参数 :<void> 返回值 :<void> */
void main() {
xdata char map[16][16] = {
0 };
unsigned char direction = 1, x = 0, y = 0, length = 3, i, j;
unsigned long systemCD = 0, keyCD = 0, foolCD = 0;
TMOD = 0x01;
TH0 = (65535 - 10000) / 256;
TL0 = (65535 - 10000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
P0 = P1 = P2 = 0;
P3 = 0xff;
map[x][y] = length;
while (1) {
if(timeNumber - systemCD >= 40 - length) {
switch(direction) {
case 0:
if(x == 0)x = 15;
else x--;
break;
case 1:
if(x == 15)x = 0;
else x++;
break;
case 2:
if(y == 0)y = 15;
else y--;
break;
case 3:
if(y == 15)y = 0;
else y++;
break;
}
if(map[x][y] > 0) break;
else if(map[x][y] == -1) {
length++;
}
else {
for(i = 0; i < 16; i++) {
for(j = 0; j < 16; j++) {
if(map[i][j] > 0)map[i][j]--;
}
}
}
map[x][y] = length;
systemCD = timeNumber;
}
if(timeNumber - foolCD >= 1000) {
map[(timeNumber - systemCD) % 16][(timeNumber - keyCD) % 16] = -1;
foolCD = timeNumber;
}
if(timeNumber - keyCD >= 25) {
if((P3 & 0xf0) != 0xf0) {
if(mKey4 == 0)direction = 0;
else if(mKey5 == 0)direction = 1;
else if(mKey6 == 0)direction = 2;
else if(mKey7 == 0)direction = 3;
keyCD = timeNumber;
}
}
for(i = 0; i < 16; i++) {
LATTICE_LINE_WRITE(temFunction(map[i]), i);
}
showNumber((1000 - timeNumber % 1000)/100);
}
}
void allInit() {
digitInit();
latticeInit();
}
void T0_Time() interrupt 1 {
TH0 = (65535 - 10000) / 256;
TL0 = (65535 - 10000) % 256;
timeNumber++;
}
今天的文章基于51单片机的贪吃蛇游戏设计_经典贪吃蛇小游戏分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/85102.html