DS1302测出时间并且在LCD1602上面显示

主函数文件:

#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_control_add		0x8e		
#define ds1302_charger_add		0x90 					 
#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[3] = {19,27,55};
unsigned char tempbuf[3];
unsigned char writebuf[3];
unsigned char readbuf[3];
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 < 3; 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_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);
	  for(i = 0; i < 3; i++)
	  {
			  temp = (readbuf[i] >> 4);
			  temp1 = (readbuf[i] & 0x0f);
			  disbuf[i] = temp * 10 + temp1;
		}
}

void lcd_dis_time()
{
	  unsigned char lcddisbuf[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;

    lcd1602_dis_str(4,0,lcddisbuf);
}

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);
    }
}


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