新聞中心

基于C8051F的DS1302程序

作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
#include "C8051F340.h"

#define uint unsigned int
#define uchar unsigned char
sbit SCLK = P2^3 ; //DS1302時(shí)鐘
sbit DIO = P2^4 ; //DS1302數(shù)據(jù)
sbit CE = P2^5 ; //DS1302片選
void Ds1302_Write_Byte (uchar addr, uchar dat);
uchar Ds1302_Read_Byte ( uchar addr );
void Ds1302_Write_Time(void);
void Ds1302_Read_Time(void);
void Ds1302_Init(void)

本文引用地址:http://m.butianyuan.cn/article/201612/324202.htm

#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84
#define WRITE_DAY 0x86
#define WRITE_MONTH 0x88
#define WRITE_WEEK 0x8A
#define WRITE_YEAR 0x8C
#define WRITE_TIMER_FLAG 0xC0

#define READ_SECOND 0x81
#define READ_MINUTE 0x83
#define READ_HOUR 0x85
#define READ_DAY 0x87
#define READ_MONTH 0x89
#define READ_WEEK 0x8B
#define READ_YEAR 0x8D
#define READ_TIMER_FLAG 0xC1
#define WRITE_PROTECT 0x8E
uchar time_buf1[8] = {20,11,8,31,13,35,10,3}; //空年月日時(shí)分秒周
uchar time_buf[8]; //空年月日時(shí)分秒周

//============================================
//函數(shù)名稱:void Ds1302_Write_Byte (byte addr, byte dat)
//功能: 串行發(fā)送地址、數(shù)據(jù),先發(fā)低位,且在上升沿發(fā)送
//參數(shù)傳遞:有,地址和數(shù)據(jù)
//返回值: 無
//===========================================
void Ds1302_Write_Byte (uchar addr, uchar dat)
{
uchar i;
//DIOOUT; //數(shù)據(jù)端口定義為輸出
CE = 1;
//寫入目標(biāo)地址:addr
addr = addr & 0xFE; //最低位置零
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
DIO = 1;
}
else
{
DIO = 0;
}
SCLK = 1;
SCLK = 0;
addr = addr >> 1;
}

//寫入數(shù)據(jù):d
for (i = 0; i < 8; i ++)
{
if (dat & 0x01)
{
DIO = 1;
}
else
{
DIO = 0;
}
SCLK = 1;
SCLK = 0;
dat = dat >> 1;
}
CE = 0;; //停止DS1302總線

}


上一頁 1 2 下一頁

關(guān)鍵詞: C8051FDS130

評(píng)論


技術(shù)專區(qū)

關(guān)閉