這是一個通過ds1302時鐘芯片寫的一個電子時鐘
并且加入了紅外傳感器,用一個遙控來控制時鐘
希望對大家有用。
#include "ds1302.h"
unsigned char code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d}; //DS1302讀時間寄存器地址
unsigned char code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};//DS1302寫時間寄存器地址
unsigned char code qq[16]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x9,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
unsigned char code qdm[]={0x02,0x9f,0x25,0x0d,0x99,0x49,0x40,0x1f,0x00,0x08,0xff};
unsigned char code hh[12]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12};
unsigned char tim[7]; //存放時間數據臨時數組
unsigned char s[16],t[16];
unsigned char c = 0,u = 0;wei = 0,zx = 0,o = 0,date = 0,xw = 0;
unsigned char month = 0,day = 0,h = 0,m = 0,send = 0;
bit zt = 0,x = 0,f = 0,stop = 0;
unsigned char key = 0,ke = 0,PH1 = 0,PL1 = 0;
unsigned int js = 0,js1 = 0,year = 0,Time;
sbit IRIN = P3^2;
unsigned char IrValue[5]; // IrValue的0-3用來放原始數據,4用來放經過校驗確認無誤的鍵值
sbit SHCP=P3^3;
sbit DS=P3^4;
sbit STCP=P3^6;
sbit OE=P3^5;
sbit KEY1 = P2^0;
sbit KEY2 = P2^1;
sbit KEY3 = P2^2;
void Init(void)
{
SHCP=DS=STCP=1; //DS1302初始化
TMOD = 0x20;
TH1 = TL1 = 56;
ET1 = 1;
TR1 = 1;
OE=0;
PH1 = PL1 = 0;
IPH = 0x08;
}
void IrInit(void)
{
IT0 = 1;//下降沿觸發
EX0 = 1;//打開中斷0允許
EA = 1;
IRIN = 1;//初始化端口
PX0 = 1;
}
/*******************************************************************************
* 函 數 名 :延時函數
* 函數功能 :
* 輸 入 :X
* 輸 出 :無
*******************************************************************************/
void DelayMs(unsigned int x)
{
unsigned char i;
while(x--)
for (i = 0; i<114; i++);
}
void Delay(unsigned int i)
{
unsigned int j;
while(i--)
for(j=1;j>0;j--);
}
//毫秒級延時
void Delay_1ms(unsigned int i)
{
unsigned int j;
while(i--)
for(j=600;j>0;j--);
}
/*******************************************************************************
* 函 數 名 : ds1302_write_reg
* 函數功能 : 寫入寄存器地址,向寫入的寄存器地址寫入數據(僅在內部使用)
* 輸 入 : 寄存器地址reg_addr 數據 value
* 輸 出 : 無
*******************************************************************************/
static void ds1302_write_reg(unsigned char reg_addr,unsigned char value)
{
unsigned char i = 0;
unsigned char dat = 0;
//第一部分:時序起始部分
DS_SCLK = 0; //DS_SCLK為低時,DS_RST由低變高,
Delay(1);
DS_RST = 0;
Delay(1);
DS_RST = 1;
Delay(1);
//第二部分:寫入地址
for(i=0;i<8;i++)
{
dat = reg_addr & 0x01;
DS_IO = dat;
DS_SCLK = 1;//制造上升沿
Delay(1);
DS_SCLK = 0;//小周期結束,給下一個周期做準備
Delay(1);
reg_addr>>=1;
}
|