📄 ds18b20温度测量.lst
字号:
C51 COMPILER V7.50 DS18B20温度测量 03/31/2007 23:27:40 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE DS18B20温度测量
OBJECT MODULE PLACED IN DS18B20温度测量.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE DS18B20温度测量.C BROWSE DEBUG OBJECTEXTEND
line level source
1 //MCU: AT89S51
2 //晶振:12M
3 #include "AT89X51.h"
4 #include "intrins.h"
5 //common part
6 #define HIGH 1
7 #define LOW 0
8 #define TRUE 1
9 #define ZERO 0
10 #define MSB 0x80
11 //ds18b20 part
12 #define SkipRom 0xcc
13 #define ConvertTemperature 0x44
14 #define ReadScratchpad 0xbe
15
16 /*******************************************************************/
17 sbit One_Wire_Bus=P2^7;
18
19 unsigned char code numcode[]={0xc0,0xf9,0xa4,0xb0,0x99,
20 0x92,0x82,0xf8,0x80,0x90,
21 0x86,0x8c,0xb7,0X9C,0XC6
22 };//数字0~9及"EP=OC"共阳数码管代码
23 unsigned char code dot_numcode[]={0X40,0X79,0X24,0X30,0X19,
24 0X12,0X02,0X78,0X00,0X10
25 };//带数点的0~9共阳数码管代码
26 unsigned char code bitcode[]={0xfe,0xfd,0xfb,0xf7,
27 0xef,0xdf,0xbf,0x7f}; //数码管位选代码
28 unsigned char dispbuff[8]={14,13,0,0,0,12,11,10};
29 unsigned char timecount;
30 unsigned char disp_bit_count;
31
32 /********************************************************************/
33 unsigned char GetScratchpad[2];
34 unsigned char code decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
35 unsigned char code decimalL[16]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
36 unsigned char ResultTemperatureH;
37 unsigned char ResultTemperatureLH,ResultTemperatureLL;
38 unsigned char ResultSignal;
39
40 /********************************************************************/
41 void One_Wire_Delay(unsigned char delay_time)
42 {
43 1 while(delay_time)delay_time--;//延时时间:=(8+delay_time*6)us;
44 1 }
45
46 //*****初始化DS18B20******/
47 void Initize_One_Wire_Bus(void)
48 {
49 1 One_Wire_Bus=0;
50 1 One_Wire_Delay(80);//总线拉低 488us
51 1 One_Wire_Bus=1;
52 1 One_Wire_Delay(25);//总线拉高 158us;
53 1 }
54 /***********************************************************************/
55
C51 COMPILER V7.50 DS18B20温度测量 03/31/2007 23:27:40 PAGE 2
56 /*******************读一个字节ds18b20 *********************************/
57 unsigned char One_Wire_Read_Byte(void)
58 {
59 1 bit temp_bit;
60 1 unsigned char i,result=0;
61 1 for(i=0;i<8;i++)
62 1 {
63 2 One_Wire_Bus=0;
64 2 One_Wire_Bus=1;
65 2 temp_bit=One_Wire_Bus;
66 2 One_Wire_Delay(9);//延时 62 us
67 2 if(temp_bit)
68 2 result|=0x01<<i;
69 2 }
70 1 return(result); //返回ds18b20值
71 1
72 1 }
73
74 /*********向ds18b20写一个字节*********************/
75 void One_Wire_Write_Byte(unsigned char oww_dat)
76 {
77 1 unsigned char i;
78 1 for(i=0;i<8;i++)
79 1 {
80 2 One_Wire_Bus=0;
81 2 if(oww_dat&0x01)One_Wire_Bus=1;
82 2 One_Wire_Delay(20);// 128 us
83 2 One_Wire_Bus=1;
84 2 oww_dat>>=1;
85 2 }
86 1 One_Wire_Delay(10);
87 1 }
88 /******************************************/
89 void Read_18B20(void)
90 {
91 1 unsigned char tempH,tempL;
92 1 Initize_One_Wire_Bus();
93 1 One_Wire_Write_Byte(SkipRom);
94 1 _nop_();
95 1 //There is just one DS1820 on the bus;
96 1 One_Wire_Write_Byte(ConvertTemperature);
97 1 One_Wire_Delay(5);
98 1 //Start to convert temperature;
99 1 Initize_One_Wire_Bus();
100 1 One_Wire_Write_Byte(SkipRom);
101 1 _nop_();
102 1 One_Wire_Write_Byte(ReadScratchpad);
103 1 GetScratchpad[0]=One_Wire_Read_Byte();
104 1 //Master samples the LSB temperature from the scratchpad;
105 1 GetScratchpad[1]=One_Wire_Read_Byte();
106 1 //Master samples the MSB temperature from the scratchpad;
107 1 One_Wire_Delay(120);
108 1 tempH=(GetScratchpad[1]<<4)|(GetScratchpad[0]>>4);
109 1 tempL=(GetScratchpad[0]&0x0f);
110 1 Initize_One_Wire_Bus();
111 1 //Issue a reset to terminate left parts;
112 1 if(tempH&0x80)
113 1 {
114 2 tempH=~tempH;
115 2 tempL=~tempL+1;
116 2 ResultSignal=1;
117 2 //Negative temperature;
C51 COMPILER V7.50 DS18B20温度测量 03/31/2007 23:27:40 PAGE 3
118 2 }
119 1 ResultTemperatureH=tempH;
120 1 ResultTemperatureLL=decimalL[tempL];
121 1 ResultTemperatureLH=decimalH[tempL];
122 1 //Result of temperature;
123 1 }//Read the byte0 and byte1 from scratchpad;
124 /***********************************************************************/
125
126 void main(void)
127 {
128 1
129 1 TMOD=0x01; //使用定时器0,选择方式1(16位定时器)
130 1 TH0=(65536-3000)/256; //定时3MS初值
131 1 TL0=(65536-3000)%256;
132 1 ET0=1; //开定时器0溢出中断
133 1 EA=1; //开总中断
134 1 Initize_One_Wire_Bus();
135 1 TR0=1; //开定时器0
136 1 while(TRUE )
137 1 {
138 2 if(timecount==10) //每30ms采样一次温度可以修改采样时间
139 2 {
140 3 timecount=0;
141 3 Read_18B20();
142 3 dispbuff[4]=((ResultTemperatureH%100)/10); //温度十位
143 3 dispbuff[3]=(ResultTemperatureH%10); //温度个位
144 3 dispbuff[2]=(ResultTemperatureLH/10); //温度小数位
145 3 }
146 2 }
147 1
148 1 }
149
150 /*********3MS中断服务程序*************/
151 void t0(void) interrupt 1
152 {
153 1 TH0=(65536-3000)/256;
154 1 TL0=(65536-3000)%256;
155 1 timecount++;
156 1 if(disp_bit_count==3)
157 1 P0=dot_numcode[dispbuff[disp_bit_count]];
158 1 else
159 1 P0=numcode[dispbuff[disp_bit_count]];
160 1 P1=bitcode[disp_bit_count];
161 1 disp_bit_count++;
162 1 if(disp_bit_count==8)
163 1 disp_bit_count=0;
164 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 330 ----
CONSTANT SIZE = 65 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 16 ----
IDATA SIZE = ---- ----
BIT SIZE = ---- 1
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -