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

📄 1302.c

📁 使用台湾普诚pt6961驱动数码管和按键,显示ds1302的源码,详细的6961驱动代码
💻 C
字号:


/********************************************************************
*
* 模 块 名:时钟芯片的读写                型号:DS1302   
*  
* 创 建 人:yingjiangan                   日期:2008-03-03           
* 修 改 人:                              日期:2000-00-00            
* 功能描述:                                                         
* 其他说明:                                                         
* 版    本:V1.0
**********************************************************************/


/************头文件************/
#include <intrins.h> 
#include <sm5964.h>
#include "my_type.h"
#include "1302.h"

code uint8 set_rtc_code[7]={0x15,0x32,0x15,0x03,0x03,0x01,0x08};
/************************************秒***分***时***日***月**星期**年**/
code uint8 write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; 
code uint8 read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
code uint8 *day[7]={"Mon","Tue","Wen","Thu","Fri","Sat","Sun"};
uint8 read_rtc_code[7];


/*********************************************************************  
*
* 函 数 名: Init_1302
* 功能描述: 1302初始化
* 函数说明: 
* 输    入: 无
* 返    回: 无
* 设 计 者:ying                         日期:2008-03-03
* 修 改 者:ying                         日期:2000-00-00
***********************************************************************/
																																	  /****************************************************************************/
void Init_1302(void)      
{ 
 
	DS1302_WP_ENABLE;  //开禁止 写入数据
	Set_RTC();   //设定秒、分、时、日、月、星期、年
	DS1302_WP_DISENABLE;   //禁止写入

}


/*********************************************************************  
* 
* 函 数 名: Write_1302_Byte
* 功能描述: 写入1302一个字节数据
* 函数说明: 物理层
* 输    入: 要写入的数据
* 返    回: 无
* 设 计 者:ying                         日期:2008-03-03
* 修 改 者:ying                         日期:2000-00-00
***********************************************************************/
void Write_1302_Byte(uint8 temp) 
{
	uint8 i;
	for (i=0;i<8;i++)      	//循环8次 写入数据
	{ 
		SDA=temp&LSB;      	//每次传输低字节
		temp>>=1;    		//右移一位
		SCL=HIGH;
		_nop_();
		SCL=LOW;
	}
}

/*********************************************************************  
* 
* 函 数 名: Write_1302
* 功能描述: 写入1302地址和数据
* 函数说明: 物理层
* 输    入: address、dat
* 返    回: 无
* 设 计 者:ying                         日期:2008-03-03
* 修 改 者:ying                         日期:2000-00-00
***********************************************************************/
void Write_1302( uint8 address,uint8 dat )     
{
	DS1302_RESET;  //启动
	Write_1302_Byte(address); //发送地址
	Write_1302_Byte(dat);  //发送数据
	RST=LOW;    //恢复
}



/*********************************************************************  
* 
* 函 数 名: Read_1302
* 功能描述: 读出1302一个字节数据并转化BCD码
* 函数说明: 物理层
* 输    入: address
* 返    回: 读出的数据
* 设 计 者:ying                         日期:2008-03-03
* 修 改 者:ying                         日期:2000-00-00
***********************************************************************/
uint8 Read_1302 ( uint8 address )
{
	uint8 i,temp=0x00,temp_temp;
	DS1302_RESET;
	Write_1302_Byte(address);
	for (i=0;i<7;i++)   //循环7次 读取数据
	{
		SCL=LOW;
		if(SDA)
		{	
			temp|=0x80;   //每次传输低字节
		}
		temp>>=1;   //右移一位
		SCL=HIGH;
	} 
	RST=LOW;

	temp_temp=temp/16;  //数据处理
	temp=temp%16;
	temp=temp+temp_temp*10;

	return (temp);   //返回
}


/*********************************************************************  
* 
* 函 数 名: Read_RTC
* 功能描述: 读出时钟芯片的时间
* 函数说明: 器件层
* 输    入: 
* 返    回: 读出的时间数组
* 设 计 者:ying                         日期:2008-03-03
* 修 改 者:ying                         日期:2000-00-00
***********************************************************************/
void Read_RTC(void)  //读取 日历
{
	uint8 i,*p;
	p=read_rtc_address;  //地址传递
	for(i=0;i<7;i++)  //分7次读取 秒、分、时、日、月、星期、年
	{
		read_rtc_code[i]=Read_1302(*p);
		p++;
	}
}


/*********************************************************************  
* 
* 函 数 名: Set_RTC
* 功能描述: 设定时钟芯片的时间
* 函数说明: 器件层
* 输    入: 
* 返    回: 
* 设 计 者:ying                         日期:2008-03-03
* 修 改 者:ying                         日期:2000-00-00
***********************************************************************/
void Set_RTC(void)  //设定 日历
{
	uint8 i,*p;
	p=write_rtc_address; //传地址 
	for(i=0;i<7;i++)  //7次写入 秒、分、时、日、月、星期、年
	{
		Write_1302(*p,set_rtc_code[i]);
		p++;  
	}
}	   

⌨️ 快捷键说明

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