DS1302LCD1602显示

在学习的过程 这个网站帮了很多忙 虽然很多资料都要下载  我就把代码直接贴出来吧

#include 
#include 

typedef unsigned char uchar;

#define DS1302_W_ADDR 0x80
#define DS1302_R_ADDR 0x81
/*******************DS1302********************/
//              Ãë ·Ö Ê±
uchar TIME[3] = {35,16,23};

sbit SCLK = P1^0;
sbit IO   = P1^1;
sbit RST  = P1^2;

//д×Ö½Ú
void DS1302write_byte(uchar byte)
{
    uchar i;
	  for(i = 0; i < 8; i++)
	 {
	    SCLK = 0;          //À­µÍʱÖÓÏß
		  IO = byte & 0x01; 
      byte >>= 1;     
      SCLK = 1; 		    //À­¸ßʱÖÓÏß ²úÉúÉÏÉýÑØ Ð´ÈëÊý¾Ý
	 }
}
//¶Á×Ö½Ú
uchar DS1302read_byte()
{
     uchar i,dat;
     for(i = 0; i < 8; i++)
      {
			  SCLK = 0;
				dat >>= 1;
				if(IO) dat |= 0x80;
			  SCLK = 1;	
			}  
     return dat;
}
//дÊý¾Ý
void DS1302write_dat(uchar addr,uchar dat)
{
   
	  DS1302write_byte(addr);
	  DS1302write_byte(dat);
	  
}
//¶ÁÊý¾Ý
uchar DS1302read_dat(uchar addr)
{
	 uchar dat;
	 DS1302write_byte(addr);
	 dat = DS1302read_byte();
   return dat;
}

//Çå³ýд±£»¤
void DS1302clear_wp()
{
	RST = 0;
	SCLK = 0;
	RST = 1;
  DS1302write_dat(0x8e,0x00);
	
}
//ÉèÖÃд±£»¤
void DS1302set_wp()
{
	
  DS1302write_dat(0x8e,0x80);
	SCLK = 0;
	RST = 0;
}
// ÉèÖÃÆðʼʱ¼ä
//Ê®½øÖÆתBCD
void DS1302set_time(uchar addr)
{
  uchar i,j;
	 
	DS1302clear_wp();
	for(i = 0; i < 3; i++)
	{
		
	  j=TIME[i]/10;
		TIME[i]=TIME[i]%10;
		TIME[i]=TIME[i]+j*16;	
	  DS1302write_dat(addr,TIME[i]);  
    addr += 2;
	
	}
	DS1302set_wp();
}
//¶ÁDS1302µÄÊý¾Ý
//¶ÁÈ¡BCDÂë
void DS1302read_time(uchar addr)
{
   uchar i;
	 
	 DS1302clear_wp();
	 for(i = 0; i < 3; i++)
	 {
		 
		 TIME[i] = DS1302read_dat(addr);
		 addr += 2;
	
   }
	 DS1302set_wp();
}
/*********************LCD1602ÏÔʾ***********************************/
sbit RS = P3^5;
sbit RW = P3^6;
sbit EN = P3^4;

//ÅÐæ
void read_busy()
{
	  uchar busy;
    RS = 0;
    RW = 1;
	  P0 = 0xff;
   do{
	   EN = 1;
		 busy = P0;
     EN = 0;		 
	  }while(busy & 0x80);
}
//дÃüÁî
void write_cmd(uchar cmd)
{
	  read_busy();
    RS = 0;
	  RW = 0;
	  P0 = cmd;
	  EN = 1;
	  EN = 0;
}
//дÊý¾Ý
void write_dat(uchar dat)
{
   read_busy();
	 RS = 1;
	 RW = 0;
   P0 = dat;
	 EN = 1;
	 EN = 0;
}
//ÏÔʾһ¸ö×Ö·û
void dis_onechar(uchar x,uchar y,uchar dat)
{
	if(y) x |= 0xc0;
	  x |= 0x80;
	write_cmd(x);
  write_dat(dat); 
}
//ÏÔʾһ¸ö×Ö·û´®
void dis_str(uchar x,uchar y,uchar *str)
{
   if(y) x |= 0xc0;
	   x |= 0x80;
	write_cmd(x);
	while(*str != '\0')
	{
	  P0 = *str;
		str++;
	}
}
void LCD1602_init()
{
   write_cmd(0x38);
	 write_cmd(0x0c);
	 write_cmd(0x06);
	 write_cmd(0x01);//ÇåÆÁ
}
/**********************Ö÷º¯Êý****************************************/
void main()
{
   DS1302set_time(0x80);
	 LCD1602_init();
   
	 while(1)
	 {
	 DS1302read_time(0x81);
	 dis_onechar(7,0,TIME[2]/16 + '0'); //ʱ
	 dis_onechar(8,0,TIME[2]%16 + '0');
	 dis_onechar(9,0,':');
		 
	 dis_onechar(10,0,TIME[1]/16 + '0'); //·Ö
	 dis_onechar(11,0,TIME[1]%16 + '0');
	 dis_onechar(12,0,':');
		
	 dis_onechar(13,0,TIME[0]/16 + '0');//Ãë
	 dis_onechar(14,0,TIME[0]%16 + '0');
	 }
}

 

你可能感兴趣的:(单片机)