第七届的赛题比较容易,1~2h即可完成。当然还有一个小问题:当读取DS18B20时,数码管会轻微闪烁。其实有两种解决办法:

  • 在延迟函数中添加显示函数,但是会影响18B20的通信延迟。
  • 通过中断按位刷新数码管,由于需要重构显示函数所以将会在下篇介绍。

题目要求

模拟风扇控制系统,题目可与此处下载


注意事项

  • DS18B20读取时数码管会轻微闪烁,需要在定时器中按位刷新数码管。
  • 注意if和else if的使用,判断是否真的存在并列关系(这个错误找了好久)。

实例

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
#include<stc15f2k60s2.h>
#include"onewire.h"
typedef unsigned char uchar;
typedef unsigned int uint;
uchar code dig_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6};
//三种模式下的倒计时间隔
uchar time[3];
//定义初始变量:长按、触发、工作模式、pwm生成标志位、温度显示标志位、按键标志位、温度读取标志位、pwm占空比
uchar cont=0,trg=0,mode=1,flag_pwm=1,flag_temp=0,flag_key=0,flag_get_temp=0,pwm=1;
uint temp=0;
sbit SIG=P3^4;
void select(uchar channel)
{
switch(channel)
{
case 4:
P2=(P2&0x1f)|0x80;
break;
case 5:
P2=(P2&0x1f)|0xa0;
break;
case 6:
P2=(P2&0x1f)|0xc0;
break;
case 7:
P2=(P2&0x1f)|0xe0;
break;
case 0:
P2=(P2&0x1f);
break;
}
}

void init_sys()
{
select(4);
P0=0xff;
select(5);
P0=0x00;
select(0);
}

void delay(uint t)
{
while(t--);
}

void display(uchar pos,uchar num)
{
select(6);
P0=0x01<<pos;
select(7);
P0=dig_code[num];
delay(1000);
P0=0xff;
select(0);
}

void scan_key()
{
uchar dat=P3^0xff;
trg=dat&(dat^cont);
cont=dat;
}

void key_fun()
{
if(trg==0x01)
{
mode++;
if(mode>=4)
{
mode=1;
}
}
if(trg==0x02)
{
if(flag_key==0)
{
time[mode-1]=0;
flag_key=1;
}
else if(flag_key==1)
{
time[mode-1]=60;
flag_key=2;
}
else if(flag_key==2)
{
time[mode-1]=120;
flag_key=0;
}
flag_pwm=1;
}
if(trg==0x04)
{
time[mode-1]=0;
flag_pwm=0;
}
if(trg==0x08)
{
flag_temp=~flag_temp;
}
}

void display_data()
{
if(flag_temp)
{
display(0,10);
display(1,4);
display(2,10);
display(3,11);
display(4,11);
display(5,temp/10);
display(6,temp%10);
display(7,12);
}
else
{
display(0,10);
display(1,mode);
display(2,10);
display(3,11);
display(4,0);
display(5,time[mode-1]/100);
display(6,time[mode-1]%100/10);
display(7,time[mode-1]%10);
}
}


void get_temp()
{
uchar LSB,MSB;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LSB=Read_DS18B20();
MSB=Read_DS18B20();
init_ds18b20();
temp=(MSB<<8)|LSB;
temp=temp>>4;
}

void set_pwm()
{
if(mode==1)
{
pwm=2; //睡眠风
}
else if(mode==2)
{
pwm=3; //自然风
}
else if(mode==3)
{
pwm=7; //常风
}
}

void set_led()
{
if(mode==1&&flag_pwm)
{
select(4);
P0=0xfe;
select(0);
}
else if(mode==2&&flag_pwm)
{
select(4);
P0=0xfd;
select(0);
}
else if(mode==3&&flag_pwm)
{
select(4);
P0=0xfb;
select(0);
}
else if(!flag_pwm) //如果停止工作
{
select(4);
P0=0xff;
select(0);
}
}

void init_timer0(void) //100微秒@11.0592MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xA4; //设置定时初值
TH0 = 0xFF; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA = 1;
}

void ser_timer0(void) interrupt 1
{
static uint count_50ms=0,count_1s=0;
static uchar count=0;
count++;
count_50ms++;
count_1s++;
if(flag_pwm)
{
if(count<=pwm)
{
SIG=1;
}
else if(count<10)
{
SIG=0;
}
else if(count==10)
{
count=0;
}
}
if(count_50ms==500) //50ms时扫描按键,执行按键功能,设置占空比
{
count_50ms=0;
scan_key();
key_fun();
set_pwm();
}
if(count_1s==10000) //1s时读取温度,递减当前定时器
{
count_1s=0;
flag_get_temp=~flag_get_temp;
if(flag_pwm) //如果正在运行
{
time[mode-1]=time[mode-1]-1;
if(time[mode-1]<=0) //如果时间减少到0s则停止pwm波的生成
{
flag_pwm=0;
}
else if(time[mode-1]>120) //如果时间大于120s则将数据非法归零
{
time[mode-1]=0;
}
}
}
}


void main()
{
init_sys();
init_timer0();
while(1)
{
display_data();
set_led();
if(flag_get_temp)
{
get_temp();
}
}
}