利用單片機DS1302芯片編制數字時鐘程序:能計算2100之前的秒,分,時,日,星期,月和年的能力,能進行閏
提問者:zncwtb2013-10-13 00:00
利用單片機DS1302芯片及接口編制數字時鐘程序:能計算2100之前的秒,分,時,日,星期,月和年的能力,能進行閏年的調整。
時間緊迫,望大哥大姐們速度快點。我非常的感謝你們!
我一旦采用了,會給你們很多分的..謝謝!謝謝!
最佳答案
/*******************************************************************************
文件:DS1302.C
環境:編譯為ICC AVR6.25A,仿真為AVR Studio4.10
硬件:ATMEGA64L芯片
日期:2008年3月7日
功能:驅動時鐘芯片DS1302
備注:
*******************************************************************************/
/*=======================硬件說明===============================================
指令 指令碼 功能
==============================================================================*/
//=======================頭文件=================================================
#include
#include
#include
#include
#include
#include
#include "main.h"
#include "Variable.h"
#include "DS1302.h"
//==============================================================================
/****************************************************************
* 名稱 : void Reset_3wire(void)
* 功能描述: reset and enable the 3-wire interfaT_CE
* 輸入參量: 無
* 輸出參量: 無
* 調用子程: 無
* 使用方法: 對3-wire 操作的話,首先要使能
-------------------------------------------------*/
void Reset_3wire(void)
{
DS1302_RST_OUT// 輸出
DS1302_CLK_OUT
DS1302_IO_OUT
DS1302_CLK_LOW
Delay_nus(10);
DS1302_RST_LOW
Delay_nus(10);
DS1302_RST_HIGH
Delay_nus(10);
}
/****************************************************************
* 名稱 : void Write_Byte_3W(unsigned char W_Byte)
* 功能描述: write one byte to the deviT_CE
* 輸入參量: 需要寫的字節
* 輸出參量: 無
* 調用子程: 無
* 使用方法: 無論讀寫,都首先要寫東西
-------------------------------------------------*/
void Write_Byte_3W(unsigned char W_Byte)
{
unsigned char i;
for(i = 0; i < 8; i++)
{
DS1302_IO_LOW
if(W_Byte & 0x01)
{
DS1302_IO_HIGH /* set port pin high to read data */
}
Delay_nus(10);
DS1302_CLK_LOW
Delay_nus(10);
DS1302_CLK_HIGH
Delay_nus(10);
W_Byte >>= 1;
}
}
//****************************************************************
/****************************************************************
* 名稱 : unsigned char Read_Byte_3W()
* 功能描述: read one byte from the deviT_CE
* 輸入參量: 需要寫的字節
* 輸出參量: 讀出的數據
* 調用子程: 無
* 使用方法: 從3 線器件讀數據
----------------------------*/
unsigned char Read_Byte_3W(void)
{
unsigned char i;
unsigned char R_Byte;
unsigned char TmpByte;
R_Byte = 0x00;
DS1302_IO_IN //0xF7;//改變io 口的方向為輸入
Delay_nus(10);
for(i = 0; i < 8; i++)
{
DS1302_CLK_HIGH
Delay_nus(10);
DS1302_CLK_LOW //下降沿數據有效
Delay_nus(10);
if(READ_DS1302_IO)
TmpByte=1;//位操作要小心
else
TmpByte=0;
TmpByte <<= 7;
R_Byte >>= 1;
R_Byte |= TmpByte;
}
return R_Byte;
}
//******************************************************************
/****************************************************************
* 名稱 : Get_Time
* 功能描述: 讀取DS1302 當前時間
* 輸入參量: ucCurtime: 保存當前時間地址。當前時間格式為: 秒 分 時 日 月 星期 年
7Byte (BCD 碼) 1B 1B 1B 1B 1B 1B 1B
* 輸出參量: 無
* 調用子程: 無
* 使用方法:
----------------------------*/
void DS1302_Get_Time(unsigned char ucCurtime[])
{
unsigned char i;
Reset_3wire();
Write_Byte_3W(0xBF); /* clock burst */
for(i=0;i<7;i++)
ucCurtime[i]=Read_Byte_3W();
//Reset_3wire();
DS1302_RST_LOW
}
/****************************************************************
* 名稱 : Set_Time
* 功能描述: initialize time & date from user entries
* 輸入參量: unsigned char yr, mn, date, dy, hr, min, sec, day;設置時間并啟動
* 輸出參量: OK --- 設置成功
* ERROR --- 時間格式錯誤,無法設置
* 調用子程: 無
* 使用方法:
----------------------------*/
unsigned char DS1302_Set_Time(unsigned char *pClock)
{
unsigned char i;
i = Check_Time(pClock);
if(i == ERROR)
return ERROR;
pClock[5] = Get_Day(pClock[6],pClock[4],pClock[3]); //取星期
//hr = hr & 0x3f; /* forT_CE clock to 24 hour mode */
Reset_3wire();
Write_Byte_3W(0x8e); /* control register */
Write_Byte_3W(0); /* disable write protect */
Reset_3wire();
//Write_Byte_3W(0x90); /* trickle charger register */
//Write_Byte_3W(0xab); /* enable, 2 dT IOdes, 8K resistor */
//Reset_3wire();
Write_Byte_3W(0xbe); /* clock burst write (eight registers) */
for(i=0;i<7;i++)
{
Write_Byte_3W(*(pClock++));
}
Write_Byte_3W(0); /* must write control register in burst mode */
//Reset_3wire();
DS1302_RST_LOW
return OK;
}//******************************************************************
//------------------------------------------------------------------------------
//函數名稱:Get_Day
//函數功能:根據年月日算出星期
//函數參數:year --- 年
// month --- 月
// date --- 日
//函數返回:day --- 星期
//------------------------------------------------------------------------------
//計算公式使用著名的蔡勒(Zeller)公式。即:
//w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
//公式中的符號含義如下,
//w:星期;
//c:世紀-1;
//y:年(兩位數);
//m:月(m大于等于3,小于等于14,即在蔡勒公式中,
// 某年的1、2月要看作上一年的 13、14月來計算,
// 比如2003年1月1日要看作2002年的13月1日來計算);
//d:日;[ ]代表取整,即只要整數部分。
//C是世紀數減一,y是年份后兩位,M是月份,d是日數。
//1月和2月要按上一年的13月和 14月來算,這時C和y均按上一 年取值。
//------------------------------------------------------------------------------
unsigned char Get_Day(unsigned char year,unsigned char month,unsigned char date)
{
unsigned char day;
unsigned int temp;
unsigned char century;
year = Hex_To_Decimal(year);
month = Hex_To_Decimal(month);
date = Hex_To_Decimal(date);
century = 20;
if(month < 3)
{
month += 12;
if(year == 0)
{
year = 99;
century --;
}
else
year --;
}
temp = (month + 1) * 26;
temp = temp / 10;
temp += year;
temp += (year >> 2);
temp += (century >> 2);
temp -= (century << 1);
temp += date;
temp --;
day = temp % 7;
if(day == 0)
day = 7;
return day;
}
//=======================函 數===============================================
//函數名稱:Check_Time
//函數功能:檢查時間格式是否正確,只能檢查2000-2099年
//函數參數:buffer --- 時間:字節從高到低,年 星期 月 日 時 分 秒
//函數返回:result --- OK: 格式正確
// ERROR:格式錯誤
//==============================================================================
unsigned char Check_Time(unsigned char *buffer)
{
unsigned char year;
unsigned char month;
unsigned char date;
unsigned char hour;
unsigned char minute;
unsigned char second;
year = Hex_To_Decimal(buffer[6]);
month = Hex_To_Decimal(buffer[4]);
date = Hex_To_Decimal(buffer[3]);
hour = Hex_To_Decimal(buffer[2]);
minute = Hex_To_Decimal(buffer[1]);
second = Hex_To_Decimal(buffer[0]);
if(second >= 60)
buffer[0] = 0x30;
if(minute >= 60)
return ERROR;
if(hour >= 24)
return ERROR;
if(month >= 13)
return ERROR;
if(month & 1)
{
if(date >= 32)
return ERROR;
}
else if(month == 2)
{
if(year % 4)
{
if(date >= 29)
return ERROR;
}
else
{
if(date >= 30)
return ERROR;
}
}
else
{
if(date >= 31)
return ERROR;
}
return OK;
}
回答者:w11273339362016-10-13 00:00
DS 5相關問題
-
說明p1.2是用了非門按制RST腳,所以SETBP1.2;令=0
DS_READ?SETBP1.2;令=0。
CLRP1.1;令SCLK=0。
CLRP1.2;令=1,啟動芯片。
提問者:s55353942013-08-03
-
1、存儲和讀取的時間的內容應為BCD碼。write_1302(0x8c,0x11);maioh=(miao&0x7f)>>4;……
2、建議上電后,ce=0;clk=0;延時一會兒再操作1302。或者多寫兩次撤銷寫保護
提問者:hikqbwgjp2013-09-06
-
僅供參考,不懂再問我,哈哈……
--------------------------------------------------------------
#include
#include
提問者:y2787121422013-04-28
-
/*************** writer:shopping.w ******************/
#include
#include
#include
提問者:guiymbo54282014-10-10
-
電路圖就自己接了,網上大把呢
#include
#define Writesecond 0x80
#define Readsecond 0x81
#define Writeminute 0x
提問者:rhjf38872013-09-14
-
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit SCK=P3^
提問者:baobao4221252013-09-04
- DS 5熱門車型
- DS 5同品牌車系
- 上市新車
- 即將上市新車