📄 lcd12232.lst
字号:
224 2 while(TempCycB--);
225 2 };
226 1 }
227 /***********************************************************************/
228
229 /*************************************时间操作函数**************************************/
230 void Write_Ds1302_Byte(unsigned char temp)
231 {
232 1 unsigned char i;
233 1 for (i=0;i<8;i++) //循环8次 写入数据
234 1 {
235 2 SCL2=0;
236 2 SDA2=temp&LSB; //每次传输低字节
237 2 temp>>=1; //右移一位
238 2 SCL2=1;
239 2 }
240 1 }
241
C51 COMPILER V8.02 LCD12232 03/13/2009 19:43:48 PAGE 5
242 void Write_Ds1302( unsigned char address,unsigned char dat )
243 {
244 1 RST=0;
245 1 _nop_();
246 1 SCL2=0;
247 1 _nop_();
248 1 RST=1;
249 1 _nop_(); //启动
250 1 Write_Ds1302_Byte(address); //发送地址
251 1 Write_Ds1302_Byte(dat); //发送数据
252 1 RST=0; //恢复
253 1 }
254
255 unsigned char Read_Ds1302 ( unsigned char address )
256 {
257 1 unsigned char i,temp=0x00;
258 1 RST=0;
259 1 _nop_();
260 1 SCL2=0;
261 1 _nop_();
262 1 RST=1;
263 1 _nop_();
264 1 Write_Ds1302_Byte(address);
265 1 for (i=0;i<8;i++) //循环8次 读取数据
266 1 {
267 2 SCL2=1;
268 2 _nop_();
269 2 if(SDA2)
270 2 temp|=0x80; //每次传输低字节
271 2 SCL2=0;
272 2 temp>>=1; //右移一位
273 2 }
274 1 RST=0;
275 1 _nop_();
276 1 SCL2=1;
277 1 SDA2=0;
278 1
279 1 return (temp); //返回
280 1 }
281
282 void Read_RTC(void) //读取 日历
283 {
284 1 unsigned char i,*p;
285 1 p=read_rtc_address; //地址传递
286 1 for(i=0;i<7;i++) //分7次读取 年月日时分秒星期
287 1 {
288 2 l_tmpdate[i]=Read_Ds1302(*p);
289 2 p++;
290 2 }
291 1 }
292
293 void Set_RTC(void) //设定 日历
294 {
295 1 unsigned char i,*p,tmp;
296 1 for(i=0;i<7;i++){
297 2 tmp=l_tmpdate[i]/10;
298 2 l_tmpdate[i]=l_tmpdate[i]%10;
299 2 l_tmpdate[i]=l_tmpdate[i]+tmp*16;
300 2 }
301 1 Write_Ds1302(0x8E,0X00);
302 1
303 1 p=write_rtc_address; //传地址
C51 COMPILER V8.02 LCD12232 03/13/2009 19:43:48 PAGE 6
304 1 for(i=0;i<7;i++) //7次写入 年月日时分秒星期
305 1 {
306 2 Write_Ds1302(*p,l_tmpdate[i]);
307 2 p++;
308 2 }
309 1 Write_Ds1302(0x8E,0x80);
310 1 }
311 /***********************************************************************/
312
313 /**********************DS18B20温度读操作函数********************************/
314 void delayb(unsigned int count) //delay
315 {
316 1 unsigned int i;
317 1 while(count)
318 1 {
319 2 i=200;
320 2 while(i>0)
321 2 i--;
322 2 count--;
323 2 }
324 1 }
325
326 void dsreset(void) //DS18B20初始化
327 {
328 1 unsigned int i;
329 1 DS=0;
330 1 i=103;
331 1 while(i>0)i--;
332 1 DS=1;
333 1 i=4;
334 1 while(i>0)i--;
335 1 }
336
337 bit tmpreadbit(void) // 读一位
338 {
339 1 unsigned int i;
340 1 bit dat;
341 1 DS=0;i++; //小延时一下
342 1 DS=1;i++;i++;
343 1 dat=DS;
344 1 i=8;while(i>0)i--;
345 1 return (dat);
346 1 }
347
348 unsigned char tmpread(void) //读一个字节
349 {
350 1 unsigned char i,j,dat;
351 1 dat=0;
352 1 for(i=1;i<=8;i++)
353 1 {
354 2 j=tmpreadbit();
355 2 dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好//一个字节在DAT里
356 2 }
357 1 return(dat); //将一个字节数据返回
358 1 }
359
360 void tmpwritebyte(unsigned char dat)
361 { //写一个字节到DS18B20里
362 1 unsigned int i;
363 1 unsigned char j;
364 1 bit testb;
365 1 for(j=1;j<=8;j++)
C51 COMPILER V8.02 LCD12232 03/13/2009 19:43:48 PAGE 7
366 1 {
367 2 testb=dat&0x01;
368 2 dat=dat>>1;
369 2 if(testb) // 写1部分
370 2 {
371 3 DS=0;
372 3 i++;i++;
373 3 DS=1;
374 3 i=8;while(i>0)i--;
375 3 }
376 2 else
377 2 {
378 3 DS=0; //写0部分
379 3 i=8;while(i>0)i--;
380 3 DS=1;
381 3 i++;i++;
382 3 }
383 2 }
384 1 }
385 void tmpchange(void) //发送温度转换命令
386 {
387 1 dsreset(); //初始化DS18B20
388 1 delayb(1); //延时
389 1 tmpwritebyte(0xcc); // 跳过序列号命令
390 1 tmpwritebyte(0x44); //发送温度转换命令
391 1 }
392
393 int tmep() //获得温度
394 {
395 1 float tt;
396 1 int temp;
397 1 unsigned char a,b;
398 1 dsreset();
399 1 delayb(1);
400 1 tmpwritebyte(0xcc);
401 1 tmpwritebyte(0xbe); //发送读取数据命令
402 1 a=tmpread(); //连续读两个字节数据
403 1 b=tmpread();
404 1 temp=b;
405 1 temp<<=8;
406 1 temp=temp|a; //两字节合成一个整型变量。
407 1 tt=temp*0.0625; //得到真实十进制温度值,因为DS18B20
408 1 //可以精确到0.0625度,所以读回数据的最低位代表的是
409 1 //0.0625度。
410 1 temp=tt*10+0.5; //放大十倍,这样做的目的将小数点后第一位
411 1 //也转换为可显示数字,同时进行一个四舍五入操作。
412 1 return temp; //返回温度值
413 1 }
414
415 void readrom() //read the serial 读取温度传感器的序列号
416 { //本程序中没有用到此函数
417 1 unsigned char sn1,sn2;
418 1 dsreset();
419 1 delayb(1);
420 1 tmpwritebyte(0x33);
421 1 sn1=tmpread();
422 1 sn2=tmpread();
423 1
424 1 }
425
426 void delay10ms()
427 {
C51 COMPILER V8.02 LCD12232 03/13/2009 19:43:48 PAGE 8
428 1
429 1 unsigned char a,b;
430 1
431 1 for(a=10;a>0;a--)
432 1
433 1 for(b=60;b>0;b--);
434 1
435 1 }
436 /***********************************************************************/
437
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1066 ----
CONSTANT SIZE = 14 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 38 18
IDATA SIZE = ---- ----
BIT SIZE = ---- 2
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -