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

📄 时钟ds1302的读写d7279显示显示分时.txt

📁 功能:时钟DS1302的读写 hd7279显示显示分 小时 说明: (1)每次上电时
💻 TXT
字号:
/****************************************************************
名称:ds1302.c
功能:时钟DS1302的读写   hd7279显示显示分 小时
说明:    (1)每次上电时,必须把秒寄存器高位设置为0,时钟才能走时
        (2)如果每次需要写入数据和时钟日历信息,须将"写保护"寄存器设置成为0
        (3)P0低4位接段码,高4位接位选,从DS1302中读出来的是BCD码
        (4)TS=1010,DS=01,RS=01(在Vcc1与Vcc2之间接2K电阻)
        (5)初始时间设置为05年 6月 10日 星期1 8:00
*****************************************************************/
#include <reg51.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define CMD_RESET 0xa4
#define CMD_TEST 0xbf
#define DECODE0 0x80
#define DECODE1 0xc8
#define CMD_READ 0x15
#define UNDECODE 0x90
#define RTL_CYCLE 0xa3
#define RTR_CYCLE 0xa2
#define RTL_UNCYL 0xa1
#define RTR_UNCYL 0xa0
#define ACTCTL 0x98
#define SEGON 0xe0
#define SEGOFF 0xc0
#define BLINKCTL 0x88
sbit IO=P3^6;//数据线                            //ds1302 io
sbit CLK=P3^5;//时钟线                           //clk
sbit RST=P3^7;//复位线                           //rst
sbit dcs=P2^0;					// dcs at P2.0
sbit dclk=P2^1;					// dclk 连接于 P2.1
sbit ddat=P2^2;					// ddat 连接于 P2.2
sbit dkey=P2^3;					// dkey 连接于 P2.3
sbit ACC_0=ACC^0;
sbit ACC_7=ACC^7;
void long_delay(void);				// 长延时
void short_delay(void);				// 短暂延时
void write7279(uchar,uchar);	                // 写入到HD7279
uchar read7279(uchar);			        // 从HD7279读出
void send_byte(uchar);				// 发送一个字节
uchar receive_byte(void);			// 接收一个字节

uchar data second,mintue,hour,dat,month,week,year;
         //秒,分,时,日,月,星期,年
uchar data ucBuffer;

void Display();
/***********************************************************
***功能:延时
************************************************************/
void Delay()
{
        uchar j;
        for(j=250;j>0;j--)
		continue ;
}
/**************************************************************
***功能:写入数据
***入口参数:ucSendData
**************************************************************/
void Write_Data(uchar ucSendData)
{
        uchar i;
        ACC=ucSendData;
        for(i=8;i>0;i--)
        {
                IO=ACC_0;//从第0位开始传输
                CLK=1;   //上升沿写入数据
                _nop_();
                _nop_();
                CLK=0;
                ACC=_cror_(ACC,1);//右移一位
        }
}
/****************************************************************
***功能:读出数据
***出口参数:ACC
****************************************************************/
uchar Read_Data()
{
        uchar i;
        for(i=8;i>0;i--)
        {
                ACC=_cror_(ACC,1);
                ACC_7=IO;//读取数据线上的数据
                CLK=1;   //下降沿读出数据
                _nop_();
                _nop_();
                CLK=0;
        }
        return ACC;
}
/**************************************************************
***功能:向DS1302中写入数据
***说明:先写地址,后写数据 ucAddress-地址,ucData-数据
**************************************************************/
void Write_DS1302(uchar ucAddress,uchar ucData)
{
        RST=0;
        Delay();
        CLK=0;
        Delay();
        RST=1;
        Delay();
        Write_Data(ucAddress);//写入地址
        Delay();
        Delay();
        Write_Data(ucData);//写入数据
        Delay();
        CLK=0;
        Delay();
        RST=0;
        Delay();
}
/****************************************************************
***功能:从DS1302中读出数据
***说明:先写地址,后读数据 ucAddress-地址,ucBuffer-接收数据
****************************************************************/
uchar Read_DS1302(uchar ucAddress)
{
        RST=0;
        Delay();
        CLK=0;
        Delay();
        RST=1;
        Delay();
        Write_Data(ucAddress);//写入地址
        ucBuffer=Read_Data();//读出数据
        Delay();
        CLK=0;
        Delay();
        RST=0;
        Delay();
        return ucBuffer;
}
/***************************************************************
***功能:main()
***说明:
***************************************************************/
void main()
{
        Write_DS1302(0x8e,0x00);//WP=0
        Delay();
        Delay();
        Write_DS1302(0x90,0xa5);//
        Delay();
        Delay();
        Write_DS1302(0x80,0x00);//设定second=0x00
        Delay();
        Delay();
        Write_DS1302(0x82,0x00);//设定mintue=0x00
        Delay();
        Delay();
        Write_DS1302(0x84,0x08);//hour=8
        Delay();
        Delay();
        Write_DS1302(0x86,0x01);//dat=1
        Delay();
        Delay();
        Write_DS1302(0x88,0x06);//month=6
        Delay();
        Delay();
        Write_DS1302(0x8a,0x02);//week=1
        Delay();
        Delay();
        Write_DS1302(0x8c,0x06);//year=5
        while(1)
        {
                second=Read_DS1302(0x81);//读秒
                Delay();
                Delay();
                mintue=Read_DS1302(0x83);//读分
                Delay();
                Delay();
                hour=Read_DS1302(0x85);  //读时
                Delay();
                Delay();
                dat=Read_DS1302(0x87);   //读日
                Delay();
                Delay();
                month=Read_DS1302(0x89); //读月
                Delay();
                Delay();
                week=Read_DS1302(0x8b);  //读星期
                Delay();
                Delay();
                year=Read_DS1302(0x8d);  //读年
                Display();
        }
}
/****************************************************************
***功能:将从DS1302中读出的数值送显
***说明:P0低4位接段码,高四位接位选
****************************************************************/
void Display()
{       
	uchar minh,minl, hourh,hourl;
	minl = mintue&0x0f;
	minh = mintue>>4;
	hourl = hour&0x0f;
	hourh = hour>>4;
        write7279(DECODE0,minl);
        write7279(DECODE0+1,minh);
        write7279(DECODE0+2,hourl);
        write7279(DECODE0+3,hourh);
}

void write7279(uchar cmd, uchar dta)
{
	send_byte (cmd);
	send_byte (dta);
}	

uchar read7279(uchar command)
{
	send_byte(command);
	return(receive_byte());
}

void send_byte(uchar out_byte)
{
	uchar i;
	dcs=0;
	long_delay();
	for (i=0;i<8;i++)
	{
		if (out_byte&0x80)
		{	
			ddat=1;
		}
		else
		{
			ddat=0;
		}
		dclk=1;
		short_delay();
		dclk=0;
		short_delay();
		out_byte<<=1;
	}
	ddat=0;
}

uchar receive_byte(void)
{
	uchar i, in_byte;
	ddat=1;				// set to input mode
	long_delay();
	for (i=0;i<8;i++)
	{
		dclk=1;
		short_delay();
		in_byte<<=1;
		if (ddat)
		{
			in_byte|=0x01;
		}
		dclk=0;
		short_delay();
	}
	ddat=0;
	return (in_byte);
}

void long_delay(void)
{
	uchar i;
	for (i=0;i<0x30;i++);
}

void short_delay(void)
{
	uchar i;
	for (i=0;i<8;i++);
}

⌨️ 快捷键说明

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