代码
1 #include <reg52.h>
2
3 sbit Diola=P2^5;
4 sbit Dula=P2^6;
5 sbit Wela=P2^7;
6
7
8 typedef unsigned int uint;
9 typedef unsigned char uchar;
10 typedef void (*fun)(void*);
11 struct funNode;
12 typedef struct funNode funNode;
13 struct funNode{
14 fun me;
15 uint interval;
16 void* param;
17 uint val;
18 funNode* next;
19 };
20
21
22 funNode* head;
23 uint Delay=0;
24
25 void setInterval(funNode* fnode){
26 funNode* node=head;
27 funNode* newNode=fnode;
28 //find the final node
29 if(node){
30 while(node->next){
31 node=node->next;
32 }
33 //link the new node
34 node->next=newNode;
35 }else{
36 //let the new node be the head
37 head=newNode;
38 }
39 newNode->val=newNode->interval;
40 newNode->next=0;
41 }
42
43 void cleanInterval(funNode* fnode){
44 funNode* node=head;
45 funNode* preNode=0;
46 if(!node)goto CleanIntervalEnd;
47 while(node!=fnode){
48 if(node->next){
49 preNode=node;
50 node=node->next;
51 }else{
52 goto CleanIntervalEnd;
53 }
54 }
55 //the first one is going to remove
56 if(!preNode){
57 head=node->next;
58 }else{
59 preNode->next=node->next;
60 }
61 CleanIntervalEnd:
62 {}
63 }
64
65
66 void initT0(){
67 TH0=(65536-1000)/256;
68 TL0=(65536-1000)%256;
69 EA=1;
70 ET0=1;
71 TR0=1;
72 TMOD=0x01;
73 }
74
75
76 void delay(uint ms){
77 Delay=ms;
78 while(Delay>0);
79 }
80
81 void time0() interrupt 1{
82 funNode* node=head;
83 if(Delay>0){
84 --Delay;
85 }
86 while(node){
87 if(node->val>0){
88 --node->val;
89 }else{
90 node->me(node->param);
91 node->val=node->interval;
92 }
93 node=node->next;
94 }
95
96 TH0=(65536-1000)/256;
97 TL0=(65536-1000)%256;
98 }
99
100
101 void init(){
102 initT0();
103
104 P0=0xff;
105 Wela=1;
106 Wela=0;
107 P0=0x00;
108 Diola=1;
109 Diola=0;
110 }
111 uchar code dTable[]={
112 1,2,4,8,
113 16,32,64,128
114 };
115
116 uchar showInfo[]={
117 0x00,0x00,0x00,0x00,
118 0x00,0x00,0x00,0x00
119 };
120
121 uchar direct='o';
122 bit light=1;
123 uchar r,c;
124
125 void setBit(uchar* base,uchar d,bit val){
126 uchar temp=0;
127 if(val){
128 temp=dTable[d];
129 temp=(*base)|temp;
130 }else{
131 temp=~dTable[d];
132 temp=(*base)&temp;
133 }
134 *base=temp;
135 }
136
137 void goNext(void* p){
138
139 switch(direct){
140 case 'o':
141 setBit(&showInfo[0],0,light);
142 break;
143 case 'u':
144 setBit(&showInfo[--r],c,light);
145 break;
146 case 'd':
147 setBit(&showInfo[++r],c,light);
148 break;
149 case 'l':
150 setBit(&showInfo[r],--c,light);
151 break;
152 case 'r':
153 setBit(&showInfo[r],++c,light);
154 break;
155 }
156
157 switch(r*10+c){
158 case 0:
159 direct='r';
160 break;
161 case 7:
162 direct='d';
163 break;
164 case 77:
165 direct='l';
166 break;
167 case 70:
168 direct='u';
169 break;
170 case 10:
171 direct='r';
172 break;
173 case 16:
174 direct='d';
175 break;
176 case 66:
177 direct='l';
178 break;
179 case 61:
180 direct='u';
181 break;
182 case 21:
183 direct='r';
184 break;
185 case 25:
186 direct='d';
187 break;
188 case 55:
189 direct='l';
190 break;
191 case 52:
192 direct='u';
193 break;
194 case 32:
195 direct='r';
196 break;
197 case 34:
198 direct='d';
199 break;
200 case 44:
201 direct='l';
202 break;
203 case 43:
204 direct='o';
205 r=0;
206 c=0;
207 light=(light==1?0:1);
208 break;
209 }
210
211 }
212
213 void showRow(uchar row,uchar show){
214 P0=0;
215 Diola=1;
216 Diola=0;
217
218 P0=show;
219 Dula=1;
220 Dula=0;
221
222 P0=dTable[row];
223 Diola=1;
224 Diola=0;
225
226 delay(1);
227 }
228
229 void show(){
230 uchar i=0;
231 while(1){
232 for(i=0;i<8;++i){
233 showRow(i,~showInfo[i]);
234 }
235 }
236 }
237
238 void main(){
239 funNode f;
240 init();
241 f.me=goNext;
242 f.interval=50;
243 f.param=0;
244 setInterval(&f);
245 show();
246 cleanInterval(&f);
247 }
248