Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7678703
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-01-05 16:08:04

//显示程序(一共两个文件第一个LCD显示,第二个读DS18B20数据)

#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define NOP _nop_()
sbit RS=P2^6; sbit E=P2^7; sbit RW=P2^5; sbit BEEP = P1^5;
sbit P3_2=P3^2; sbit P3_3=P3^3;
void delay (void); void init(void); void wc51r(uchar i); void wc51ddr (uchar i);
void fbusy( ); void week_show(); void date_show(); void Lcd_show();
void nwc51r (uchar i); void time_show(); void Tem_show();
void delays(); void beep(); void DS18B20_show();
int de;bit bbs;
uchar date=26,week,moth=6,second,mintue,hour,j,i=0,k=0;
uchar code Cutime[]={"Current time is:"};
uchar code Cudate[]={"Current date is:"};
uchar code Cuweek[]={"Current week is:"};
uchar code Tem[]={"Temperature:"};
uchar code Etem[]={"Error:"};
extern bit flag;
extern unsigned char temp_dis[6];
extern void Disp_temperatuer();extern void read_bytes (void);
uchar weekd[7][3]={{'S','u','n'},{'M','o','n'},{'T','u','e'},{'W','e','d'},{'T','h','u'},{'F','r','i'},{'S','t','a'}};
uchar dated[10]={'2','0','1','0','/','0','6','/','2','5'};
void main( )
{
  SP=0x66;
 
  init ();
  DS18B20_show();
  week_show();
  date_show();
 while(1)
 {
 while(k<4) time_show();
 while(k==4) week_show();
 while(k==5) date_show();
 }
}
void DS18B20_show()
{
 EA=0;
 read_bytes();
 Disp_temperatuer();
 while(!bbs){wc51r(0xca);break;}
 while(bbs){wc51r(0xd1);break;}
 delay();
 while(flag)
 {
  wc51ddr('*'); delay();
  wc51ddr(temp_dis[2]); delay();
  wc51ddr(temp_dis[1]); delay();
  wc51ddr('.'); delay();
  wc51ddr(temp_dis[0]); delay();
  wc51ddr('C'); delay();
  break;
 }
 while(!flag)
 {
  uchar ei;
  delay();
  for (ei=0;ei<5;ei++) { wc51ddr(Etem[ei]);delay();}
  break;
 }
 delay(); EA=1;
 while(20<=temp_dis[5]<=25)
 {
  P1=0xff;break;
 }
 
}
void beep()
{
  unsigned char bep;
  for (bep=0;bep<45;bep++)
   {
     BEEP=!BEEP;
  delay( );
   }
  BEEP=1;
}
void Lcd_show()
{
 wc51r (0x01); delay( ); delay();delay(); delay();delay();
 week_show(); date_show(); DS18B20_show(); // time_show();

}
modification2() interrupt 2
{
 beep();
 for(de=0;de<200;de++) { delay( ); } //延时防抖?

 
 while(k==0) //温度显示

 {
  Tem_show();
  delays();
  Lcd_show();
  beep();
  break;
 }
 
 while(k==1){second++;
 if(second==60){second=0;}break;}
 while(k==2){mintue++;
 if(mintue==60){mintue=0;}break;}
 while(k==3){hour++;
 if(hour==24){hour=0;} break;}
 while(k==4){week++;
 if(week==7){week=0;} break;}
 
 while(k==5){date++;
 if(date==31){date=0;} break;}
    
}
void delays()
{
 uchar Del_0,Del_1,Del_2;
 for(Del_0=0;Del_0<50;Del_0++)
 for(Del_1=0;Del_1<100;Del_1++)
 for(Del_2=0;Del_2<0xff;Del_2++);
}
void Tem_show() //温度显示

{
 wc51r (0x01); delay( ); delay();delay(); delay();delay();
 wc51r (0x07); delay( );
 wc51r(0x8e);
 delay();
 for (i=0;i<12;i++)
 {
  wc51ddr(Tem[i]);
  for(de=0;de<250;de++) { delay( ); }
 }
 wc51r (0x06); delay( ); wc51r (0x06); delay( );
 bbs=1;DS18B20_show();bbs=0;
}
 
void date_show() //日期显示

{
 EA=0;
 while(k==0){wc51r(0x86);break;}
 while(k==5){wc51r(0xc3);break;}
 
 delay();
 dated[5]=moth/10+0x30;
 dated[6]=moth%10+0x30;
 
 dated[8]=date/10+0x30;
 dated[9]=date%10+0x30;
 for (i=0;i<10;i++)
 {
  wc51ddr(dated[i]); delay();
 }
 while(k==5){wc51ddr('_'); delay(); break;}
 EA=1;
 delay();delay();delay();delay();
}
void week_show() //星期显示

{
 EA=0;
 while(k==0){wc51r(0x80);break;}
 while(k==4){wc51r(0xc7);break;}
 for(i=0;i<3;i++)
 {
  wc51ddr (weekd[week][i]); delay();
 }
  
 EA=1;
 delay();delay();delay();delay();
}
void time_show() //时间显示

{
 EA=0;
  while(k!=0){wc51r (0xc4);break;}
 while(k==0){wc51r (0xc0);break;}
 delay();
    wc51ddr (hour/10+0x30);
    wc51ddr (hour%10+0x30);
 while(k!=3){ wc51ddr (':'); break;}
 while(k==3){ wc51ddr ('_'); break;}
 
 wc51ddr (mintue/10+0x30);
    wc51ddr (mintue%10+0x30);
  
 while(k==2){ wc51ddr ('_'); break;}
 while(k!=2){ wc51ddr (':'); break;}
  
 wc51ddr (second/10+0x30);
 wc51ddr (second%10+0x30);
 while(k==1){ wc51ddr ('_'); break;}
 while(k!=2){ wc51ddr (' '); break;}
 EA=1;
 delay();delay();delay();delay();delay();delay();
}
 void init ( )
 {
   TMOD=0X01;
   TH0=0X3c;
   TL0=0Xb0;
   EA=1; ET0=1; TR0=1; EX1=1; EX0=1;
 
      IT0=1; IT1=1;
      PX1=1;
   for(de=0;de<1000;de++) { delay( ); }
 
    nwc51r (0x38); delay( );nwc51r (0x38); delay( );nwc51r (0x38); delay( );
      nwc51r (0x0c); delay( ); nwc51r (0x06); delay( ); nwc51r (0x01); delay( );
 }
void fbusy( )
{
 RS=0;RW=1;
 NOP;NOP;NOP;NOP;
 E=1;
 delay();
 while(P0&0x80);
 E=0;
 delay();
}
void nwc51r (uchar i)
  {
   E=0; RS=0; RW=0;
   P0=i;
   NOP;NOP;
   E=1;
   NOP;NOP;NOP;NOP;
   E=0;
   delay();
 }
void wc51r (uchar i)
  {
   fbusy ();
   E=0; RS=0; RW=0;
   P0=i;
   NOP;NOP;
   E=1;
   NOP;NOP;NOP;NOP;
   E=0;
   delay();
 }
void wc51ddr (uchar i)
  {
   fbusy ();
   E=0; RS=1; RW=0;
 
   P0=i;
   NOP;NOP;
   E=1;
   NOP;NOP;NOP;NOP;
   E=0;
   delay();
  }
  void delay( )
  {
   uchar y;
   for (y=0;y<0xff;y++);
  }
modification1() interrupt 0
{
 beep();
 for(de=0;de<200;de++) delay( );
 k++;
 nwc51r (0x01); delay( );
 wc51r(0x80);
 delay();
 while(k<4)
 {
 for (i=0;i<16;i++) { wc51ddr( Cutime[i]); delay(); }
 break;
 }
 while(k==4)
 {
  for (i=0;i<16;i++) { wc51ddr( Cuweek[i]); delay(); }
  break;
 }
 while(k==5)
 {
  for (i=0;i<16;i++) { wc51ddr( Cudate[i]); delay(); }
  break;
 }
    
 if(k==6)
  {
   k=0; Lcd_show();
  }
}
time() interrupt 1
{
   TH0=0x3c; TL0=0xb0;
   j++;
   while(j==20){j=0;second++;while(k==0){DS18B20_show(); break;}}
   while(second==60){second=0;mintue++;}
   while(mintue==60){mintue=0;hour++;}
   while(hour==24)
   {
    hour=0;date++;
   while((date%7)==0){ week++;break; }
   }
   while(week==7){week=0;}
   while(date==30){date=0;moth++;}
   while(moth==12)moth=0;
}
 
 
 
第二个
#include <reg52.H>
#include <intrins.h>
sbit D18B20=P3^7; bit flag;
unsigned char Temperatured;
unsigned char temp_data[2];
unsigned char temp_dis[6]={0xFF,0xFF,0xFF,0xFF,0xFF};
unsigned char temp_tab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x07,0x08,0x09,0x09};
void Disp_temperatuer();void read_bytes (void); void TempDelay (unsigned int us);
void Init18b20 (void);void WriteByte (unsigned char idata wr); unsigned char ReadByte (void);
void Disp_temperatuer()
{
   temp_dis[4]= temp_data[0]&0x0f;
   temp_dis[0]= temp_tab[temp_dis[4]]+0x30;
   temp_dis[3]= (((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x0f)<<4));
   temp_dis[5]= (temp_dis[3]%100);
   temp_dis[2]= (temp_dis[3]%100)/10+0x30;
   temp_dis[1]= temp_dis[3]%10+0x30;
}
void read_bytes (void)
{
  Init18b20();
  WriteByte(0xcc); WriteByte(0x44);
  TempDelay(5000);
  Init18b20();
  WriteByte(0xcc); WriteByte(0xbe);
 
  temp_data[0] = ReadByte(); TempDelay(2);
  temp_data[1] = ReadByte(); TempDelay(2);
}

unsigned char ReadByte (void) //读取单字节

{
 unsigned char idata Ti,u=0;
 for(Ti=0;Ti<8;Ti++)
 {
  D18B20 = 0;
  u >>= 1;
  D18B20 = 1;
  TempDelay(3);
  if(D18B20==1)
  u |= 0x80;
  TempDelay (1);
  _nop_();
  D18B20 = 1;
 }
 return(u);
}
void WriteByte (unsigned char idata wr) //单字节写入

{
 unsigned char idata i;
 for (i=0;i<8;i++)
 {
  D18B20 = 0;
  _nop_();
  D18B20=wr&0x01;
  TempDelay(3); //delay 45 uS //5

  _nop_();
  _nop_();
  D18B20=1;
  wr >>= 1;
  _nop_();
 }
}
void Init18b20 (void)
{
 D18B20=1;
 _nop_();
 D18B20=0;
 TempDelay(100); //delay 530 uS//80

 _nop_();
 D18B20=1;
 TempDelay(13); //delay 100 uS//14

 _nop_();
 _nop_();
 _nop_();
 
 if(D18B20==0)
  flag = 1; //detect 1820 success!

 else
  flag = 0; //detect 1820 fail!

 TempDelay(10); //20

 _nop_();
 _nop_();
 D18B20 = 1;
}
void TempDelay (unsigned int us)
{
 while(us--);
}
  


阅读(994) | 评论(0) | 转发(2) |
0

上一篇:时钟

下一篇:LED时种显示

给主人留下些什么吧!~~