📄 ds18b20-lcd1602-c51.lst
字号:
C51 COMPILER V8.02 DS18B20_LCD1602_C51 08/21/2006 15:48:03 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE DS18B20_LCD1602_C51
OBJECT MODULE PLACED IN DS18B20-LCD1602-C51.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE DS18B20-LCD1602-C51.C BROWSE DEBUG OBJECTEXTEND
line level source
1 /*******************************************************************/
2 /* */
3 /* ME300B单片机开发系统演示程序 - DS18B20温度显示 */
4 /* */
5 /* LCD1602同时显示两个DS18B20的温度值 */
6 /* */
7 /* 版本: V1.0 (2006/11/20) */
8 /* 作者: gguoqing (Email: gguoqing@willar.com) */
9 /* 网站: www.willar.com(伟纳电子) www.mcusj.com(伟纳单片机世界) */
10 /* 时间: 2006/08/20 */
11 /* */
12 /*【版权】Copyright(C)伟纳电子 www.willar.com All Rights Reserved */
13 /*【声明】此程序仅用于学习与参考,引用请注明版权和作者信息! */
14 /* */
15 /*******************************************************************/
16
17 #include <reg51.h>
18 #include <intrins.h>
19
20 #define uchar unsigned char
21 #define uint unsigned int
22
23 sbit DQ = P3^3; //定义DS18B20端口DQ
24 sbit BEEP=P3^7 ; //蜂鸣器驱动线
25
26 bit presence ;
27
28 sbit LCD_RS = P2^0;
29 sbit LCD_RW = P2^1;
30 sbit LCD_EN = P2^2;
31
32 uchar code cdis1[ ] = {" TEMP1: . C "};
33 uchar code cdis2[ ] = {" TEMP2: . C "};
34 uchar code cdis3[ ] = {" DS18B20 ERR0R "};
35 uchar code cdis4[ ] = {" PLEASE CHECK "};
36
37 unsigned char data temp_data[2] = {0x00,0x00};
38 unsigned char data display[5] = {0x00,0x00,0x00,0x00,0x00};
39
40 unsigned char code ditab[16] = {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,
41 0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
42
43 unsigned char code mytab[8] = {0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00};
44
45 unsigned char code RomCode[2][8] = {0x28,0xd6,0x31,0x8a,0x00,0x00,0x00,0xe3,
46 0x28,0x0e,0x4e,0xb3,0x00,0x00,0x00,0x5f};
47 void beep();
48
49 #define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
50
51 /*******************************************************************/
52 void delay1(int ms)
53 {
54 1 unsigned char y;
55 1 while(ms--)
C51 COMPILER V8.02 DS18B20_LCD1602_C51 08/21/2006 15:48:03 PAGE 2
56 1 {
57 2 for(y = 0; y<250; y++)
58 2 {
59 3 _nop_();
60 3 _nop_();
61 3 _nop_();
62 3 _nop_();
63 3 }
64 2 }
65 1 }
66
67 /******************************************************************/
68 /* */
69 /*检查LCD忙状态 */
70 /*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。 */
71 /* */
72 /******************************************************************/
73
74 bit lcd_busy()
75 {
76 1 bit result;
77 1 LCD_RS = 0;
78 1 LCD_RW = 1;
79 1 LCD_EN = 1;
80 1 delayNOP();
81 1 result = (bit)(P0&0x80);
82 1 LCD_EN = 0;
83 1 return(result);
84 1 }
85
86 /*******************************************************************/
87 /* */
88 /*写指令数据到LCD */
89 /*RS=L,RW=L,E=高脉冲,D0-D7=指令码。 */
90 /* */
91 /*******************************************************************/
92
93 void lcd_wcmd(uchar cmd)
94
95 {
96 1 while(lcd_busy());
97 1 LCD_RS = 0;
98 1 LCD_RW = 0;
99 1 LCD_EN = 0;
100 1 _nop_();
101 1 _nop_();
102 1 P0 = cmd;
103 1 delayNOP();
104 1 LCD_EN = 1;
105 1 delayNOP();
106 1 LCD_EN = 0;
107 1 }
108
109 /*******************************************************************/
110 /* */
111 /*写显示数据到LCD */
112 /*RS=H,RW=L,E=高脉冲,D0-D7=数据。 */
113 /* */
114 /*******************************************************************/
115
116 void lcd_wdat(uchar dat)
117 {
C51 COMPILER V8.02 DS18B20_LCD1602_C51 08/21/2006 15:48:03 PAGE 3
118 1 while(lcd_busy());
119 1 LCD_RS = 1;
120 1 LCD_RW = 0;
121 1 LCD_EN = 0;
122 1 P0 = dat;
123 1 delayNOP();
124 1 LCD_EN = 1;
125 1 delayNOP();
126 1 LCD_EN = 0;
127 1 }
128
129 /*******************************************************************/
130 /* */
131 /* LCD初始化设定 */
132 /* */
133 /*******************************************************************/
134
135 void lcd_init()
136 {
137 1 delay1(15);
138 1 lcd_wcmd(0x01); //清除LCD的显示内容
139 1 lcd_wcmd(0x38); //16*2显示,5*7点阵,8位数据
140 1 delay1(5);
141 1 lcd_wcmd(0x38);
142 1 delay1(5);
143 1 lcd_wcmd(0x38);
144 1 delay1(5);
145 1
146 1 lcd_wcmd(0x0c); //显示开,关光标
147 1 delay1(5);
148 1 lcd_wcmd(0x06); //移动光标
149 1 delay1(5);
150 1 lcd_wcmd(0x01); //清除LCD的显示内容
151 1 delay1(5);
152 1 }
153
154 /*******************************************************************/
155 /* */
156 /* 设定显示位置 */
157 /* */
158 /*******************************************************************/
159
160 void lcd_pos(uchar pos)
161 {
162 1 lcd_wcmd(pos | 0x80); //数据指针=80+地址变量
163 1 }
164
165 /*******************************************************************/
166 /* */
167 /*自定义字符写入CGRAM */
168 /* */
169 /*******************************************************************/
170 void writetab()
171 {
172 1 unsigned char i;
173 1 lcd_wcmd(0x40); //写CGRAM
174 1 for (i = 0; i< 8; i++)
175 1 lcd_wdat(mytab[i]);
176 1 }
177
178 /*******************************************************************/
179 /* */
C51 COMPILER V8.02 DS18B20_LCD1602_C51 08/21/2006 15:48:03 PAGE 4
180 /*us级延时函数 */
181 /* */
182 /*******************************************************************/
183
184 void Delay(unsigned int num)
185 {
186 1 while( --num );
187 1 }
188
189 /*******************************************************************/
190 /* */
191 /*初始化ds1820 */
192 /* */
193 /*******************************************************************/
194 Init_DS18B20(void)
195 {
196 1 DQ = 1; //DQ复位
197 1 Delay(8); //稍做延时
198 1
199 1 DQ = 0; //单片机将DQ拉低
200 1 Delay(90); //精确延时大于480us
201 1
202 1 DQ = 1; //拉高总线
203 1 Delay(8);
204 1
205 1 presence = DQ; //如果=0则初始化成功 =1则初始化失败
206 1 Delay(100);
207 1 DQ = 1;
208 1
209 1 return(presence); //返回信号,0=presence,1= no presence
210 1 }
211
212 /*******************************************************************/
213 /* */
214 /* 读一个字节 */
215 /* */
216 /*******************************************************************/
217 ReadOneChar(void)
218 {
219 1 unsigned char i = 0;
220 1 unsigned char dat = 0;
221 1
222 1 for (i = 8; i > 0; i--)
223 1 {
224 2 DQ = 0; // 给脉冲信号
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -