一、功能、总体方案设计

1.1 功能分析

1、系统能实时显示,环境的温度和风扇的档数,具有自动模式与手动模式;
2、自动模式能检测是否有人控制风扇的启动或停止,并在无人为控制的情况下根据环境温度自动调节风扇的档数(转速),温度的上下限可以通过按键进行设置;
3、手动模式通过按键分5档控制风扇转速;
4、手动模式利用手机APP通过蓝牙模块与单片机通信,实现远程遥控调节风扇的档位和自动/手动工作模式的切换。

1.2 总体方案设计

本设计的整体思路是:利用温度传感器DS18B20来检测环境温度,并直接输出数字温度给51单片机进行处理,并将实时温度、设置温度、风扇转速显示在0.91寸OLED显示屏上。检测到的环境温度可以精确到小数点后一位。本系统能够人为切换自动手动来控制风扇转速。切换自动手动模式、自动控制时控制转速的温度上下限也可通过单片机上的按键进行修改,此外本系统还配备一个HC-05蓝牙模块,可通过装有蓝牙app的手机进行远程控制。设计简单经济、准确性高。总体设计如下图:
总体设计

二、系统硬件电路设计

2.1 CHD6012ES-AH 12V 直流风扇

风扇
风扇的两个正极连接在电压转化模块上的12V针脚,风扇的两个负极连接在J47的OUT1,OUT2口并通过开发板上的ULN2003D大电流达林顿陈列来进行驱动。(电压转化模块的地应与单片机开发板的地连接)

2.2 HC-05蓝牙模块

蓝牙
将蓝牙模块的TX,RX引脚分别连接单片机的RX,TX。以及将GND和VCC连接对应单片机的地与电源接口。程序运行时用蓝牙app发送数据给蓝牙模块并通过串行中断控制单片机接收对应数据进行处理。

2.3 温度传感器DS18B20

温度传感器
DS18B20可收集环境温度数据。将DS18B20插入J12,并通过单总线的方式与单片机进行通信。时序如下两图:
DS18B20初始化时序
DS18B20写时序
DS18B20读时序

2.4 0.91寸 OLED显示屏

OLED显示屏
0.91寸 OLED显示屏通过P2.6,P2.7引脚与单片机连接,单片机通过IIC总线的方式传输数据给显示屏显示出相应的文字,数据。时序如下两图:
IIC初始化时序图
IIC传输数据时序图

三、系统软件设计

蓝牙模块程序

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
void UsartConfiguration()
{
SCON=0X50;//SCON 是一个特殊功能寄存器,用以设定串行口的工作方式、接收/发送控制以及设置状态标志
TMOD=0X21;
PCON=0X00;
TH1=0Xfd;
TL1=0Xfd; //波特率为9600算出来的
TR1= 1;
ES = 1;
EA = 1;
}
//中断程序本人放在了main.c里面,方便变量调用:
void Com_Int(void) interrupt 4
{
uint8 receive_data;
EA = 0;
if(RI == 1)
{
RI=0;
receive_data = SBUF;
switch(receive_data)
{
case ('1'):speed++;if(speed==5)speed=4;break;
case ('2'):speed--;if(speed==-1)speed=0;break;
case ('3'):if(modeflag==0)modeflag=1;else modeflag=0;break;
case ('4'):if(openflag==0)openflag=1;else openflag=0;break;
case ('5'):if(!openflag)shangxian+=1;break;
case ('6'):if(!openflag)shangxian-=1;break;
case ('7'):if(!openflag)xiaxian+=1;break;
case ('8'):if(!openflag)xiaxian-=1;break;
case ('9'):if(wenduflag==0)wenduflag=1;else wenduflag=0;break;
}
}
EA = 1;
}

DS18B20模块程序

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
#ifndef	__DS18B20_H
#define __DS18B20_H
#include <reg51.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;

void ds18b20init();
void ds18b20wr(uint8 dat);
void display();
uint8 ds18b20rd();
uint8 readtemp();
sbit dq=P3^7;
extern uint8 a,temp;
#endif



//DS18B20.c:
#include "ds18b20.h"
#include "delay.h"
void ds18b20init() //18b20的初始化
{
dq=1;
delay(1);
dq=0;
delay(80);
dq=1;
delay(5);
dq=0;
delay(20);
dq=1;
delay(35);
}
void ds18b20wr(uint8 dat) //18b20写数据
{
uint8 i;
for(i=0;i<8;i++)
{
dq=0;
dq=dat&0x01;
dat>>=1;
delay(8);//在时序上只有这一块对时序要求最准确,他的时间必须大于15us
dq=1;
delay(1);
}
}
uint8 ds18b20rd() //18b20读数据
{
uint8 value,i;
for(i=0;i<8;i++)
{
dq=0;
value>>=1;
dq=1;
if(dq==1)
value|=0x80;
delay(8);//在这一块也对时间要求特别准确,整段程序必须大于60us
}
return value;
}
uint8 readtemp() //读取温度内需要复位的
{
uint8 b;
ds18b20init(); //初始化
ds18b20wr(0xcc); //发送忽略ROM指令
ds18b20wr(0x44); //发送温度转换指令
delay(100);
ds18b20init(); //初始化
ds18b20wr(0xcc); //发送忽略ROM指令
ds18b20wr(0xbe); //发读暂存器指令
a=ds18b20rd(); //温度的低八位
b=ds18b20rd(); //温度的高八位
b<<=4; //ssss s***;s为标志位s=0表示温度值为正数,s=1温度值为负数
//c=b&0x80; //温度正负标志位确认
b+=(a&0xf0)>>4;
a=a&0x0f; //温度的小数部分
return b;
}

OLED模块程序

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
400
401
402
403
404
405
406
407
408
//oled.h:

#ifndef __OLED_H
#define __OLED_H
#include<reg51.h>
#include "delay.h"
#include "intrins.h"
#define u16 unsigned int
#define u8 unsigned char
#define u32 unsigned long
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
#define OLED_MODE 0

sbit OLED_SCL=P2^6;//时钟 D0(SCLK?
sbit OLED_SDIN=P2^7;//D1(MOSI) 数据

#define OLED_CS_Clr() OLED_CS=0
#define OLED_CS_Set() OLED_CS=1

#define OLED_RST_Clr() OLED_RST=0
#define OLED_RST_Set() OLED_RST=1

#define OLED_DC_Clr() OLED_DC=0
#define OLED_DC_Set() OLED_DC=1

#define OLED_SCLK_Clr() OLED_SCL=0
#define OLED_SCLK_Set() OLED_SCL=1

#define OLED_SDIN_Clr() OLED_SDIN=0
#define OLED_SDIN_Set() OLED_SDIN=1
#define Max_Column 128
//-----------------OLED端口定义----------------

//OLED控制用函数
void OLED_WR_Byte(unsigned dat,unsigned cmd);

void OLED_Init(void);
void OLED_Clear(void);
void OLED_DrawPoint(u8 x,u8 y,u8 t);
void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot);
void OLED_ShowChar(u8 x,u8 y,u8 chr);
void OLED_ShowString(u8 x,u8 y, u8 *p);
void OLED_ShowCHinese(u8 x,u8 y,u8 no);
void OLED_Set_Pos(u8 x, u8 y);

void IIC_Start();
void IIC_Stop();
void Write_IIC_Command(u8 IIC_Command);
void Write_IIC_Data(u8 IIC_Data);
void Write_IIC_Byte(u8 IIC_Byte);
void IIC_Wait_Ack();

#endif







//oled.c:
#include "oled.h"
#include "oledfont.h"
#include "intrins.h"
#include "delay.h"
void IIC_Start()
{
OLED_SCLK_Set();
OLED_SDIN_Set();
OLED_SDIN_Clr();
OLED_SCLK_Clr();
}

void IIC_Stop()
{
OLED_SCLK_Set() ;
OLED_SDIN_Clr();
OLED_SDIN_Set();
}
void IIC_Wait_Ack()
{
OLED_SCLK_Set() ;
OLED_SCLK_Clr();
}
void Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
unsigned char m,da;
da=IIC_Byte;
OLED_SCLK_Clr();
for(i=0;i<8;i++)
{
m=da;
m=m&0x80;
if(m==0x80)
{
OLED_SDIN_Set();
}
else
OLED_SDIN_Clr();
da=da<<1;
OLED_SCLK_Set();
OLED_SCLK_Clr();
}
}
void Write_IIC_Command(unsigned char IIC_Command)
{
IIC_Start();
Write_IIC_Byte(0x78); //Slave address,SA0=0
IIC_Wait_Ack();
Write_IIC_Byte(0x00); //write command
IIC_Wait_Ack();
Write_IIC_Byte(IIC_Command);
IIC_Wait_Ack();
IIC_Stop();
}
void Write_IIC_Data(unsigned char IIC_Data)
{
IIC_Start();
Write_IIC_Byte(0x78); //D/C#=0; R/W#=0
IIC_Wait_Ack();
Write_IIC_Byte(0x40); //write data
IIC_Wait_Ack();
Write_IIC_Byte(IIC_Data);
IIC_Wait_Ack();
IIC_Stop();
}
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
if(cmd)
{
Write_IIC_Data(dat);
}
else
{
Write_IIC_Command(dat);
}
}
//坐标设置
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f),OLED_CMD);
}
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<4;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte (0x00,OLED_CMD); //设置显示位置—列低地址
OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
} //更新显示
}
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//void OLED_ShowChar(u8 x,u8 y,u8 chr)
void OLED_ShowChar(u8 x,u8 y,u8 chr)
{
unsigned char c=0,i=0;
c=chr-' ';//得到偏移后的值
if(x>Max_Column-1){x=0;y=y+2;}
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
}
//显示一个字符号串
void OLED_ShowString(u8 x,u8 y,u8 *chr)
{
unsigned char j=0;
while (chr[j]!='\0')
{
OLED_ShowChar(x,y,chr[j]);
x+=8;
if(x>120){x=0;y+=2;}
j++;
}
}
//显示汉字
void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{
u8 t,adder=0;
OLED_Set_Pos(x,y);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
adder+=1;
}
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
adder+=1;
}
}
//初始化SSD1306
void OLED_Init(void)
{
OLED_WR_Byte(0xAE,OLED_CMD);//关闭显示
OLED_WR_Byte(0x40,OLED_CMD);//---set low column address
OLED_WR_Byte(0xB0,OLED_CMD);//---set high column address

OLED_WR_Byte(0xC8,OLED_CMD);//-not offset
OLED_WR_Byte(0x81,OLED_CMD);//设置对比度
OLED_WR_Byte(100,OLED_CMD);
OLED_WR_Byte(0xa1,OLED_CMD);//段重定向设置
OLED_WR_Byte(0xa6,OLED_CMD);//
OLED_WR_Byte(0xa8,OLED_CMD);//设置驱动路数
OLED_WR_Byte(0x1f,OLED_CMD);
OLED_WR_Byte(0xd3,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xd5,OLED_CMD);
OLED_WR_Byte(0xf0,OLED_CMD);
OLED_WR_Byte(0xd9,OLED_CMD);
OLED_WR_Byte(0x22,OLED_CMD);
OLED_WR_Byte(0xda,OLED_CMD);
OLED_WR_Byte(0x02,OLED_CMD);
OLED_WR_Byte(0xdb,OLED_CMD);
OLED_WR_Byte(0x49,OLED_CMD);
OLED_WR_Byte(0x8d,OLED_CMD);
OLED_WR_Byte(0x14,OLED_CMD);
OLED_WR_Byte(0xaf,OLED_CMD);
OLED_Clear();
}







//Oledfont.h:(字库里的模板本人使用PCtoLCD2002进行编辑生成。)
#ifndef __OLEDFONT_H
#define __OLEDFONT_H
//常用ASCII表
//偏移量32
//ASCII字符集
//偏移量32
//大小:8x16
/****************************************8*16的点阵************************************/
unsigned char code F8X16[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};
unsigned char code Hzk[][25]={

//温(0) 度(1) 转(2) 速(3) 风(4) 扇(5) 控(6) 制(7) °(8) 模(9) 式(10) 自(11) 手(12) 动(13) 最(14) 高(15) 低(16)

{0x10,0x60,0x02,0x8C,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00},
{0x04,0x04,0x7E,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00},/*"温",0*/

{0x00,0x00,0xFC,0x24,0x24,0x24,0xFC,0x25,0x26,0x24,0xFC,0x24,0x24,0x24,0x04,0x00},
{0x40,0x30,0x8F,0x80,0x84,0x4C,0x55,0x25,0x25,0x25,0x55,0x4C,0x80,0x80,0x80,0x00},/*"度",1*/

{0xC8,0xB8,0x8F,0xE8,0x88,0x88,0x40,0x48,0x48,0xE8,0x5F,0x48,0x48,0x48,0x40,0x00},
{0x08,0x18,0x08,0xFF,0x04,0x04,0x00,0x02,0x0B,0x12,0x22,0xD2,0x0A,0x06,0x00,0x00},/*"转",2*/

{0x40,0x40,0x42,0xCC,0x00,0x04,0xF4,0x94,0x94,0xFF,0x94,0x94,0xF4,0x04,0x00,0x00},
{0x00,0x40,0x20,0x1F,0x20,0x48,0x44,0x42,0x41,0x5F,0x41,0x42,0x44,0x48,0x40,0x00},/*"速",3*/

{0x00,0x00,0xFE,0x02,0x12,0x22,0xC2,0x02,0xC2,0x32,0x02,0xFE,0x00,0x00,0x00,0x00},
{0x80,0x60,0x1F,0x00,0x20,0x10,0x0C,0x03,0x0C,0x30,0x00,0x0F,0x30,0x40,0xF8,0x00},/*"风",4*/

{0x00,0x00,0xFC,0x24,0x24,0x24,0x25,0x26,0x24,0x24,0x24,0x24,0x24,0x3C,0x00,0x00},
{0x40,0x30,0x0F,0x21,0x15,0x49,0x81,0x7F,0x00,0x21,0x15,0x49,0x81,0x7F,0x00,0x00},/*"扇",5*/

{0x10,0x10,0x10,0xFF,0x90,0x20,0x98,0x48,0x28,0x09,0x0E,0x28,0x48,0xA8,0x18,0x00},
{0x02,0x42,0x81,0x7F,0x00,0x40,0x40,0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x40,0x00},/*"控",6*/

{0x40,0x50,0x4E,0x48,0x48,0xFF,0x48,0x48,0x48,0x40,0xF8,0x00,0x00,0xFF,0x00,0x00},
{0x00,0x00,0x3E,0x02,0x02,0xFF,0x12,0x22,0x1E,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00},/*"制",7*/

/*{0x00,0x40,0x42,0x44,0x58,0x40,0x40,0x7F,0x40,0x40,0x50,0x48,0xC6,0x00,0x00,0x00},
{0x00,0x40,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00},/*"当",8*/

/*{0x08,0x08,0xE8,0x29,0x2E,0x28,0xE8,0x08,0x08,0xC8,0x0C,0x0B,0xE8,0x08,0x08,0x00},
{0x00,0x00,0xFF,0x09,0x49,0x89,0x7F,0x00,0x00,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00},/*"前",9*/

{0x00,0x00,0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"°",8*/

{0x10,0x10,0xD0,0xFF,0x90,0x14,0xE4,0xAF,0xA4,0xA4,0xA4,0xAF,0xE4,0x04,0x00,0x00},
{0x04,0x03,0x00,0xFF,0x00,0x89,0x4B,0x2A,0x1A,0x0E,0x1A,0x2A,0x4B,0x88,0x80,0x00},/*"模",9*/

{0x10,0x10,0x90,0x90,0x90,0x90,0x90,0x10,0x10,0xFF,0x10,0x10,0x11,0x16,0x10,0x00},
{0x00,0x20,0x60,0x20,0x3F,0x10,0x10,0x10,0x00,0x03,0x0C,0x10,0x20,0x40,0xF8,0x00},/*"式",10*/

{0x00,0x00,0x00,0xF8,0x88,0x8C,0x8A,0x89,0x88,0x88,0x88,0xF8,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0xFF,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00,0x00},/*"自",11*/

{0x00,0x00,0x24,0x24,0x24,0x24,0x24,0xFC,0x22,0x22,0x22,0x23,0x22,0x00,0x00,0x00},
{0x02,0x02,0x02,0x02,0x02,0x42,0x82,0x7F,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00},/*"手",12*/

{0x40,0x44,0xC4,0x44,0x44,0x44,0x40,0x10,0x10,0xFF,0x10,0x10,0x10,0xF0,0x00,0x00},
{0x10,0x3C,0x13,0x10,0x14,0xB8,0x40,0x30,0x0E,0x01,0x40,0x80,0x40,0x3F,0x00,0x00},/*"动",13*/

{0x40,0x40,0xC0,0x5F,0x55,0x55,0xD5,0x55,0x55,0x55,0x55,0x5F,0x40,0x40,0x40,0x00},
{0x20,0x60,0x3F,0x25,0x15,0x15,0xFF,0x90,0x47,0x29,0x11,0x2D,0x43,0x80,0x80,0x00},/*"最",14*/

{0x04,0x04,0x04,0x04,0xF4,0x94,0x95,0x96,0x94,0x94,0xF4,0x04,0x04,0x04,0x04,0x00},
{0x00,0xFE,0x02,0x02,0x7A,0x4A,0x4A,0x4A,0x4A,0x4A,0x7A,0x02,0x82,0xFE,0x00,0x00},/*"高",15*/

{0x00,0x80,0x60,0xF8,0x07,0x00,0xFC,0x84,0x84,0x84,0xFE,0x82,0x83,0x82,0x80,0x00},
{0x01,0x00,0x00,0xFF,0x00,0x00,0xFF,0x40,0x20,0x00,0x41,0x8E,0x30,0x40,0xF8,0x00},/*"低",16*/

};
#endif

delay函数

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
//delay.h:
#ifndef __DELAY_H
#define __DELAY_H

#include "intrins.h"
void delay_ms(unsigned int ms);
void Delay1ms(void); //@11.0592MHz
void delay(unsigned int i);
#endif



//delay.c:
#include "delay.h"
void delay_ms(unsigned int ms)
{
while(ms--)
{
Delay1ms();
}
}

void Delay1ms(void)
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}

void delay(unsigned int i)
{
while(i--);
}

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
#include <reg51.h>
#include <intrins.h>
#include "oled.h"
#include "ds18b20.h"
#include "delay.h"
#include "lanya.h"

uint8 num[10]="0123456789";
uint8 a,temp;
uint8 time,j,k;
uint8 modeflag=1,openflag=1,wenduflag=0;
uint8 T=20,zk=19;
int speed=0;
uint8 shangxian=35,xiaxian=18;

sbit S2=P3^1;
sbit S3=P3^0;
sbit S4=P3^2;
sbit S5=P3^3;
sbit moto=P1^0; //直流电机
sbit moto2=P1^1;

void open()
{
speed=0;
if(openflag==0)
openflag=1;
else
openflag=0;
}

void Com_Int(void) interrupt 4
{
uint8 receive_data;
EA = 0;
if(RI == 1)
{
RI=0;
receive_data = SBUF;
switch(receive_data)
{
case ('1'):speed++;if(speed==5)speed=4;break;
case ('2'):speed--;if(speed==-1)speed=0;break;
case ('3'):if(modeflag==0)modeflag=1;else modeflag=0;break;
case ('4'):if(openflag==0)openflag=1;else openflag=0;break;
case ('5'):if(!openflag)shangxian+=1;break;
case ('6'):if(!openflag)shangxian-=1;break;
case ('7'):if(!openflag)xiaxian+=1;break;
case ('8'):if(!openflag)xiaxian-=1;break;
case ('9'):if(wenduflag==0)wenduflag=1;else wenduflag=0;break;
}
}
EA = 1;

}

timer0() interrupt 1
{
TH0=(65536-1000*12/12)/256;
TL0=(65536-1000*12/12)%256;
switch(speed)
{
case 0:zk=20;break;
case 4:zk=19;break;
case 3:zk=13;break;
case 2:zk=10; break;
case 1:zk=6; break;
}
if(++time==zk)
{
moto=0;
moto2=0;
}
else if(time==T)
{
time=0;
moto=1;
moto2=1;
}
}

void display()
{
uint16 k;
float dio;
dio=a*0.0625;
k=dio*10000;//取小数点后两位有效数字
if(openflag)
{
//OLED_ShowCHinese(0,0,8); //当前
//OLED_ShowCHinese(16,0,9);
OLED_ShowCHinese(0,0,0);
OLED_ShowCHinese(16,0,1);
OLED_ShowChar(32,0,':');
OLED_ShowChar(40,0,num[temp%100/10]);
OLED_ShowChar(48,0,num[temp%100%10]);
OLED_ShowChar(56,0,'.');
OLED_ShowChar(64,0,num[k/1000]);
OLED_ShowChar(72,0,num[k%1000/100]);
OLED_ShowCHinese(80,0,8);
OLED_ShowChar(88,0,'C');

//OLED_ShowCHinese(0,2,8); //当前
//OLED_ShowCHinese(16,2,9);
OLED_ShowCHinese(0,2,2);
OLED_ShowCHinese(16,2,3);
OLED_ShowChar(32,2,':');
OLED_ShowChar(40,2,num[speed]);
OLED_ShowCHinese(50,2,9);
OLED_ShowCHinese(66,2,10);
OLED_ShowChar(82,2,':');
if(modeflag==1)
OLED_ShowCHinese(90,2,11);
else
OLED_ShowCHinese(90,2,12);
OLED_ShowCHinese(106,2,13);
}
else if(wenduflag==0)
{
OLED_ShowCHinese(0,0,14);
OLED_ShowCHinese(16,0,15);
OLED_ShowCHinese(32,0,0);
OLED_ShowCHinese(48,0,1);
OLED_ShowChar(64,0,':');
OLED_ShowChar(0,2,num[shangxian/10]);
OLED_ShowChar(8,2,num[shangxian%10]);
}
else if(wenduflag==1)
{
OLED_ShowCHinese(0,0,14);
OLED_ShowCHinese(16,0,16);
OLED_ShowCHinese(32,0,0);
OLED_ShowCHinese(48,0,1);
OLED_ShowChar(64,0,':');
OLED_ShowChar(0,2,num[xiaxian/10]);
OLED_ShowChar(8,2,num[xiaxian%10]);
}

}


void main()
{
TMOD=0x01;
TH0=(65536-1000*12/12)/256;
TL0=(65536-1000*12/12)%256;
ET0=1;
EA=1;
TR0=1;
moto=0;
OLED_Init();delay(3);
OLED_Clear();
OLED_ShowCHinese(32,0,4);
OLED_ShowCHinese(48,0,5);
OLED_ShowCHinese(80,0,6);
OLED_ShowCHinese(96,0,7);
OLED_ShowString(32,2," by JAS");
delay(1000);
OLED_Clear();
ds18b20init();
UsartConfiguration();
while(1)
{
display();
while(openflag)
{
temp=readtemp();
if(S5==0)
{
delay(20);
if(S5==0)
{
while(!S5);
open();
}
}
display();
if(S4==0)
{
delay(20);
if(S4==0)
{
while(!S4);
if(modeflag==0)
modeflag=1;
else
modeflag=0;
}
}
if(modeflag==0)
{
if(S2==0)
{
delay(20);
if(S2==0)
{
while(!S2);
speed++;
if(speed==5)
speed=4;
}
}
if(S3==0)
{
delay(20);
if(S3==0)
{

while(!S3);
speed--;
if(speed==-1)
speed=0;
}
}
}
if(modeflag==1)
{
if(temp>xiaxian && temp<=22) //速度0
speed=0;
if(temp>22 && temp<=23) //速度1
speed=1;
if(temp>23 && temp<=24) //速度2
speed=2;
if(temp>24 && temp<=25) //速度3
speed=3;
if(temp>25 && temp<shangxian) //速度4
speed=4;
}
}
OLED_Clear();
while(!openflag)
{
if(S5==0)
{
delay(20);
if(S5==0)
{
while(!S5);
open();
}
}
display();
if(S4==0)
{
delay(20);
if(S4==0)
{
while(!S4);
if(wenduflag==0)
wenduflag=1;
else
wenduflag=0;
}
}
if(wenduflag==0)
{
if(S2==0)
{
delay(20);
if(S2==0)
{
while(!S2);
shangxian+=1;
}
}
if(S3==0)
{
delay(20);
if(S3==0)
{
while(!S3);
shangxian-=1;
}
}
}
if(wenduflag==1)
{
if(S2==0)
{
delay(20);
if(S2==0)
{
while(!S2);
xiaxian+=1;
}
}
if(S3==0)
{
delay(20);
if(S3==0)
{
while(!S3);
xiaxian-=1;
}
}
}
}
}
}

四、系统运行结果

本次实训是利用单片机开发板将程序下载到STC89C516芯片,然后利用芯片进行实物调试。以下是调试结果:
蓝牙调试过程
系统调试过程
系统调试过程

五、总结

本次实训的系统以51单片机为控制核心,以温度传感器DS18B20检测环境温度、0.91寸 OLED显示屏显示数据、以及HC-05 蓝牙模块控制系统的工作方式,实现了根据所接收的数据进行智能风扇控制系统的功能控制。因此实现了基于51单片机的智能风扇控制系统的设计。
通过这几天的单片机的实训,我在原有的单片机基础上更深刻的掌握了单片机的深层内容及实际生活中的应用,实训锻炼了自己动手能力和思维能力,还有在软件方面的编程能力,让我受益匪浅。但与此同时,实训的过程中我也发现自己对于许多基础问题还是没有掌握好,甚至刚开始还让直流风扇直接连接单片机的IO口上。还有这次我接触到了许多在以往单片机学习中没有接触到的东西,比如蓝牙模块,OLED显示屏等等。各种大大小小问题的发现将为我以后的学习和工作找明道路,查漏补缺为进一步学习作好准备。此次实训让我学会了如何制作较为复杂的单片机系统,希望这些经验也可以让我在今后的日常生活中灵活运用。