74HC573与ds1302基本使用实例代码

板子这样化

74HC573与ds1302基本使用实例代码_第1张图片
效果
74HC573与ds1302基本使用实例代码_第2张图片

源代码

ds1302.c

#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar sec,min,hour,date,month,week,year;
sbit rst = P3^0;
sbit sda = P3^2;
sbit scl = P3^1;
/*对DS1302初始化*/
void ds1302_init(void)
{
	rst = 0;
	scl = 0;
	rst = 1;
}
/*对DS1302写1字节函数*/
void write_ds1302_onebyte(uchar dat)	   
{
	uchar i;
	for(i = 0;i < 8;i++)
	{
		sda = (bit)(dat & 0x01);
		scl = 0;
		scl = 1;
		dat = dat >> 1;
	}
}
/*从DS1302读一字节函数*/
uchar read_ds1302_onebyte(void)	
{
	uchar i,dat;
	for(i = 0;i < 8;i++)
	{
		scl = 1;
		scl = 0;
		dat = dat >> 1;
		if(sda)dat = dat | 0x80;
	}
	return(dat);	
}
/*对DS1302的某一地址写一字节函数*/
void write_ds1302_add_dat(uchar add,uchar dat)
{
	ds1302_init();
	write_ds1302_onebyte(add);
	write_ds1302_onebyte(dat);
	scl = 1;
		rst = 0;
}
/*从DS1302的某一地址读一字节函数*/
uchar read_ds1302_add(uchar add)				//读1302数据
{
	uchar dat_temp;
	ds1302_init();
	write_ds1302_onebyte(add);
	dat_temp = read_ds1302_onebyte();
	scl = 1;
	rst = 0;
	return(dat_temp);
}
/*8421BCD码到十进制转换*/
uchar BCD_DEC_conv(uchar x)
{
	uchar dec;
	dec =  0x0f & x;
	x = x >> 4;
	dec	= dec + x * 10;
	return(dec);
}
/*十进制到8421BCD码转换*/
uchar DEC_BCD_conv(uchar x)
{
	uchar bcd;
	bcd =  x % 10;
	x = x / 10;
	x = x << 4;
	bcd	= bcd | x ;
	return(bcd);
}
 /*获得时间信息函数*/
void get_ds1302_time(void)    //获取1302的时间数据(时、分、秒),存入time1数组中
{
	uchar d;
	d = read_ds1302_add(0x81);				//读秒
	sec = BCD_DEC_conv(d);				//得到秒	
	d = read_ds1302_add(0x83);				//读分
	min = BCD_DEC_conv(d);  			//得到分
	d = read_ds1302_add(0x85);				//读小时
	hour = BCD_DEC_conv(d);  			//得到小时
	d = read_ds1302_add(0x87);				//读日
	date = BCD_DEC_conv(d);  			//得到日
   	d = read_ds1302_add(0x89);				//读月
	month = BCD_DEC_conv(d);  			//得到月
	d = read_ds1302_add(0x8b);				//读星期
	week = BCD_DEC_conv(d);  			//得到星期
	d = read_ds1302_add(0x8d);				//读年
	year = BCD_DEC_conv(d);  			//得到年		
}
/*结束*/

main.c

#include<reg51.h>
#include "ds1302.c"
uchar cp1,cp2,cp3;
uchar seven_seg[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar flash;
bit conv;
sbit P1_0  =  P2^0;
void time0_isr(void) interrupt 1	 //利用中断对数码管上显示的数据进行刷新
{
	TH0 = (65536-2000)/256;	
	TL0 = (65536-2000)%256;
	cp1++;
	if(cp1 >= 250)//0.5秒
	{
		cp1 = 0;
		flash = ~flash; //产生小数点闪烁变量
		cp2++;
		if(cp2 >= 5)
		{
			conv = !conv;//产生交替显示变量
			cp2 = 0;
		}
		
	}
	P0 = 0xff;//消隐
	if(conv == 1)
	{
	switch(cp3)
		{
			case 0: 	P0 = 0x01;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[sec % 10];break;
			case 1: 	P0 = 0x02;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[sec / 10];break;
			case 2: 	P0 = 0x04;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = 0xfd | flash; break;//显示闪烁的“—”
			case 3: 	P0 = 0x08;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[min % 10];break;
			case 4: 	P0 = 0x10;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[min / 10];break;
			case 5: 	P0 = 0x20;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = 0xfd | flash; break;//显示闪烁的“—”
			case 6: 	P0 = 0x40;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[hour % 10];break;
			case 7: 	P0 = 0x80;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[hour / 10];break;
		}
	}
	else
	{	
		switch(cp3)
		{
			case 0: 	P0 = 0x01;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[date % 10];break;
			case 1: 	P0 = 0x02;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[date / 10];break;
			case 2: 	P0 = 0x04;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = 0xfd | flash; break;//显示闪烁的“—”
			case 3: 	P0 = 0x08;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[month % 10];break;
			case 4: 	P0 = 0x10;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[month / 10];break;
			case 5: 	P0 = 0x20;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = 0xfd | flash; break;//显示闪烁的“—”
			case 6: 	P0 = 0x40;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[year % 10];break;
	                                    case 7: 	P0 = 0x80;P1_0 = 0; P1_0 = 1; P1_0 = 0;
				P0 = seven_seg[hour / 10];break;
		}	}
		cp3++;
	if(cp3 >= 8)cp3 = 0;			
}
void timer0_init(void)			//Timer0初始化
{
	TMOD = 0x01;
	TH0 = (65536 -2000) / 256;	
	TL0 = (65536-2000) % 256;
	TR0 = 1;
	ET0 = 1;
	EA  = 1;
}
void main(void)
{
	uchar i = 46;//举例,比如要调整时间,分钟设定为46分
	i = DEC_BCD_conv(i);
	timer0_init();
	write_ds1302_add_dat(0x8e,0x00);	//写操作,可以对DS1302调整
	write_ds1302_add_dat(0x80,0x30);	//写秒,30秒
	write_ds1302_add_dat(0x82,i);		//写分,46分
	write_ds1302_add_dat(0x84,0x12);	//写时,12时
	write_ds1302_add_dat(0x86,0x28);	//写日,28日
	write_ds1302_add_dat(0x88,0x05);	//写月,5月 
	write_ds1302_add_dat(0x8a,0x03);	//写星期,星期三
	write_ds1302_add_dat(0x8c,0x12);	//写年,(20)12年
	write_ds1302_add_dat(0x8e,0x80);	//写保护
	while(1)
	{
		get_ds1302_time();
	}
}


专业课小作业ds1302了解,代码复制就可使用
单片机怎么也与前端合不来啊,那位大佬也可分享单片机与前端领域的应用结合吗?感谢感谢~~~

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