在LCD1602上面显示DS1302掉电时钟和日期

主函数文件:

#include 
#include "./delay/delay.h"

#define ds1302_sec_add			0x80
#define ds1302_min_add			0x82		
#define ds1302_hr_add			0x84	
#define ds1302_date_add			0x86	
#define ds1302_month_add		0x88		
#define ds1302_day_add			0x8a		
#define ds1302_year_add			0x8c
#define ds1302_charger_add		0x90 		
#define ds1302_control_add		0x8e							 
#define ds1302_clkburst_add		0xbe

sbit RST = P1^0;
sbit SCK = P1^1;
sbit IO = P1^2;
sbit RS = P2^4;
sbit RW = P2^5;
sbit E  = P2^6;

#define LCDPORT P0
#define LCD_WRITE_DATA 1
#define LCD_WRITE_COM  0

unsigned char timebuf[7] = {16,48,40,16,8,17,3};
unsigned char tempbuf[3];
unsigned char writebuf[7];
unsigned char readbuf[7];
unsigned char disbuf[3];

void lcd1602_write(unsigned char byte,unsigned char flag)
{
	  if(flag)
		{
			  RS = 1;
		}
		else
		{
			  RS = 0; 
		}
		RW = 0; 
		E = 1; 
		LCDPORT = byte;
		delay_us(5);
		E = 0;
}

void lcd1602_dis_str(unsigned char x, unsigned char y, unsigned char *str)
{
	  if((x > 15) || (y > 1))
		{
			  return ;
		}
		if(0 == y)
		{
			  lcd1602_write(0x80 + x,LCD_WRITE_COM);
		}
		else
		{
			  lcd1602_write(0x80 + 0x40 + x,LCD_WRITE_COM);
		}
		while(*str != '\0')
		{
			  lcd1602_write(*str,LCD_WRITE_DATA);
			  str++;
		}
}

void lcd_init()
{
	  delay_ms(15);
	  lcd1602_write(0x38,LCD_WRITE_COM);
	  delay_ms(5);
		lcd1602_write(0x38,LCD_WRITE_COM); 
	  delay_ms(5);
		lcd1602_write(0x38,LCD_WRITE_COM); 
	  delay_ms(5);
		lcd1602_write(0x38,LCD_WRITE_COM); 
	  delay_ms(5);
		lcd1602_write(0x08,LCD_WRITE_COM); 
	  delay_ms(5);
		lcd1602_write(0x01,LCD_WRITE_COM); 
	  delay_ms(5);
	  lcd1602_write(0x06,LCD_WRITE_COM); 
	  delay_ms(5);
		lcd1602_write(0x0c,LCD_WRITE_COM); 
	  delay_ms(5);
}

void ds1302_write_byte(unsigned char addr, unsigned char byte)
{
	  unsigned char i;
	  addr = addr & 0xfe;
	  SCK = 0;
	  RST = 0;
	 
	  RST = 1;
	  for(i = 0; i < 8; i++)
	  {
			  IO = addr & 0x01;
			  SCK = 0;
			  SCK = 1;
				addr >>= 1;
  	}
		for(i = 0; i < 8; i++)
	  {
			  IO = byte & 0x01;
			  SCK = 0;
			  SCK = 1;
				byte >>= 1;
  	}
		
	  SCK = 0;
	  RST = 0;
}

unsigned char ds1302_read_byte(unsigned char addr)
{
	  unsigned char i;
	  unsigned char temp;
	  addr = addr & 0xfe;
	  SCK = 0;
	  RST = 0;
	  
	  RST = 1;
	
	  addr = addr + 1;
	
	  for(i = 0; i < 8; i++)
	  {
			  IO = addr & 0x01;
			  SCK = 0;
			  SCK = 1;
				addr >>= 1;
  	}
		for(i = 0; i < 8; i++)
		{
			  SCK = 1;
			  SCK = 0;
			  temp >>= 1;
			  if(IO)
				{ 
					  temp += 0x80;
				}
		}
		RST = 0;
		return temp;
}

void ds1302_write_time()
{
	  unsigned char temp;
	  unsigned char temp1;
	  unsigned char i;
	  for(i = 0; i < 7; i++)
	  {
			  temp = timebuf[i] / 10;
			  temp1 = timebuf[i] % 10;
			  writebuf[i] = (temp << 4) | temp1;
		}
		ds1302_write_byte(ds1302_control_add,0x00);
		ds1302_write_byte(ds1302_hr_add,writebuf[0]);
		ds1302_write_byte(ds1302_min_add,writebuf[1]);
		ds1302_write_byte(ds1302_sec_add,writebuf[2]);
		ds1302_write_byte(ds1302_year_add,writebuf[3]);
		ds1302_write_byte(ds1302_month_add,writebuf[4]);
		ds1302_write_byte(ds1302_date_add,writebuf[5]);
		ds1302_write_byte(ds1302_day_add,writebuf[6]);
		ds1302_write_byte(ds1302_control_add,0x80);
}

void ds1302_read_time()
{
	  unsigned char i;
		unsigned char temp;
	  unsigned char temp1;
    readbuf[0] = ds1302_read_byte(ds1302_hr_add);
	  readbuf[1] = ds1302_read_byte(ds1302_min_add);
	  readbuf[2] = ds1302_read_byte(ds1302_sec_add);
	  readbuf[3] = ds1302_read_byte(ds1302_year_add);
	  readbuf[4] = ds1302_read_byte(ds1302_month_add);
	  readbuf[5] = ds1302_read_byte(ds1302_date_add);
	  for(i = 0; i < 6; i++)
	  {
			  temp = (readbuf[i] >> 4);
			  temp1 = (readbuf[i] & 0x0f);
			  disbuf[i] = temp * 10 + temp1;
		}
		switch(ds1302_read_byte(ds1302_day_add))
		{
			  case 1:
				{
					  lcd1602_dis_str(12,1,"MON");
					  break;
				}
				case 2:
				{
					  lcd1602_dis_str(12,1,"TUE");
				    break;
        }
				case 3:
				{
					  lcd1602_dis_str(12,1,"WED");
					  break;
				}
				case 4:
				{
					  lcd1602_dis_str(12,1,"THU");
					  break;
				}
				case 5:
				{
					  lcd1602_dis_str(12,1,"FRI");
					  break;
				}
				case 6:
				{
					  lcd1602_dis_str(12,1,"SAT");
					  break;
				}
				case 7:
				{
					  lcd1602_dis_str(12,1,"SUN");
				    break;
        }
		}
}

void lcd_dis_time()
{
	  unsigned char lcddisbuf[9] = {0};
    unsigned char daydisbuf[9] = {0};
    lcddisbuf[0] = (disbuf[0] / 10) + 0x30;
    lcddisbuf[1] = (disbuf[0] % 10) + 0x30;
    lcddisbuf[2] = ':';
    lcddisbuf[3] = (disbuf[1] / 10) + 0x30;
    lcddisbuf[4] = (disbuf[1] % 10) + 0x30;
    lcddisbuf[5] = ':';
    lcddisbuf[6] = (disbuf[2] / 10) + 0x30;
    lcddisbuf[7] = (disbuf[2] % 10) + 0x30;

    daydisbuf[0] = (disbuf[3] / 10) + 0x30;
    daydisbuf[1] = (disbuf[3] % 10) + 0x30;
    daydisbuf[2] = '-';
    daydisbuf[3] = (disbuf[4] / 10) + 0x30;
    daydisbuf[4] = (disbuf[4] % 10) + 0x30;
    daydisbuf[5] = '-';
    daydisbuf[6] = (disbuf[5] / 10) + 0x30;
    daydisbuf[7] = (disbuf[5] % 10) + 0x30;

    lcd1602_dis_str(4,0,lcddisbuf);
    lcd1602_dis_str(1,1,daydisbuf);
}

void main()
{
	  lcd_init();
	  ds1302_write_time();
	  while(1)
		{
			  ds1302_read_time();
			  lcd_dis_time();
		}
}



延时函数文件:

void delay_us(unsigned char t)
{
    while(--t);
}

void delay_ms(unsigned char t)
{
    while(t--)
		{
        delay_us(245);
			  delay_us(245);
    }
}

void delay_s(unsigned char t)
{
    while(t--)
    {
		    delay_ms(200);	
			  delay_ms(200);
			  delay_ms(200);
			  delay_ms(200);
			  delay_ms(200);
    }
}


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