Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

MengFanjun的博客

距离比赛还有15天,昨天发愤图强,想做一个练练手,又想到了第十二届的第二批没有写,然后在同专业大佬的帮助下,在一天做完了一届,速度最快的一次了,废话不多说,先看原题 请添加图片描述请添加图片描述 请添加图片描述 请添加图片描述

设计思路

第一眼看真是没什么特别难的感觉,就是一个长按键还没有想到解决方法,先从四个按键入手,S4按键切换三个模式,一开始我用的是递归,在四个while循环,直接把整个程序写死在一个函数里面了,也是迫不得已,当时也没想出什么好方法,S5是电压模式切换,这个就是两个模式切换,简单,S6是读取电压值,一看到保存数据,我直接写了个eeprom,后来一想,没必要那么麻烦,直接保存在一个中间变量进行比较就好,S7也是一样的思路。 到了LED的部分,这个一开始我把所有的LED都写到数码管显示的函数里面了,但是注意了,长按S7是可以关闭LED的,但是关闭LED不影响数码管,这就看出来我的代码有很大问题了,所以要记住,LED一定不要和数码管显示放在一个函数里面。 关于长按怎么做,我这里是大佬教我的,用的定时器2,计时1ms,使变量count_led++,如果count_led=1000后,计时1s,把count_led置为0,然后另一个变量,设一个新变量count_S7每1s自加1,这样的话,定时器中断就设置完毕了,接下来就是S7的长按功能,当S7被按下后,使count_led和count_S7都置0,接下来就进行判断,如果count_S7>=1,判断灯的状态,我们设变量aaa灯亮aaa为0,灯灭aaa为1,如果aaa<1则aaa++,把灯灭掉,else aaa=0,让灯亮,这就实现了长按,看文字描述还是不是很懂,要看代码才行。

源代码

IIC.C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "reg52.h"
#include "intrins.h"

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1


sbit SDA = P2^1;
sbit SCL = P2^0;

void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}

void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}

void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}


void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}


bit IIC_WaitAck(void)
{
bit ackbit;

SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}


void IIC_SendByte(unsigned char byt)
{
unsigned char i;

for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}


unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}

MAIN.C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include<reg51.h>
#include<iic.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sfr AUXR = 0x8e;
sfr T2H = 0xD6;
sfr T2L = 0xD7;
sfr IE2 = 0xAF;
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
unsigned char code shuzi[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0xbf,0xff};
unsigned char code zimu[]={0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0xc7,0x8c,0xc1,0xc8};
unsigned char code num[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};
unsigned char code weizhi[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char ledweizhi[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};
unsigned int count_f = 0;
unsigned char count_t = 0;
unsigned int dat_f = 0;//频率数值
unsigned int dat_term = 0;//周期数值
unsigned char S5_mode=4;
uchar mode;
uint count_led=0;
uint count_S7=0;
uint voltage_temp;//存电压的中间变量
uint fre_temp;//存频率的中间变量
uchar i=0;
uchar j=0;
int voltage_rd1;//rd1的电压值
int dat_rd1;//rd1的数值
int voltage_rb2;//rb2的电压值
int dat_rb2;//rb2的数值
uchar aaa=0;//灯的状态
void led_0();
void Scan_S7();
void led_fun();
void Scan_S4();
void Scan_S5();
void Scan_S6();
void delay_ms(int ms)
{
int q,w;
for(q = 0;q<ms;q++){
for(w=845;w>0;w--);
}
}
void shumaguan_shuzi(uchar a,uchar b)
{
delay_ms(1);
P2=(P2&0X1f)|0xC0;P0=weizhi[a];
P2=(P2&0X1f)|0xE0;P0=shuzi[b];
delay_ms(1);
}
void shumaguan_zimu(uchar a,uchar b)
{
delay_ms(1);
P2=(P2&0X1f)|0xC0;P0=weizhi[a];
P2=(P2&0X1f)|0xE0;P0=zimu[b];
delay_ms(1);
}
void ledlight(uchar x)
{
P2=(P2&0X1f)|0x80;
P0=ledweizhi[x];
}
void Init()
{
R1=R2=R3=R4=1;
}


void Init_Timer()
{
TH0 = 0xff;
TL0 = 0xff;

TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
TMOD = 0x16;
ET0 = 1;
ET1 = 1;
EA = 1;
TR0 = 1;
TR1 = 1;
}

void Timer2(void)
{
T2H = (65535-1000)/256;
T2L = (65535-1000)%256;
EA = 1;
IE2 |=0x04;
AUXR|=0x10;
}
void Service_T12() interrupt 12
{
T2H = (65535-1000)/256;
T2L = (65535-1000)%256;
if(count_led==1000)
{
count_led=0;
count_S7++;
}
else count_led++;
}
void Service_T0() interrupt 1
{
count_f++;
}

void Service_T1() interrupt 3
{
TH1 = (65536 - 50000) / 256;
TL1 = (65536 - 50000) % 256;
count_t++;
if(count_t == 20)
{
dat_f = count_f;
count_f = 0;
count_t = 0;
}
}
void display_f()
{
mode=0;
shumaguan_zimu(0,5);
if(dat_f>9999)
shumaguan_shuzi(3,dat_f/10000);
if(dat_f>999)
shumaguan_shuzi(4,dat_f/1000%10);
if(dat_f>99)
shumaguan_shuzi(5,dat_f/100%10);
if(dat_f>9)
shumaguan_shuzi(6,dat_f/10%10);

shumaguan_shuzi(7,dat_f%10);

}
void display_term()
{
mode=1;

shumaguan_zimu(0,10);
dat_term=1000000/dat_f;
if(dat_f>9999)
shumaguan_shuzi(3,dat_term/1000);
if(dat_f>999)
shumaguan_shuzi(4,dat_term/1000%10);
if(dat_f>99)
shumaguan_shuzi(5,dat_term/100%10);
if(dat_f>9)
shumaguan_shuzi(6,dat_term/10%10);

shumaguan_shuzi(7,dat_term%10);
}
void display_RD1()
{
mode=2;

IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x01);
IIC_WaitAck();
IIC_Stop();

IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat_rd1 = IIC_RecByte();
IIC_WaitAck();
IIC_Stop();


voltage_rd1=dat_rd1*100/51;
shumaguan_zimu(0,9);
shumaguan_shuzi(1,10);
shumaguan_shuzi(2,1);
shumaguan_shuzi(6,voltage_rd1%100/10);
shumaguan_shuzi(7,voltage_rd1%10);
P2=(P2&0X1f)|0xC0;P0=weizhi[5];
P2=(P2&0X1f)|0xFF;P0=shuzi[voltage_rd1/100]+0x80;
}
void display_Rb2()
{
mode=3;

IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();

IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat_rb2 = IIC_RecByte();
IIC_WaitAck();
IIC_Stop();


voltage_rb2=dat_rb2*100/51;
shumaguan_zimu(0,9);
shumaguan_shuzi(1,10);
shumaguan_shuzi(2,3);
shumaguan_shuzi(6,voltage_rb2%100/10);
shumaguan_shuzi(7,voltage_rb2%10);
P2=(P2&0X1f)|0xC0;P0=weizhi[5];
P2=(P2&0X1f)|0xFF;P0=shuzi[voltage_rb2/100]+0x80;
}
void Scan_S4()
{
if(R4==0)
{
while(R4==0){display_term();}
while(1)
{
led_fun();
Scan_S7();
display_term();

if(R4==0)
{

if(R4==0)
{
while(R4==0){display_RD1();}
while(1)
{
led_fun();
Scan_S7();
display_RD1();
Scan_S5();

if(R4==0)
{
delay_ms(60);
if(R4==0)
{
while(R4==0){display_f();}
while(1)
{

led_fun();
Scan_S7();
display_f();

if(R4==0)
{

if(R4==0)
{
while(R4==0){Scan_S4();}
while(1)
{
Scan_S4();
}
}
}
while(R4==0);
}
while(R4==0);
}
}
while(R4==0);
}

while(R4==0);
}
}
while(R4==0);
}
while(R4==0);
}

}
void Scan_S5()
{


if(R3==0)
{
delay_ms(10);
if(R3==0)
{
S5_mode++;
while(R3==0);
}
}
if(S5_mode%2==0)
{
display_RD1();
}
if(S5_mode%2==1)
{
while(1)
{
led_fun();
Scan_S7();
Scan_S6();
display_Rb2();
if(R3==0)
{
delay_ms(10);
if(R3==0)
{
break;
while(R3==0);
}
}
}
}
}
void Scan_S6()
{

if(R2==0)
{
delay_ms(50);
if(R2==0)
{
voltage_temp=voltage_rb2;

while(R2==0);
}
}
}
void Scan_S7()
{
if(R1==0)
{
count_S7=0;
count_led=0;
while(R1==0);
if(count_S7>=1) {if(aaa<1) aaa++;else aaa=0;}
else
{

fre_temp=dat_f;
}
}
}
void led_fun()
{
if(aaa==0)
{
if(mode==0)
{
if(dat_f>fre_temp)
{
P2=(P2&0X1f)|0x80;
P0=0xf9;
}
else ledlight(2);
}
if(mode==1)
{
ledlight(3);
}
if(mode==2)
{
ledlight(4);
}
if(mode==3)
{
if(voltage_rb2>voltage_temp)
{
P2=(P2&0X1f)|0x80;
P0=0xee;
}
else ledlight(4);
}
}
if(aaa==1)
{
P2=0x80;P0=0xff;
}
}

void main()
{
Init_Timer();
Timer2();
Init();
while(1)
{
led_fun();
Scan_S7();
Scan_S6();
Scan_S4();
display_f();

}
}


总体来说,就是长按键那里有点让人懵,剩下的部分还是很简单的。

评论