历史上的今天
今天是:2025年06月28日(星期六)
2020年06月28日 | 基于AVR单片机的DS1302子程序
2020-06-28 来源:eefocus
摘要: 目 的: 建立DS1302操作库 目标系统: 基于AVR单片机 应用软件: ICCAVR
/*********************************************************************
目 的: 建立DS1302操作库
目标系统: 基于AVR单片机
应用软件: ICCAVR
*********************************************************************/
/*01010101010101010101010101010101010101010101010101010101010101010101
----------------------------------------------------------------------
版本更新记录:
----------------------------------------------------------------------
入口参数说明:
#include "D:ICC51CmmICC.H"
#define OUT_SCK sbi(DDRD,0)
#define OUT_SIO sbi(DDRD,1)
#define OUT_RST sbi(DDRD,2)
#define IN_SIO cbi(DDRD,1)
#define SET_SCK sbi(PORTD,0)
#define SET_SIO sbi(PORTD,1)
#define SET_RST sbi(PORTD,2)
#define CLR_SCK cbi(PORTD,0)
#define CLR_SIO cbi(PORTD,1)
#define CLR_RST cbi(PORTD,2)
#define GET_SIO gbi(PIND,1)
----------------------------------------------------------------------
待定参数说明:
#define DELAY() {NOP();NOP();NOP();}
----------------------------------------------------------------------
对外变量说明:
----------------------------------------------------------------------
对外函数说明:
----------------------------------------------------------------------
10101010101010101010101010101010101010101010101010101010101010101010*/
#ifndef DS1302_H
#define DS1302_H
#include "D:ICC_HCmmICC.H"
#define DELAY() {NOP();NOP();NOP();NOP();NOP();NOP();}
//uint8 DS1302Second;
//uint8 DS1302Minute;
//uint8 DS1302Hour;
//uint8 DS1302Day;
//uint8 DS1302Month;
//uint8 DS1302Year;
//uint8 DS1302SecLow;
//uint8 DS1302MinLow;
//uint8 DS1302HourLow;
//uint8 DS1302DayLow;
//uint8 DS1302MonLow;
//uint8 DS1302YearLow;
//uint8 DS1302SecHi;
//uint8 DS1302MinHi;
//uint8 DS1302HourHi;
//uint8 DS1302DayHi;
//uint8 DS1302MonHi;
//uint8 DS1302YearHi;
uint8 operData;
#define data0 (bool)operData&0x01
//#define data7 (bool)operData&0x80
/*--------------------------------------------------------------------
函数名称:DS1302写一个字节
函数功能:
注意事项:无
提示说明:无
输 入:无
返 回:无
--------------------------------------------------------------------*/
void writeByte(void)
{
uint8 i;
OUT_SIO;
DELAY();
for(i=0;i<8;i++)
{
CLR_SCK;
if(data0)
SET_SIO;
else
CLR_SIO;
SET_SCK;
operData = operData>>1;
}
}
/*--------------------------------------------------------------------
函数名称:DS1302读一个字节
函数功能:
注意事项:无
提示说明:无
输 入:无
返 回:无
--------------------------------------------------------------------*/
void readByte(void)
{
uint8 i;
IN_SIO;
SET_SCK;
for(i=0;i<8;i++)
{
operData = operData>>1;
CLR_SCK;
DELAY();
//delay50us(1);
if(GET_SIO)
operData |= 0x80;
SET_SCK;
}
}
/*--------------------------------------------------------------------
函数名称:DS1302充电参数
函数功能:
注意事项:无
提示说明:无
输 入:无
返 回:无
--------------------------------------------------------------------*/
void setChargePrmt(void)
{
CLR_SCK;
SET_RST;
operData=0X90;
writeByte();
operData=0XA4;
writeByte();
/* the following sentence here is not indispensable,
but insert this sentence here can prevent ariseing error!
because when start operating,RST must keep low level! */
CLR_RST;
}
/*--------------------------------------------------------------------
函数名称:DS1302关写保护
函数功能:
注意事项:无
提示说明:无
输 入:无
返 回:无
--------------------------------------------------------------------*/
void closeWP(void)
{
CLR_RST;
CLR_SCK;
SET_RST;
operData=0X8E;
writeByte();
operData=0X80;
CLR_SIO;
writeByte();
/* the following sentence here is not indispensable,
but insert this sentence here can prevent ariseing error!
because when start operating,RST must keep low level! */
CLR_RST;
}
/*--------------------------------------------------------------------
函数名称:DS1302开写保护
函数功能:
注意事项:无
提示说明:无
输 入:无
返 回:无
--------------------------------------------------------------------*/
void openWP(void)
{
CLR_RST;
CLR_SCK;
SET_RST;
operData=0X8E;
writeByte();
operData=0X00;
CLR_SIO;
writeByte();
/* the following sentence here is not indispensable,
but insert this sentence here can prevent ariseing error!
because when start operating,RST must keep low level! */
CLR_RST;
}
/*--------------------------------------------------------------------
函数名称:DS1302的一个完整写操作
函数功能:
注意事项:无
提示说明:无
输 入:
返 回:无
--------------------------------------------------------------------*/
void write(uint8 addr,uint8 dat)
{
CLR_RST; //before SCL change to low level, RST must keep low level
CLR_SCK;
SET_RST;
//CLR_SCK; //also ok!!!
//CLR_RST;
//SET_RST;
//CLR_RST; //error
//SET_RST;
//CLR_SCK;
operData=0X80|(addr<<1);
writeByte();
operData=dat;
writeByte();
/* the following sentence here is not indispensable,
but insert this sentence here can prevent ariseing error!
because when start operating,RST must keep low level! */
CLR_RST;
}
/*--------------------------------------------------------------------
函数名称:DS1302的一个完整读操作
函数功能:
注意事项:无
提示说明:无
输 入:
返 回:无
--------------------------------------------------------------------*/
uint8 read(uint8 cmd)
{
//CLR_RST;
CLR_SCK;
SET_RST;
operData=(cmd<<1)|0x81;
writeByte();
readByte();
/* the following sentence here is not indispensable,
but insert this sentence here can prevent ariseing error!
because when start operating,RST must keep low level! */
CLR_RST;
return(operData);
}
/*--------------------------------------------------------------------
函数名称:DS1302读时间
函数功能:
注意事项:无
提示说明:无
输 入:
返 回:无
--------------------------------------------------------------------*/
void DS1302_getTime(uint8 *buf)
{
uint8 hourAdr=2,minuteAdr=1,secondAdr=0;
buf[0]=read(secondAdr);
buf[0]=changeHexToInt(buf[0]);
buf[1]=read(minuteAdr);
buf[1]=changeHexToInt(buf[1]);
buf[2]=read(hourAdr);
buf[2]=changeHexToInt(buf[2]);
//DS1302_speaTime();
}
/*--------------------------------------------------------------------
函数名称:DS1302读日期
函数功能:
注意事项:无
提示说明:无
输 入:无
返 回:无
--------------------------------------------------------------------*/
//void DS1302_getDate(void)
//{
//
//}
/*--------------------------------------------------------------------
函数名称:DS1302拆时间
函数功能:
注意事项:无
提示说明:低层将时间拆好,方便上层调用
输 入:无
返 回:无
--------------------------------------------------------------------*/
//void DS1302_speaTime(void)
//{
// speaData(DS1302Second,2);
// DS1302SecHi=dataElem[1];
// DS1302SecLow=dataElem[0];
//
// speaData(DS1302Minute,2);
// DS1302MinHi=dataElem[1];
// DS1302MinLow=dataElem[0];
//
// speaData(DS1302Hour,2);
// DS1302HourHi=dataElem[1];
// DS1302HourLow=dataElem[0];
//}
/*--------------------------------------------------------------------
函数名称:DS1302拆日期
函数功能:
注意事项:无
提示说明:无
输 入:无
返 回:无
--------------------------------------------------------------------*/
//void DS1302_speaDate(void)
//{
//
//}
/*--------------------------------------------------------------------
函数名称:DS1302设置时间
函数功能:
注意事项:无
提示说明:无
输 入:
返 回:无
--------------------------------------------------------------------*/
void DS1302_setTime(uint8 hour,uint8 minute,uint8 second)
{
uint8 hourAdr=2,minuteAdr=1,secondAdr=0;
hour=changeIntToHex(hour);
minute=changeIntToHex(minute);
second=changeIntToHex(second);
openWP();
write(hourAdr,hour);
write(minuteAdr,minute);
write(secondAdr,second);
closeWP();
}
/*--------------------------------------------------------------------
函数名称:DS1302设置日期
函数功能:
注意事项:无
提示说明:无
输 入:
返 回:无
--------------------------------------------------------------------*/
//void DS1302_setDate(uint8 year,uint8 month,uint8 day)
//{
//
//}
/*--------------------------------------------------------------------
函数名称:DS1302初始化
函数功能:
注意事项:无
提示说明:无
输 入:无
返 回:无
--------------------------------------------------------------------*/
void DS1302_init(void)
{
OUT_SCK;
OUT_SIO;
OUT_RST;
openWP();
setChargePrmt();
closeWP();
}
#endif
上一篇:AVR LCD1602 程序
下一篇:AVR PCF8591 程序
史海拾趣
|
随着微型化程度不断提高,元件和布线技术也取得巨大发展,例如BGA外壳封装的高集成度的微型IC,以及导体之间的绝缘间距缩小到0.5mm,这些仅是其中的两个例子。电子元件的布线设计方式,对以后制作流程中的测试能否很好进行,影响越来越大。下面介绍 ...… 查看全部问答> |
|
4-20mA一路转多路信号隔离放大低成本、小体积解决方案一路转多路4-20mA电流环变送器低成本应用方案 工业现场传感器输出的4-20mA信号,可以采用ISO系列隔离放大器来实现低成本、小体积信号一路转多路(一进二出、一进四出、一进八出等)的方案来实 ...… 查看全部问答> |
|
我按自己的要求定制的操作系统,BUILD之后不知道为什么不能够生成NK.BIN文件,有那个nk.exe文件,与生成NK.BIN文件相关的设置是什么?请各位指点一下,先谢谢各位了.(原来可以生成的,但我没有注意有哪些设置!)… 查看全部问答> |
|
平台为X86,芯片组为QM57,操作系统为vxworks6.8 定制后的bsp包可以通过EFI进行bootrom的引导,之后系统启动成功。通过串口可以进行调试,但是无法通过VGA进行调试,并且VGA也无法进行输出。为何?… 查看全部问答> |
|
详情: 我的产品坏了100多个了(总共做了400个)。拿回来修,大多是单片机坏了。单片机发热。有时也能下载程序。就是不会正常工作/。用万用表量发显短路。不知道什么原因。 出厂都没问题。到客户那里运行一段时间就短路了。这是什么原因。工 ...… 查看全部问答> |
|
ucos移植问题(ucos2.86+stm32+lib3.5):提示main.o和os_core.o出现41个重复定义的错误,但是main函数里并没有定义那些东西呢?不知道为什么,请求解决,谢谢… 查看全部问答> |
|
首先会让你输入root用户的密码。 1)出错的时候如果告诉你是哪一块硬盘的分区有问题,比如是/dev/hda3 接着用如下的命令去对付它呀: &nb ...… 查看全部问答> |




