📄 text1.lst
字号:
C51 COMPILER V8.02 TEXT1 06/04/2006 16:26:49 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE TEXT1
OBJECT MODULE PLACED IN Text1.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE Text1.c BROWSE DEBUG OBJECTEXTEND TABS(3)
line level source
1 #include"reg51.h"
2
3 //*******************LCD模块
4
5 #define LCD_DATA P1 //LCD的数据口
6
7 sbit LCD_BUSY=LCD_DATA^7; //LCD忙信号位
8
9 sbit LCD_RW=P3^1; //LCD读写控制
10 sbit LCD_RS=P3^0; //LCD寄存器选择
11 sbit LCD_EN=P3^2; //LCD使能信号
12
13
14 void LCD_check_busy(void) //检测LCD状态,看它是不是还在忙呢
15 {
16 1 while(1)
17 1 {
18 2 LCD_EN=0;
19 2 LCD_RS=0; //指令寄存器通信
20 2 LCD_RW=1; //read data
21 2 LCD_DATA=0xff;
22 2 LCD_EN=1;
23 2 if(!LCD_BUSY)break;
24 2 }
25 1 LCD_EN=0;
26 1 }
27
28 void LCD_cls(void) //LCD清屏
29 {
30 1
31 1 LCD_check_busy();
32 1 LCD_RS=0;
33 1 LCD_RW=0;
34 1 LCD_DATA=1;
35 1 LCD_EN=1;
36 1 LCD_EN=0;
37 1
38 1 }
39
40 void LCD_write_instruction(unsigned char LCD_instruction) //写指令到LCD
41 {
42 1 LCD_check_busy();
43 1 LCD_RS=0;
44 1 LCD_RW=0; //写数据
45 1
46 1 LCD_DATA=LCD_instruction;
47 1 LCD_EN=1;
48 1 LCD_EN=0;
49 1 }
50
51 void LCD_write_data(unsigned char LCD_data) //输出一个字节数据到LCD
52 {
53 1 LCD_check_busy();
54 1 LCD_RS=1;
55 1 LCD_RW=0;
C51 COMPILER V8.02 TEXT1 06/04/2006 16:26:49 PAGE 2
56 1
57 1 LCD_DATA=LCD_data;
58 1 LCD_EN=1;
59 1 LCD_EN=0;
60 1 }
61
62 void LCD_set_position(unsigned char x) //LCD光标定位到x处
63 {
64 1 LCD_write_instruction(0x80+x);
65 1 }
66
67
68
69
70
71 void LCD_printc(unsigned char lcd_data) //输出一个字符到LCD
72 {
73 1 LCD_write_data(lcd_data);
74 1 }
75
76 void LCD_prints(unsigned char *lcd_string) //输出一个字符串到LCD
77 {
78 1 unsigned char i=0;
79 1 while(lcd_string[i]!=0x00)
80 1 {
81 2 LCD_write_data(lcd_string[i]);
82 2 i++;
83 2 }
84 1 }
85
86 void LCD_initial(void) //初始化LCD
87 {
88 1 LCD_write_instruction(0x3c);
89 1 LCD_write_instruction(0x0c);
90 1 LCD_write_instruction(0x06);//显示屏一定要不移动。
91 1 LCD_cls();
92 1 }
93 //*************************LCD模块结束
94
95 //*******************************ds18b20
96 sbit DQ =P2^0; //定义通信端口
97
98 //延时函数
99
100 void delay(unsigned int i)
101 {
102 1 while(i--);
103 1 }
104
105 //初始化函数
106 Init_DS18B20(void)
107 {
108 1 unsigned char x=0;
109 1 DQ = 1; //DQ复位
110 1 delay(8); //稍做延时
111 1 DQ = 0; //单片机将DQ拉低
112 1 delay(80); //精确延时 大于 480us
113 1 DQ = 1; //拉高总线
114 1 delay(14);
115 1 x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
116 1 delay(20);
117 1 }
C51 COMPILER V8.02 TEXT1 06/04/2006 16:26:49 PAGE 3
118
119 //读一个字节
120 ReadOneChar(void)
121 {
122 1 unsigned char i=0;
123 1 unsigned char dat = 0;
124 1 for (i=8;i>0;i--)
125 1 {
126 2 DQ = 0; // 给脉冲信号
127 2 dat>>=1;
128 2 DQ = 1; // 给脉冲信号
129 2 if(DQ)
130 2 dat|=0x80;
131 2 delay(4);
132 2 }
133 1 return(dat);
134 1 }
135
136 //写一个字节
137 WriteOneChar(unsigned char dat)
138 {
139 1 unsigned char i=0;
140 1 for (i=8; i>0; i--)
141 1 {
142 2 DQ = 0;
143 2 DQ = dat&0x01;
144 2 delay(5);
145 2 DQ = 1;
146 2 dat>>=1;
147 2 }
148 1 //delay(4);
149 1 }
150
151 //读取温度
152 ReadTemperature(void)
153 {
154 1 unsigned char a=0;
155 1 unsigned char b=0;
156 1 unsigned int t=0;
157 1 float tt=0;
158 1 Init_DS18B20();
159 1 WriteOneChar(0xCC); // 跳过读序号列号的操作
160 1 WriteOneChar(0x44); // 启动温度转换
161 1 Init_DS18B20();
162 1 WriteOneChar(0xCC); //跳过读序号列号的操作
163 1 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
164 1 a=ReadOneChar();
165 1 b=ReadOneChar();
166 1 t=b;
167 1 t<<=8;
168 1 t=t|a;
169 1 tt=t*0.0625;
170 1 t= tt*10+0.5; //放大10倍输出并四舍五入---此行没用
171 1 return(t);
172 1 }
173 //****************************************ds18b20结束
174 unsigned char TempBuffer[5];
175 void IntToStr(unsigned int t, unsigned char *str, unsigned char n)
176 {
177 1 unsigned char a[5]; char i, j; //取得整数值到数组
178 1 a[0]=t/100; //百位
179 1 a[1]=(t/10)%10;//十位
C51 COMPILER V8.02 TEXT1 06/04/2006 16:26:49 PAGE 4
180 1 a[2]=t%10; //个位
181 1
182 1
183 1 for(i=0; i<3; i++) //转成ASCII码
184 1 a[i]=a[i]+'0';
185 1 for(i=0; a[i]=='0' && i<=3; i++);
186 1 for(j=3-n; j<i; j++) //填充空格
187 1 { *str=' '; str++; }
188 1 for(; i<3; i++)
189 1 { *str=a[i]; str++; } //加入有效的数字
190 1 *str='\0';
191 1 }
192
193
194
195
196 main()
197 {
198 1 unsigned int i,a;
199 1 i=ReadTemperature(); //读温度
200 1 a=i;
201 1 IntToStr(i,&TempBuffer[0],3);
202 1 LCD_initial();
203 1 LCD_set_position(0);
204 1 LCD_prints("temperature :");
205 1 LCD_set_position(0x40);
206 1 LCD_prints(&TempBuffer[0]);
207 1 while(1)
208 1 { i=ReadTemperature();
209 2 if (i!=a ) //如果有变化
210 2 { a=i;
211 3 IntToStr(i,&TempBuffer[0],3);
212 3 LCD_initial();
213 3 LCD_set_position(0);
214 3 LCD_prints("temperature :");
215 3 LCD_set_position(0x40);
216 3 LCD_prints(&TempBuffer[0]);
217 3 }
218 2
219 2 }
220 1
221 1
222 1 }
223
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 603 ----
CONSTANT SIZE = 14 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 5 22
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 + -