日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > 單片機 > 單片機
[導讀]發(fā)現(xiàn)網(wǎng)上一些程序在16M晶振下不能工作,自己寫了個,按照2V的時序?qū)懙?但只在5V和3.3V下測試過.以下是兩個主要的文件DS1302.h:/************ AVR DS1302程序 ************* 目標.........: ATmega128* 文件名.......:

發(fā)現(xiàn)網(wǎng)上一些程序在16M晶振下不能工作,自己寫了個,按照2V的時序?qū)懙?但只在5V和3.3V下測試過.

以下是兩個主要的文件

DS1302.h:

/************ AVR DS1302程序 ************
* 目標.........: ATmega128
* 文件名.......: DS1302.h
* 編譯器.......: IAR for AVR V5.5
****************************************/
#ifndef __DS1302_H__
#define __DS1302_H__

#include




DS1302.c:

/************ AVR DS1302程序 ************
* 版本.........: 1.0
* 作者.........: 陳利棟
* 目標.........: ATmega128
* 文件名.......: DS1302.c
* 編譯器.......: IAR for AVR V5.5
* 創(chuàng)建時間.....: 2010.10.11
* 最后修改.....: 2010.10.11
****************************************/
#include "DS1302.h"

RTC_DateTimeMy_RTC_DateTime;
DS1302_RegisterMy_DS1302_Register;
volatileboolRTC_Update=false;// RTC時間更新標志
volatileunsignedcharsecond_last=0;

unsignedcharGetDayFromDate(unsignedcharyear,unsignedcharmonth,unsignedchardate);

// 發(fā)送一個字節(jié),不操作CE
staticvoidDS1302_TxByte(unsignedcharb)
{
unsignedchari=0;

DS1302_IO_OUT;
for(i=0;i<8;i++)
{
DS1302_SCK_0;
_delay_us(1);// CLK Low Time
if(b&0x01)
{
DS1302_IO_1;
}
else
{
DS1302_IO_0;
}
_delay_us(1);// Data to CLK Setup
DS1302_SCK_1;
_delay_us(1);// CLK High Time
b>>=1;
}
}

// 接收一個字節(jié),不操作CE
staticunsignedcharDS1302_RxByte(void)
{
unsignedchari=0;
unsignedcharb=0;

DS1302_IO_IN;
DS1302_IO_1;// 上拉
for(i=0;i<8;i++)
{
DS1302_SCK_0;
_delay_us(1);// CLK to Data Delay
b>>=1;
if(DS1302_IO_VALUE)
{
b|=0x80;
}
DS1302_SCK_1;
_delay_us(1);
}

returnb;
}

staticvoidDS1302_WriteByte(unsignedcharaddress,unsignedchar_data)
{
DS1302_CE_ENABLE;
_delay_us(4);// CE to CLK Setup
DS1302_TxByte(address);
DS1302_TxByte(_data);
DS1302_SCK_0;
_delay_us(1);// CLK to CE Hold
DS1302_CE_DISABLE;
_delay_us(4);// CE Inactive Time
}

staticunsignedcharDS1302_ReadByte(unsignedcharaddress)
{
unsignedchar_data=0;

DS1302_CE_ENABLE;
_delay_us(4);// CE to CLK Setup
DS1302_TxByte(address|0x01);
_data=DS1302_RxByte();
DS1302_SCK_0;
_delay_us(1);// CLK to CE Hold
DS1302_CE_DISABLE;
_delay_us(4);// CE Inactive Time

return_data;
}

voidDS1302_SetDateTime(void)
{
unsignedchari=0;

DS1302_WriteByte(DS1302_WRITE_PROTECT_ADDR,0x00);// 關閉寫保護

// 配置將要寫入的數(shù)據(jù)
My_DS1302_Register._struct.Seconds=DEC_TO_BCD(RTC_SECONDS);
My_DS1302_Register._struct.Minutes=DEC_TO_BCD(RTC_MINUTES);
My_DS1302_Register._struct.Hour=DEC_TO_BCD(RTC_HOURS);
My_DS1302_Register._struct.Date=DEC_TO_BCD(RTC_DATE);
My_DS1302_Register._struct.Month=DEC_TO_BCD(RTC_MONTH);
My_DS1302_Register._struct.Year=DEC_TO_BCD(RTC_YEAR);
RTC_DAY=GetDayFromDate(RTC_YEAR,RTC_MONTH,RTC_DATE);
My_DS1302_Register._struct.Day=DEC_TO_BCD(RTC_DAY);

if(RTC_HOUR_MODE==_12_hours)
{
My_DS1302_Register._struct.Hour|=0x80;
My_DS1302_Register._struct.Hour|=(RTC_AM_PM==pm?0x20:0);
}

My_DS1302_Register._struct.WriteProtect=0x80;// 寫入完畢后設置寫保護

// 連續(xù)寫入
DS1302_CE_ENABLE;
_delay_us(4);// CE to CLK Setup
DS1302_TxByte(DS1302_CLOCK_BURST_ADDR);
DS1302_SCK_0;
for(i=0;i<8;i++)
{
DS1302_TxByte(My_DS1302_Register.buf[i]);
}
DS1302_SCK_0;
_delay_us(1);// CLK to CE Hold
DS1302_CE_DISABLE;
_delay_us(4);// CE Inactive Time
}

voidDS1302_GetDateTime(void)
{
unsignedchari=0;

DS1302_CE_ENABLE;
_delay_us(4);// CE to CLK Setup
DS1302_TxByte(DS1302_CLOCK_BURST_ADDR|0x01);
for(i=0;i<8;i++)
{
My_DS1302_Register.buf[i]=DS1302_RxByte();
}
DS1302_SCK_0;
_delay_us(1);// CLK to CE Hold
DS1302_CE_DISABLE;
_delay_us(4);// CE Inactive Time

RTC_SECONDS=BCD_TO_DEC(My_DS1302_Register._struct.Seconds);
RTC_MINUTES=BCD_TO_DEC(My_DS1302_Register._struct.Minutes);
RTC_DATE=BCD_TO_DEC(My_DS1302_Register._struct.Date);
RTC_MONTH=BCD_TO_DEC(My_DS1302_Register._struct.Month);
RTC_YEAR=BCD_TO_DEC(My_DS1302_Register._struct.Year);
RTC_DAY=BCD_TO_DEC(My_DS1302_Register._struct.Day);

if((My_DS1302_Register._struct.Hour)&0x80)
{
RTC_HOUR_MODE=_12_hours;
RTC_AM_PM=(((My_DS1302_Register._struct.Hour)&0x20)?pm:am);
RTC_HOURS=BCD_TO_DEC(My_DS1302_Register._struct.Hour&0x1f);
}
else
{
RTC_HOURS=BCD_TO_DEC(My_DS1302_Register._struct.Hour);
}
}

#ifdef DEBUG
voidDS1302_WriteBuildDateTime(void);
#endif/* DEBUG */

voidDS1302_Init(void)
{
// 端口初始化
DS1302_CE_DISABLE;
DS1302_CE_DDR|=(1<DS1302_SCK_0;
DS1302_SCK_DDR|=(1<
DS1302_WriteByte(DS1302_WRITE_PROTECT_ADDR,0x00);// 關閉寫保護

// 初始配置
DS1302_WriteByte(DS1302_TRICKLE_CHARGE_ADDR,0xa7);// 使能涓流充電 1 Diode, 8kΩ

DS1302_WriteByte(DS1302_WRITE_PROTECT_ADDR,0x80);// 打開寫保護

// 配置定時器1 (用于RTC時間定時掃描)
TCCR1B_WGM12=1;// CTC 模式 TOP : OCR1A
TCCR1B_CS10=1;// 1024 分頻
TCCR1B_CS12=1;// 1024 分頻
TCNT1=0;
OCR1A=F_CPU/1024/RTC_SCAN_FREQ;
TIMSK_OCIE1A=1;// 輸出比較 A 匹配中斷使能

#ifdef DEBUG
DS1302_WriteBuildDateTime();
#endif/* DEBUG */
}


#pragma vector = TIMER1_COMPA_vect
__interruptvoidRTC_Scan(void)
{
My_DS1302_Register._struct.Seconds=DS1302_ReadByte(DS1302_SECONDS_ADDR);
if(second_last!=My_DS1302_Register._struct.Seconds)
{
second_last=My_DS1302_Register._struct.Seconds;
RTC_Update=true;
}
}

// 計算2000~2099年任一天星期幾
// year : 00-99
// month : 01-12
// day : 01-31
// 返回 1 -> 7 : 星期一 -> 星期天
unsignedcharGetDayFromDate(unsignedcharyear,unsignedcharmonth,unsignedchardate)
{
if(month==1||month==2)
{
month+=12;
if(year>0)
year--;
else
year=4;
}
return(1+((date+2*month+3*(month+1)/5+year+year/4)%7));
}

#ifdef DEBUG/* Debug 模式 在 C/C++ Compiler -> Preprocessor -> Defined symbols 自己定義 */

typedefstruct
{
unsignedintYear;
unsignedintMonth;
unsignedintDate;
unsignedintHours;
unsignedintMinutes;
unsignedintSeconds;
}BuildDateTime;

__flashunsignedcharMonthStr[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};

voidGetBuildDateTime(BuildDateTime*p_BuildDateTime)
{
unsignedchartemp_str[4]={0,0,0,0},i=0;

sscanf(__DATE__,"%s %2d %4d",temp_str,&(p_BuildDateTime->Date),&(p_BuildDateTime->Year));
sscanf(__TIME__,"%2d:%2d:%2d",&(p_BuildDateTime->Hours),&(p_BuildDateTime->Minutes),&(p_BuildDateTime->Seconds));
for(i=0;i<12;i++)
{
if(temp_str[0]==MonthStr[i][0]&&temp_str[1]==MonthStr[i][1]&&temp_str[2]==MonthStr[i][2])
{
p_BuildDateTime->Month=i+1;
break;
}
}
}

// 寫入編譯時間(便于調(diào)試)
voidDS1302_WriteBuildDateTime(void)
{
BuildDateTimeMyBuildDateTime;

GetBuildDateTime(&MyBuildDateTime);

RTC_YEAR=(MyBuildDateTime.Year)%100;
RTC_MONTH=MyBuildDateTime.Month;
RTC_DATE=MyBuildDateTime.Date;
RTC_HOURS=MyBuildDateTime.Hours;
RTC_MINUTES=MyBuildDateTime.Minutes;
RTC_SECONDS=MyBuildDateTime.Seconds;

RTC_HOUR_MODE=_12_hours;
RTC_AM_PM=(RTC_HOURS>12?pm:am);
RTC_HOURS-=(RTC_HOURS>12?12:0);

DS1302_SetDateTime();
}

#endif/* DEBUG */

本站聲明: 本文章由作者或相關機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關閉