|
|
C51 COMPILER V9.56.0.0 LIGHT 04/30/2022 20:06:41 PAGE 1 |
|
|
|
|
|
|
|
|
C51 COMPILER V9.56.0.0, COMPILATION OF MODULE LIGHT |
|
|
OBJECT MODULE PLACED IN Light.OBJ |
|
|
COMPILER INVOKED BY: D:\Keil_v5\C51\BIN\C51.EXE Light.c OPTIMIZE(8,SPEED) BROWSE DEBUG OBJECTEXTEND TABS(2) |
|
|
|
|
|
line level source |
|
|
|
|
|
1 #ifndef __LIGHT_H_ |
|
|
2 #define __LIGHT_H_ |
|
|
3 |
|
|
4 #include <REG51.H> |
|
|
5 #include <math.h> //Keil library |
|
|
6 #include <stdio.h> //Keil library |
|
|
7 |
|
|
8 |
|
|
9 #define SlaveAddress 0x46 //定义器件在IIC总线中的从地址,根据ALT ADDRESS地址引脚不同修改 |
|
|
10 //ALT ADDRESS引脚接地时地址为0x46,接电源时地址为0xB8 |
|
|
11 |
|
|
12 |
|
|
13 sbit SCL=P1^0; //IIC时钟引脚定义 |
|
|
14 sbit SDA=P1^1; //IIC数据引脚定义 |
|
|
15 |
|
|
16 |
|
|
17 typedef unsigned short WORD; |
|
|
18 |
|
|
19 unsigned char BUF[8]; //接收数据缓存区 |
|
|
20 |
|
|
21 int dis_data; //变量 |
|
|
22 |
|
|
23 void delay_nms(unsigned int k); |
|
|
24 void InitLcd(); |
|
|
25 void Init_BH1750(void); |
|
|
26 void WriteDataLCM(uchar dataW); |
|
|
*** ERROR C141 IN LINE 26 OF Light.c: syntax error near 'dataW', expected ')' |
|
|
27 void WriteCommandLCM(uchar CMD,uchar Attribc); |
|
|
*** ERROR C141 IN LINE 27 OF Light.c: syntax error near 'CMD', expected ')' |
|
|
28 void DisplayOneChar(uchar X,uchar Y,uchar DData); |
|
|
*** ERROR C141 IN LINE 28 OF Light.c: syntax error near 'X', expected ')' |
|
|
29 void conversion(uint temp_data); |
|
|
*** ERROR C141 IN LINE 29 OF Light.c: syntax error near 'temp_data', expected ')' |
|
|
30 |
|
|
31 void Single_Write_BH1750(uchar REG_Address); //单个写入数据 |
|
|
*** ERROR C141 IN LINE 31 OF Light.c: syntax error near 'REG_Address', expected ')' |
|
|
32 uchar Single_Read_BH1750(uchar REG_Address); //单个读取内部寄存器数据 |
|
|
*** ERROR C129 IN LINE 32 OF Light.c: missing ';' before 'Single_Read_BH1750' |
|
|
33 void Multiple_Read_BH1750(); //连续的读取内部寄存器数据 |
|
|
34 //------------------------------------ |
|
|
35 void Delay5us(); |
|
|
36 void Delay5ms(); |
|
|
37 void BH1750_Start(); //起始信号 |
|
|
38 void BH1750_Stop(); //停止信号 |
|
|
39 void BH1750_SendACK(bit ack); //应答ACK |
|
|
40 bit BH1750_RecvACK(); //读ack |
|
|
41 void BH1750_Sendunsigned char(unsigned char dat); //IIC单个字节写 |
|
|
42 unsigned char BH1750_Recvunsigned char(); //IIC单个字节读 |
|
|
43 |
|
|
44 |
|
|
45 //----------------------------------- |
|
|
46 |
|
|
47 //********************************************************* |
|
|
48 void conversion(uint temp_data) // 数据转换出 个,十,百,千,万 |
|
|
49 { |
|
|
C51 COMPILER V9.56.0.0 LIGHT 04/30/2022 20:06:41 PAGE 2 |
|
|
|
|
|
50 wan=temp_data/10000+0x30 ; |
|
|
51 temp_data=temp_data%10000; //取余运算 |
|
|
52 qian=temp_data/1000+0x30 ; |
|
|
53 temp_data=temp_data%1000; //取余运算 |
|
|
54 bai=temp_data/100+0x30; |
|
|
55 temp_data=temp_data%100; //取余运算 |
|
|
56 shi=temp_data/10+0x30; |
|
|
57 temp_data=temp_data%10; //取余运算 |
|
|
58 ge=temp_data+0x30; |
|
|
59 } |
|
|
60 |
|
|
61 //毫秒延时************************** |
|
|
62 void delay_nms(unsigned int k) |
|
|
63 { |
|
|
64 unsigned int i,j; |
|
|
65 for(i=0;i<k;i++) |
|
|
66 { |
|
|
67 for(j=0;j<121;j++) |
|
|
68 {;}} |
|
|
69 } |
|
|
70 |
|
|
71 /*******************************/ |
|
|
72 void WaitForEnable(void) |
|
|
73 { |
|
|
74 DataPort=0xff; |
|
|
75 LCM_RS=0;LCM_RW=1;_nop_(); |
|
|
76 LCM_EN=1;_nop_();_nop_(); |
|
|
77 while(DataPort&0x80); |
|
|
78 LCM_EN=0; |
|
|
79 } |
|
|
80 /*******************************/ |
|
|
81 void WriteCommandLCM(uchar CMD,uchar Attribc) |
|
|
82 { |
|
|
83 if(Attribc)WaitForEnable(); |
|
|
84 LCM_RS=0;LCM_RW=0;_nop_(); |
|
|
85 DataPort=CMD;_nop_(); |
|
|
86 LCM_EN=1;_nop_();_nop_();LCM_EN=0; |
|
|
87 } |
|
|
88 /*******************************/ |
|
|
89 void WriteDataLCM(uchar dataW) |
|
|
90 { |
|
|
91 WaitForEnable(); |
|
|
92 LCM_RS=1;LCM_RW=0;_nop_(); |
|
|
93 DataPort=dataW;_nop_(); |
|
|
94 LCM_EN=1;_nop_();_nop_();LCM_EN=0; |
|
|
95 } |
|
|
96 /***********************************/ |
|
|
97 void InitLcd() |
|
|
98 { |
|
|
99 WriteCommandLCM(0x38,1); |
|
|
100 WriteCommandLCM(0x08,1); |
|
|
101 WriteCommandLCM(0x01,1); |
|
|
102 WriteCommandLCM(0x06,1); |
|
|
103 WriteCommandLCM(0x0c,1); |
|
|
104 } |
|
|
105 /***********************************/ |
|
|
106 void DisplayOneChar(uchar X,uchar Y,uchar DData) |
|
|
107 { |
|
|
108 Y&=1; |
|
|
109 X&=15; |
|
|
110 if(Y)X|=0x40; |
|
|
111 X|=0x80; |
|
|
C51 COMPILER V9.56.0.0 LIGHT 04/30/2022 20:06:41 PAGE 3 |
|
|
|
|
|
112 WriteCommandLCM(X,0); |
|
|
113 WriteDataLCM(DData); |
|
|
114 } |
|
|
115 |
|
|
116 /************************************** |
|
|
117 延时5微秒(STC90C52RC@12M) |
|
|
118 不同的工作环境,需要调整此函数,注意时钟过快时需要修改 |
|
|
119 当改用1T的MCU时,请调整此延时函数 |
|
|
120 **************************************/ |
|
|
121 void Delay5us() |
|
|
122 { |
|
|
123 _nop_();_nop_();_nop_();_nop_(); |
|
|
124 _nop_();_nop_();_nop_();_nop_(); |
|
|
125 _nop_();_nop_();_nop_();_nop_(); |
|
|
126 _nop_();_nop_();_nop_();_nop_(); |
|
|
127 } |
|
|
128 |
|
|
129 /************************************** |
|
|
130 延时5毫秒(STC90C52RC@12M) |
|
|
131 不同的工作环境,需要调整此函数 |
|
|
132 当改用1T的MCU时,请调整此延时函数 |
|
|
133 **************************************/ |
|
|
134 void Delay5ms() |
|
|
135 { |
|
|
136 WORD n = 560; |
|
|
137 |
|
|
138 while (n--); |
|
|
139 } |
|
|
140 |
|
|
141 /************************************** |
|
|
142 起始信号 |
|
|
143 **************************************/ |
|
|
144 void BH1750_Start() |
|
|
145 { |
|
|
146 SDA = 1; //拉高数据线 |
|
|
147 SCL = 1; //拉高时钟线 |
|
|
148 Delay5us(); //延时 |
|
|
149 SDA = 0; //产生下降沿 |
|
|
150 Delay5us(); //延时 |
|
|
151 SCL = 0; //拉低时钟线 |
|
|
152 } |
|
|
153 |
|
|
154 /************************************** |
|
|
155 停止信号 |
|
|
156 **************************************/ |
|
|
157 void BH1750_Stop() |
|
|
158 { |
|
|
159 SDA = 0; //拉低数据线 |
|
|
160 SCL = 1; //拉高时钟线 |
|
|
161 Delay5us(); //延时 |
|
|
162 SDA = 1; //产生上升沿 |
|
|
163 Delay5us(); //延时 |
|
|
164 } |
|
|
165 |
|
|
166 /************************************** |
|
|
167 发送应答信号 |
|
|
168 入口参数:ack (0:ACK 1:NAK) |
|
|
169 **************************************/ |
|
|
170 void BH1750_SendACK(bit ack) |
|
|
171 { |
|
|
172 SDA = ack; //写应答信号 |
|
|
173 SCL = 1; //拉高时钟线 |
|
|
C51 COMPILER V9.56.0.0 LIGHT 04/30/2022 20:06:41 PAGE 4 |
|
|
|
|
|
174 Delay5us(); //延时 |
|
|
175 SCL = 0; //拉低时钟线 |
|
|
176 Delay5us(); //延时 |
|
|
177 } |
|
|
178 |
|
|
179 /************************************** |
|
|
180 接收应答信号 |
|
|
181 **************************************/ |
|
|
182 bit BH1750_RecvACK() |
|
|
183 { |
|
|
184 SCL = 1; //拉高时钟线 |
|
|
185 Delay5us(); //延时 |
|
|
186 CY = SDA; //读应答信号 |
|
|
187 SCL = 0; //拉低时钟线 |
|
|
188 Delay5us(); //延时 |
|
|
189 |
|
|
190 return CY; |
|
|
191 } |
|
|
192 |
|
|
193 /************************************** |
|
|
194 向IIC总线发送一个字节数据 |
|
|
195 **************************************/ |
|
|
196 void BH1750_Sendunsigned char(unsigned char dat) |
|
|
197 { |
|
|
198 unsigned char i; |
|
|
199 |
|
|
200 for (i=0; i<8; i++) //8位计数器 |
|
|
201 { |
|
|
202 dat <<= 1; //移出数据的最高位 |
|
|
203 SDA = CY; //送数据口 |
|
|
204 SCL = 1; //拉高时钟线 |
|
|
205 Delay5us(); //延时 |
|
|
206 SCL = 0; //拉低时钟线 |
|
|
207 Delay5us(); //延时 |
|
|
208 } |
|
|
209 BH1750_RecvACK(); |
|
|
210 } |
|
|
211 |
|
|
212 /************************************** |
|
|
213 从IIC总线接收一个字节数据 |
|
|
214 **************************************/ |
|
|
215 unsigned char BH1750_Recvunsigned char() |
|
|
216 { |
|
|
217 unsigned char i; |
|
|
218 unsigned char dat = 0; |
|
|
219 |
|
|
220 SDA = 1; //使能内部上拉,准备读取数据, |
|
|
221 for (i=0; i<8; i++) //8位计数器 |
|
|
222 { |
|
|
223 dat <<= 1; |
|
|
224 SCL = 1; //拉高时钟线 |
|
|
225 Delay5us(); //延时 |
|
|
226 dat |= SDA; //读数据 |
|
|
227 SCL = 0; //拉低时钟线 |
|
|
228 Delay5us(); //延时 |
|
|
229 } |
|
|
230 return dat; |
|
|
231 } |
|
|
232 |
|
|
233 //********************************* |
|
|
234 |
|
|
235 void Single_Write_BH1750(uchar REG_Address) |
|
|
C51 COMPILER V9.56.0.0 LIGHT 04/30/2022 20:06:41 PAGE 5 |
|
|
|
|
|
236 { |
|
|
237 BH1750_Start(); //起始信号 |
|
|
238 BH1750_Sendunsigned char(SlaveAddress); //发送设备地址+写信号 |
|
|
239 BH1750_Sendunsigned char(REG_Address); //内部寄存器地址, |
|
|
240 // BH1750_Sendunsigned char(REG_data); //内部寄存器数据, |
|
|
241 BH1750_Stop(); //发送停止信号 |
|
|
242 } |
|
|
243 |
|
|
244 //********单字节读取***************************************** |
|
|
245 /* |
|
|
246 uchar Single_Read_BH1750(uchar REG_Address) |
|
|
247 { uchar REG_data; |
|
|
248 BH1750_Start(); //起始信号 |
|
|
249 BH1750_Sendunsigned char(SlaveAddress); //发送设备地址+写信号 |
|
|
250 BH1750_Sendunsigned char(REG_Address); //发送存储单元地址,从0开始 |
|
|
251 BH1750_Start(); //起始信号 |
|
|
252 BH1750_Sendunsigned char(SlaveAddress+1); //发送设备地址+读信号 |
|
|
253 REG_data=BH1750_Recvunsigned char(); //读出寄存器数据 |
|
|
254 BH1750_SendACK(1); |
|
|
255 BH1750_Stop(); //停止信号 |
|
|
256 return REG_data; |
|
|
257 } |
|
|
258 */ |
|
|
259 //********************************************************* |
|
|
260 // |
|
|
261 //连续读出BH1750内部数据 |
|
|
262 // |
|
|
263 //********************************************************* |
|
|
264 void Multiple_read_BH1750(void) |
|
|
265 |
|
|
266 { uchar i; |
|
|
267 BH1750_Start(); //起始信号 |
|
|
268 BH1750_Sendunsigned char(SlaveAddress+1); //发送设备地址+读信号 |
|
|
269 |
|
|
270 for (i=0; i<3; i++) //连续读取2个地址数据,存储中BUF |
|
|
271 { |
|
|
272 BUF[i] = BH1750_Recvunsigned char(); //BUF[0]存储0x32地址中的数据 |
|
|
273 if (i == 3) |
|
|
274 { |
|
|
275 |
|
|
276 BH1750_SendACK(1); //最后一个数据需要回NOACK |
|
|
277 } |
|
|
278 else |
|
|
279 { |
|
|
280 BH1750_SendACK(0); //回应ACK |
|
|
281 } |
|
|
282 } |
|
|
283 |
|
|
284 BH1750_Stop(); //停止信号 |
|
|
285 Delay5ms(); |
|
|
286 } |
|
|
287 |
|
|
288 |
|
|
289 //初始化BH1750,根据需要请参考pdf进行修改**** |
|
|
290 void Init_BH1750() |
|
|
291 { |
|
|
292 Single_Write_BH1750(0x01); |
|
|
293 |
|
|
294 } |
|
|
295 |
|
|
296 |
|
|
297 #endif |
|
|
C51 COMPILER V9.56.0.0 LIGHT 04/30/2022 20:06:41 PAGE 6 |
|
|
|
|
|
|
|
|
C51 COMPILATION COMPLETE. 0 WARNING(S), 6 ERROR(S)
|
|
|
|