📄 18b20.lst
字号:
C51 COMPILER V7.06 18B20 01/15/2009 14:58:48 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE 18B20
OBJECT MODULE PLACED IN 18b20.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE 18b20.c BROWSE DEBUG OBJECTEXTEND
stmt level source
1 //18B20单线温度检测的应用样例程序
2 #include<REG52.H>
3 #include<math.h>
4 #include<INTRINS.H>
5
6 #define uchar unsigned char
7 #define uint unsigned int;
8 /*****************************************************************************/
9 sbit seg1=P2^0;
10 sbit seg2=P2^1;
11 sbit seg3=P2^2;
12 sbit DQ=P1^7;//ds18b20 端口
13 sfr dataled=0x80;//显示数据端口(P0)
14 /**********************************************************************/
15 uchar temp;
16 uchar flag_get,count,num,minute,second;
17 uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};//7段数码管段码表共阳"01234
-56789 -"
18 uchar str[5];
19 int ints[5];
20 int dot = 0;
21 int fu = 0;
22
23 typedef unsigned char byte;
24 typedef unsigned int word;
25
26 /***********************************************************************/
27 void delay1(uchar MS);
28 unsigned char ReadTemperature(void);
29 void Init_DS18B20(void);
30 unsigned char ReadOneChar(void);
31 void WriteOneChar(unsigned char dat);
32 void delay(unsigned int i);
33 /************************************************************************/
34 main()
35 {
36 1 TMOD|=0x01;//定时器设置
37 1 TH0=0xef;
38 1 TL0=0xf0;
39 1 IE=0x82;
40 1 TR0=1;
41 1 P2=0x00;
42 1 count=0;
43 1
44 1 while(1)
45 1 {
46 2 if (fu==1) //负数
47 2 {
48 3 ints[4]=11;
49 3 }
50 2 else //正数
51 2 {
52 3 ints[4]=temp/100; //百位温度
53 3 }
54 2 ints[3]=temp/10%10; //十位温度
C51 COMPILER V7.06 18B20 01/15/2009 14:58:48 PAGE 2
55 2 ints[2]=temp%10; //个位温度
56 2 ints[1]=dot;//小数点后第一位温度
57 2
58 2 if(ints[4]==0) //百位为0
59 2 {
60 3 ints[4]=10; //隐藏百位
61 3 }
62 2
63 2 if(ints[3]==0 && ints[4]!=1) //十位为0且百位不为1
64 2 {
65 3 ints[3]=ints[4]; //把百位的负号或空位挪到十位
66 3 ints[4]=10; //隐藏百位
67 3 }
68 2
69 2 str[4]=tab[ints[4]];
70 2 str[3]=tab[ints[3]]; //十位温度
71 2 str[2]=tab[ints[2]]-0x80; //个位温度
72 2 str[1]=tab[ints[1]];//小数点后第一位
73 2 str[0]=0xa7;
74 2
75 2 if(flag_get==1) //定时读取当前温度
76 2 {
77 3 temp=ReadTemperature();
78 3 flag_get=0;
79 3 }
80 2 }
81 1 }
82
83 void tim(void) interrupt 1 using 1//中断,用于数码管扫描和温度检测间隔
84 {
85 1 TH0=0xef;//定时器重装值
86 1 TL0=0xf0;
87 1 num++;
88 1 if (num==50)
89 1 {
90 2 num=0;
91 2 flag_get=1;//标志位有效
92 2 second++;
93 2 if(second>=60)
94 2 {
95 3 second=0;
96 3 minute++;
97 3 }
98 2 }
99 1 count++;
100 1 if(count==1) //数码管扫描
101 1 {
102 2 P2=0xef; //11101111
103 2 dataled=str[4];
104 2 }
105 1 if(count==2)
106 1 {
107 2 P2=0xf7; //11110111
108 2 dataled=str[3];
109 2 }
110 1 if(count==3)
111 1 {
112 2 P2=0xfb; //11111011
113 2 dataled=str[2];
114 2 }
115 1 if(count==4)
116 1 {
C51 COMPILER V7.06 18B20 01/15/2009 14:58:48 PAGE 3
117 2 P2=0xfd; //11111101
118 2 dataled=str[1];
119 2 }
120 1 if(count==5)
121 1 {
122 2 P2=0xfe; //11111110
123 2 dataled=str[0];
124 2 count=0;
125 2 }
126 1 }
127 /*************************************************************************************/
128 //延时函数
129 void delay(unsigned int i)
130 {
131 1 while(i--);
132 1 }
133
134 /***************************************************************************************/
135 //18b20初始化函数
136 void Init_DS18B20(void)
137 {
138 1 unsigned char x=0;
139 1 DQ = 1; //DQ复位
140 1 delay(8); //稍做延时
141 1 DQ = 0; //单片机将DQ拉低
142 1 delay(80); //精确延时 大于 480us
143 1 DQ = 1; //拉高总线
144 1 delay(10);
145 1 x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
146 1 delay(5);
147 1 }
148
149 //读一个字节
150 unsigned char ReadOneChar(void)
151 {
152 1 unsigned char i=0;
153 1 unsigned char dat = 0;
154 1 for (i=8;i>0;i--)
155 1 {
156 2 DQ = 0; // 给脉冲信号
157 2 dat>>=1;
158 2 DQ = 1; // 给脉冲信号
159 2 if(DQ)
160 2 {
161 3 dat|=0x80;
162 3 }
163 2 delay(5);
164 2 }
165 1 return(dat);
166 1 }
167
168
169 //写一个字节
170 void WriteOneChar(unsigned char dat)
171 {
172 1 unsigned char i=0;
173 1 for (i=8; i>0; i--)
174 1 {
175 2 DQ = 0;
176 2 DQ = dat&0x01;
177 2 delay(5);
178 2 DQ = 1;
C51 COMPILER V7.06 18B20 01/15/2009 14:58:48 PAGE 4
179 2 dat>>=1;
180 2 }
181 1 delay(5);
182 1 }
183
184 //读取温度
185 unsigned char ReadTemperature(void)
186 {
187 1 unsigned char a=0;
188 1 unsigned char b=0;
189 1 unsigned char t=0;
190 1 Init_DS18B20();
191 1 WriteOneChar(0xCC); // 跳过读序号列号的操作
192 1 WriteOneChar(0x44); // 启动温度转换
193 1 delay(200);
194 1 Init_DS18B20();
195 1 WriteOneChar(0xCC); //跳过读序号列号的操作
196 1 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
197 1 a=ReadOneChar();
198 1 b=ReadOneChar();
199 1 t=(b<<4)+((a&0xf0)>>4);
200 1 dot = (a&0x0f)*0.625+0.5;
201 1 fu=0;
202 1 if(t>>7)
203 1 {
204 2 fu = 1;
205 2 t=~t+0x01;
206 2 dot=(10-dot)%10;
207 2 if(dot!=0)
208 2 {
209 3 t--;
210 3 }
211 2 }
212 1 return(t);
213 1 }
214
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 530 ----
CONSTANT SIZE = 12 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 25 1
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -