📄 main.lst
字号:
C51 COMPILER V7.08 MAIN 01/04/2011 13:07:48 PAGE 1
C51 COMPILER V7.08, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE main.c BROWSE DEBUG OBJECTEXTEND
line level source
1 /*
2 * DS18B20
3 */
4
5 #include <reg52.h>
6 #include <intrins.h>
7
8 typedef unsigned char uint8;
9 typedef unsigned int uint16;
10 typedef char int8;
11 typedef int int16;
12
13 sbit DQ = P3^2; //温度输入口
14
15 #define nops(); {_nop_(); _nop_(); _nop_(); _nop_();} //定义空指令
16
17
18 void delay(uint16 n)
19 {
20 1 while (n--);
21 1 }
22
23 void delay_ms(uint16 n)
24 {
25 1 uint8 m=120;
26 1
27 1 while (n--)
28 1 while (m--);
29 1 }
30
31 /*
32 * 18B20复位函数
33 */
34 void DS18b20_reset(void)
35 {
36 1 bit flag=1;
37 1
38 1 while (flag)
39 1 {
40 2 while (flag)
41 2 {
42 3 DQ = 1;
43 3 delay(1);
44 3 DQ = 0;
45 3 delay(50); // 550us
46 3 DQ = 1;
47 3 delay(6); // 66us
48 3 flag = DQ;
49 3 }
50 2 delay(45); //延时500us
51 2 flag = ~DQ;
52 2 }
53 1 DQ=1;
54 1 }
55
C51 COMPILER V7.08 MAIN 01/04/2011 13:07:48 PAGE 2
56
57 /*
58 * 18B20写1个字节函数
59 * 向1-WIRE总线上写一个字节
60 */
61 void write_byte(uint8 val)
62 {
63 1 uint8 i;
64 1
65 1 for (i=0; i<8; i++)
66 1 {
67 2 DQ = 1;
68 2 _nop_();
69 2 DQ = 0;
70 2 nops(); //4us
71 2 DQ = val & 0x01; //最低位移出
72 2 delay(6); //66us
73 2 val >>= 1; //右移一位
74 2 }
75 1 DQ = 1;
76 1 delay(1);
77 1 }
78
79 /*
80 * 18B20读1个字节函数
81 * 从1-WIRE总线上读取一个字节
82 */
83 uint8 read_byte(void)
84 {
85 1 uint8 i, value=0;
86 1
87 1 for (i=0; i<8; i++)
88 1 {
89 2 DQ=1;
90 2 _nop_();
91 2 value >>= 1;
92 2 DQ = 0;
93 2 nops(); //4us
94 2 DQ = 1;
95 2 nops(); //4us
96 2 if (DQ)
97 2 value|=0x80;
98 2 delay(6); //66us
99 2 }
100 1 DQ=1;
101 1
102 1 return(value);
103 1 }
104
105 /*
106 * 启动温度转换
107 */
108 void start_temp_sensor(void)
109 {
110 1 DS18b20_reset();
111 1 write_byte(0xCC); // 发Skip ROM命令
112 1 write_byte(0x44); // 发转换命令
113 1 }
114
115 /*
116 * 读出温度
117 */
C51 COMPILER V7.08 MAIN 01/04/2011 13:07:48 PAGE 3
118 int16 read_temp(void)
119 {
120 1 uint8 temp_data[2]; // 读出温度暂放
121 1 int16 temp;
122 1
123 1 DS18b20_reset(); // 复位
124 1 write_byte(0xCC); // 发Skip ROM命令
125 1 write_byte(0xBE); // 发读命令
126 1 temp_data[0]=read_byte(); //温度低8位
127 1 temp_data[1]=read_byte(); //温度高8位
128 1
129 1 temp = temp_data[1];
130 1 temp <<= 8;
131 1 temp |= temp_data[0];
132 1 temp >>= 4;
133 1
134 1 return temp;
135 1 }
136
137
138 /**
139 * UART初始化
140 * 波特率:9600
141 */
142 void uart_init(void)
143 {
144 1 TMOD = 0x21; // 定时器1工作在方式2(自动重装)
145 1 SCON = 0x50; // 10位uart,允许串行接受
146 1
147 1 TH1 = 0xFD;
148 1 TL1 = 0xFD;
149 1
150 1 TR1 = 1;
151 1 }
152
153 /**
154 * UART发送一字节
155 */
156 void UART_Send_Byte(uint8 dat)
157 {
158 1 SBUF = dat;
159 1 while (TI == 0);
160 1 TI = 0;
161 1 }
162
163 /**
164 * 将数据转换成ASC码并通过UART发送出去
165 */
166 void UART_Send_Dat(uint8 dat)
167 {
168 1 UART_Send_Byte(dat/10%10 + '0');
169 1 UART_Send_Byte(dat%10 + '0');
170 1 }
171
172 main()
173 {
174 1 int16 ans;
175 1
176 1 uart_init();
177 1
178 1 while (1)
179 1 {
C51 COMPILER V7.08 MAIN 01/04/2011 13:07:48 PAGE 4
180 2 start_temp_sensor();
181 2
182 2 delay_ms (1000); // 延时1秒
183 2
184 2 ans=read_temp();
185 2
186 2 if (ans < 0)
187 2 {
188 3 UART_Send_Byte('-');
189 3 ans = -ans;
190 3 }
191 2
192 2 UART_Send_Dat(ans);
193 2 UART_Send_Byte('\r');
194 2 UART_Send_Byte('\n');
195 2 }
196 1
197 1 }
198
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 333 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 2
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 + -