You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

263 lines
8.3 KiB

C51 COMPILER V9.57.0.0 MAIN 06/14/2022 09:14:54 PAGE 1
C51 COMPILER V9.57.0.0, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN .\Objects\main.obj
COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE Main\main.c LARGE OPTIMIZE(8,SPEED) BROWSE INCDIR(.\Includes) DEBUG OBJE
-CTEXTEND PRINT(.\Listings\main.lst) TABS(2) OBJECT(.\Objects\main.obj)
line level source
1 #include "public.h"
2 #include "key.h"
3 #include "stdio.h"
4 #include "timer.h"
5
6 //定义74HC595控制管脚
7 sbit SRCLK=P3^6; //移位寄存器时钟输入
8 sbit RCLK=P3^5; //存储寄存器时钟输入
9 sbit SER=P3^4; //串行数据输入
10
11 #define LEDDZ_COL_PORT P0 //点阵列控制端口
12
13 u8 death[8]={0x00,0x38,0x6e,0x7c,0x7c,0x6e,0x38,0x00};
14
15 u8 gled_col[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//LED点阵显示图像的列数据
16 u8 map[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
17 u8 step[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
18
19
20 u8 queue_x[65];
21 u8 queue_y[65];
22 u8 leader=0;
23 u8 tail=0;
24 u8 length=0;
25
26 u8 act=0;
27 u8 game_over=0;
28 u8 turn=0;
29
30 u8 direction=0;
31 u8 head_x = 3;
32 u8 head_y = 3;
33
34 u8 apple_x=4;
35 u8 apple_y=4;
36
37 u8 timer = 0;
38
39 u8 i;
40 u8 j;
41 /*******************************************************************************
42 * 函 数 名 : hc595_write_data(u8 dat)
43 * 函数功能 : 向74HC595写入一个字节的数据
44 * 输 入 : dat:数据
45 * 输 出 : 无
46 *******************************************************************************/
47
48 void display(u8 x, u8 y)
49 {
50 1 u8 one = 0x01;
51 1 map[x] = map[x] + (one<<y);
52 1 }
53
54 void del(u8 x, u8 y)
C51 COMPILER V9.57.0.0 MAIN 06/14/2022 09:14:54 PAGE 2
55 {
56 1 u8 one = 0x01;
57 1 map[x] = map[x] - (one<<y);
58 1 }
59
60 void push(u8 x, u8 y)
61 {
62 1 leader++;
63 1 length++;
64 1 leader %= 65;
65 1 queue_x[leader] = x;
66 1 queue_y[leader] = y;
67 1 display(x,y);
68 1 }
69
70 void pop()
71 {
72 1 u8 one = 0x01;
73 1 tail++;
74 1 length--;
75 1 tail %= 65;
76 1 del(queue_x[tail], queue_y[tail]);
77 1 }
78
79
80
81 void hc595_write_data(u8 dat)
82 {
83 1 u8 i=0;
84 1 for(i=0;i<8;i++)//循环8次即可将一个字节写入寄存器中
85 1 {
86 2 SER=dat>>7;//优先传输一个字节中的高位
87 2 dat<<=1;//将低位移动到高位
88 2 SRCLK=0;
89 2 delay_10us(1);
90 2 SRCLK=1;
91 2 delay_10us(1);//移位寄存器时钟上升沿将端口数据送入寄存器中
92 2 }
93 1 RCLK=0;
94 1 delay_10us(1);
95 1 RCLK=1;//存储寄存器时钟上升沿将前面写入到寄存器的数据输出
96 1 }
97
98 void show(u8 gled_row[8])
99 {
100 1 u8 i=0;
101 1 for(i=0;i<8;i++)//循环8次扫描8行、列
102 1 {
103 2 LEDDZ_COL_PORT=gled_col[i];//传送列选数据
104 2 hc595_write_data(gled_row[i]);//传送行选数据
105 2 delay_10us(100);//延时一段时间,等待显示稳定
106 2 hc595_write_data(0x00);//消影
107 2 }
108 1 }
109
110 void monitor_key()
111 {
112 1 u8 key=key_scan(0);
113 1 if(key==KEY1_PRESS)
114 1 {
115 2 turn = 1;
116 2 }
C51 COMPILER V9.57.0.0 MAIN 06/14/2022 09:14:54 PAGE 3
117 1 else if(key==KEY2_PRESS)
118 1 {
119 2 turn = 3;
120 2 }
121 1 else if(key==KEY3_PRESS)
122 1 {
123 2
124 2 }
125 1 else if(key==KEY4_PRESS)
126 1 {
127 2
128 2 }
129 1 }
130
131 void timer0() interrupt 1
132 {
133 1 timer0_reset(0x4c,0x00);
134 1 timer++;
135 1 if(timer==15)
136 1 {
137 2 timer = 0;
138 2 act=1;
139 2 }
140 1 }
141
142 void rand_apple()
143 {
144 1 u8 k;
145 1 u8 rand=TL0%(64-length);
146 1 u8 count = 0;
147 1 u8 over = 0;
148 1 for(i=0;i<8;i++)
149 1 {
150 2 for(j=0;j<8;j++)
151 2 {
152 3 k = tail;
153 3 over = 0;
154 3 while(k != leader){
155 4 k = (k+1)%65;
156 4 if(i == queue_x[k] && j == queue_y[k])
157 4 {
158 5 over = 1;
159 5 }
160 4 }
161 3 if(over == 0)// over == 0 but not run rightly
162 3 {
163 4 if(count == rand)
164 4 {
165 5 apple_x = i;
166 5 apple_y = j;
167 5 return;
168 5 }
169 4 count++;
170 4 }
171 3 }
172 2 }
173 1 }
174
175 void main()
176 {
177 1 u8 i=0;
178 1 timer0_init(0x01,0x4c,0x00);
C51 COMPILER V9.57.0.0 MAIN 06/14/2022 09:14:54 PAGE 4
179 1 push(head_x,head_y);
180 1 rand_apple();
181 1 display(apple_x, apple_y);
182 1 while(1)
183 1 {
184 2 monitor_key();
185 2 if(act && game_over == 0){
186 3 direction += turn;
187 3 direction %= 4;
188 3 head_x += step[direction][0];
189 3 head_y += step[direction][1];
190 3 if(head_x >=8 || head_x < 0 || head_y >= 8 || head_y < 0)
191 3 {
192 4 game_over = 1;
193 4 }
194 3
195 3 i = tail;
196 3 while(i != leader){
197 4 i = (i+1)%65;
198 4 if(head_x == queue_x[i] && head_y == queue_y[i])
199 4 {
200 5 game_over = 1;
201 5 }
202 4 }
203 3
204 3 if(game_over == 0)
205 3 {
206 4 if(head_x == apple_x && head_y == apple_y)
207 4 {
208 5 del(apple_x,apple_y);
209 5 rand_apple();
210 5 display(apple_x,apple_y);
211 5 push(head_x,head_y);
212 5 }
213 4 else
214 4 {
215 5 push(head_x,head_y);
216 5 pop();
217 5 }
218 4 }
219 3 turn = 0;
220 3 act = 0;
221 3 }
222 2 if(game_over)
223 2 {
224 3 show(death);
225 3 }
226 2 else
227 2 {
228 3 show(map);
229 3 }
230 2 }
231 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1063 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 177 7
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
C51 COMPILER V9.57.0.0 MAIN 06/14/2022 09:14:54 PAGE 5
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)