⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1.c

📁 实时时钟芯片DS12c887的51单片机驱动程序,能读出年月日时分秒等日历信息.
💻 C
字号:
//芯片:AT89C51
//主频:12MHz

#include "reg52.h"
#include "lcd12864_driver.h"
#define ds12887_out P1
sbit ds12887_cs= P3^2;
sbit ds12887_as =P3^3;
sbit ds12887_rw =P3^4;
sbit ds12887_ds =P3^5;
sbit set_s=P2^0	;
sbit DQ =P2^1;//根据实际情况定义18B20 
void s_init(void);
void dis_one_zi(uchar x_add,uchar y_add,uchar code *po,bit back_flag,bit char_moudle);
void dis_one_char(uchar x,uchar y,uchar asc_num,bit b_flag);

uchar code x0[32] = { //16*16汉字点阵字模
/*年   CC4EA */
0x00,0x00,0x20,0x30,0xF8,0xDC,0x4C,0x48,
0x48,0xF8,0xF8,0x48,0x48,0x48,0x48,0x08,
0x00,0x04,0x04,0x04,0x07,0x07,0x04,0x04,
0x04,0x7F,0x7F,0x04,0x04,0x04,0x04,0x04};
uchar code x1[32] = {
/*月   CD4C2 */
0x00,0x00,0x00,0x00,0xF8,0xF8,0x48,0x48,
0x48,0x48,0x48,0x48,0xF8,0xF8,0x00,0x00,
0x00,0x00,0x40,0x70,0x3F,0x0F,0x02,0x02,
0x02,0x02,0x42,0x42,0x7F,0x7F,0x00,0x00};
uchar code x2[32] = {
/*日   CC8D5 */
0x00,0x00,0x00,0xF8,0xF8,0x08,0x08,0x08,
0x08,0x08,0x08,0x08,0xF8,0xF8,0x00,0x00,
0x00,0x00,0x00,0x7F,0x7F,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x3F,0x3F,0x00,0x00};
uchar code x3[32] = {
/*星   CD0C7 */
0x00,0x00,0x00,0xF8,0xF8,0xA8,0xA8,0xA8,
0xA8,0xA8,0xA8,0xA8,0xF8,0xF8,0x00,0x00,
0x00,0x48,0x4C,0x4F,0x4B,0x4A,0x4A,0x4A,
0x7F,0x7F,0x4A,0x4A,0x4A,0x4A,0x42,0x40};
uchar code x4[32] = {
/*期   CC6DA */
0x00,0x10,0x10,0xFC,0xFC,0x50,0x50,0xFC,
0xFC,0x10,0xF8,0xF8,0x48,0x48,0xF8,0xF8,
0x00,0x44,0x64,0x3F,0x1F,0x05,0x15,0x37,
0x37,0x64,0x7F,0x1F,0x42,0x42,0x7F,0x7F};
uchar code x5[32] = {
/*时   CCAB1 */
0x00,0x00,0xF8,0xF8,0x88,0x88,0xF8,0xF8,
0x20,0x20,0x20,0x20,0xFC,0xFC,0x20,0x20,
0x00,0x00,0x3F,0x3F,0x08,0x08,0x1F,0x1F,
0x01,0x43,0x47,0x46,0x7F,0x7F,0x00,0x00};
 uchar code x6[32] = {
/*分   CB7D6 */
0x00,0x00,0x80,0xC0,0xE0,0x70,0x1C,0x0C,
0x04,0x00,0x0C,0x1C,0x70,0xE0,0xC0,0x80,
0x00,0x41,0x41,0x61,0x21,0x31,0x1F,0x0F,
0x01,0x41,0x41,0x61,0x7F,0x1F,0x01,0x01};
 uchar code x7[32] = {
/*秒   CC3EB */
0x00,0x40,0x48,0xC8,0xF8,0xF8,0x4C,0x44,
0xC0,0xF0,0x30,0xFC,0xFC,0x30,0x70,0xE0,
0x00,0x08,0x0E,0x07,0x7F,0x7F,0x43,0x43,
0x41,0x60,0x20,0x37,0x1F,0x1C,0x0E,0x02

};


//DS1820 子程序 
//这里以11.0592M晶体为例,不同的晶体速度可能需要调整延时的时间 

typedef unsigned char byte; 
typedef unsigned int word; 
//延时 
void delay(word useconds) 
{ 
	for(;useconds>0;useconds--); 
} 
//复位 
byte ow_reset(void)   //总线复位
{ 
	byte presence; 
	DQ = 0; //pull DQ line low 
	delay(29); // leave it low for 480us 
	DQ = 1; // allow line to return high 
	delay(3); // wait for presence 
	presence = DQ; // get presence signal 
	delay(25); // wait for end of timeslot 
	return(presence); // presence signal returned 
} // 0=presence, 1 = no part 
//从 1-wire 总线上读取一个字节 
byte read_byte(void) 	//读取DS12b80单线总线字节
{ 
	byte i; 
	byte value = 0; 
	for (i=8;i>0;i--) 
	{ 
		value>>=1; 
		DQ = 0; // pull DQ low to start timeslot 
		DQ = 1; // then return high 
		delay(1); //for (i=0; i<3; i++); 
		if(DQ)value|=0x80; 
			delay(6); // wait for rest of timeslot 
	} 
	return(value); 
} 
//向 1-WIRE 总线上写一个字节 
void write_byte(char val) //写入DS12b80单线总线字节
{ 
	byte i; 
	for (i=8; i>0; i--) // writes byte, one bit at a time 
	{ 
		DQ = 0; // pull DQ low to start timeslot 
		DQ = val&0x01; 
		delay(5); // hold value for remainder of timeslot 
		DQ = 1; 
		val=val/2; 
	} 
	delay(5); 
} 
 
char Read_Temperature(void)//读取温度 
{ 
	union 
	{
		byte c[2]; 
		int x; 
	}temp; 
	ow_reset(); 
	write_byte(0xCC); // Skip ROM 
	write_byte(0xBE); // Read Scratch Pad 
	temp.c[1]=read_byte(); 
	temp.c[0]=read_byte(); 
	ow_reset(); 
	write_byte(0xCC); //Skip ROM 
	write_byte(0x44); // Start Conversion 
	return temp.x/2; 
} 

uchar ds12887_read(uchar addr)//读取DS12C887数据
{
	uchar temp;
	ds12887_cs=0;
	ds12887_ds=0;
	ds12887_as=1;
	ds12887_out=addr;
	_nop_();
	ds12887_as=0;
	ds12887_rw=1;
	ds12887_ds=1;
	_nop_();	
	temp=ds12887_out;
	ds12887_ds=0;  
	ds12887_cs=1;
	return temp;
}

void ds12887_write(uchar addr,uchar sdata) //写DS12c887
{

	ds12887_cs=0;
	ds12887_ds=0;
	ds12887_as=1;
	ds12887_out=addr;
	_nop_();
	ds12887_as=0;
	ds12887_rw=0;
	ds12887_ds=1;
	ds12887_out=sdata;
	_nop_();	
	ds12887_ds=0;  
	ds12887_cs=1;
}	

uchar get_s() //获取DS12c887秒数据
{
	uchar uip,temp;
	while(uip & 0x80 !=0x80)
	{
		uip=ds12887_read(0x0a);
	}
	temp=ds12887_read(0) ;
	return temp;
}

uchar get_m() //获取DS12c887分数据
{
	uchar uip,temp;
	while(uip & 0x80 !=0x80)
	{
		uip=ds12887_read(0x0a);
	}
	temp=ds12887_read(2) ;
	return temp;
}

uchar get_h() //获取DS12c887时数据
{
	uchar uip,temp;
	while(uip & 0x80 !=0x80)
	{
		uip=ds12887_read(0x0a);
	}
	temp=ds12887_read(4) ;
	return temp;
}

uchar get_week() //获取DS12c887星期数据
{
	uchar uip,temp;
	while(uip & 0x80 !=0x80)
	{
		uip=ds12887_read(0x0a);
	}
	temp=ds12887_read(6) ;
	return temp;
}	

uchar get_day()////获取DS12c887天数据
{
	uchar uip,temp;
	while(uip & 0x80 !=0x80)
	{
		uip=ds12887_read(0x0a);
	}
	temp=ds12887_read(7) ;
	return temp;
}		
uchar get_month() ////获取DS12c887月数据
{
	uchar uip,temp;
	while(uip & 0x80 !=0x80)
	{
		uip=ds12887_read(0x0a);
	}
	temp=ds12887_read(8) ;
	return temp;
}		

uchar get_year() //获取DS12c887年数据
{
	uchar uip,temp;
	while(uip & 0x80 !=0x80)
	{
		uip=ds12887_read(0x0a);
	}
	temp=ds12887_read(9) ;
	return temp;
}

void delay1() //延时1S
{
 	unsigned int i;
	unsigned char j;
	for(i=0;i<1000;i++)
		for(j=0;j<72;j++);
}	

void main(void)	//主函数
{
	uchar i,a,b,c;
	lcd_init();
	
	
	//ds12887_write(0x0a,0x20);	 //开启时钟芯片
	//ds12887_write(9,7);
	//ds12887_write(8,3);
	//ds12887_write(7,11);
	//ds12887_write(6,7);
	ds12887_write(4,19);
	ds12887_write(2,35);
	ds12887_write(0,0);
	
	while(1)
	{
		i=get_year();  //显示年
		a=i/100;
		b=(i-a*100)/10;
		c=(i-a*100)%10;
		dis_one_char(0,0,'2',1) ;
		dis_one_char(0,1,'0',1) ;
		dis_one_char(0,2,b+0x30,1) ;
		dis_one_char(0,3,c+0x30,1) ;
		dis_one_zi(0,2,x0,1,1);	
	
	
		i=get_month();
		a=i/100;
		b=(i-a*100)/10;
		c=(i-a*100)%10;
	
		dis_one_char(0,6,b+0x30,1) ;
		dis_one_char(0,7,c+0x30,1) ;
		dis_one_zi(0,4,x1,1,1);	
	
	
	
		i=get_day();
		a=i/100;
		b=(i-a*100)/10;
		c=(i-a*100)%10;
		dis_one_char(0,10,b+0x30,1) ;
		dis_one_char(0,11,c+0x30,1) ;
		dis_one_zi(0,6,x2,1,1);	
	
	
		i=get_week();
		a=i/100;
		b=(i-a*100)/10;
		c=(i-a*100)%10;
	
		dis_one_char(1,9,c+0x30,1) ;
		dis_one_zi(1,2,x3,1,1);	
		dis_one_zi(1,3,x4,1,1);	
	
		i=get_h();
		a=i/100;
		b=(i-a*100)/10;
		c=(i-a*100)%10;
		dis_one_char(2,2,b+0x30,1) ;
		dis_one_char(2,3,c+0x30,1) ;
		dis_one_zi(2,2,x5,1,1);
			
	
		i=get_m();
		a=i/100;
		b=(i-a*100)/10;
		c=(i-a*100)%10;
		dis_one_char(2,6,b+0x30,1) ;
		dis_one_char(2,7,c+0x30,1) ;
		dis_one_zi(2,4,x6,1,1);
	
		i=get_s();
		a=i/100;
		b=(i-a*100)/10;
		c=(i-a*100)%10;
		dis_one_char(2,10,b+0x30,1) ;
		dis_one_char(2,11,c+0x30,1) ;
		dis_one_zi(2,6,x7,1,1);	
	
		delay1();  //延时1秒
		dis_cls(); //刷新屏幕
		i=Read_Temperature(); //读取温度
		a=i/100;			  //显示温度
		b=(i-a*100)/10;
		c=(i-a*100)%10;
		dis_one_char(0,10,b+0x30,1) ;
		dis_one_char(0,11,c+0x30,1) ;
		dis_one_zi(0,6,x2,1,1);	
		}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -