基于51單片機的模路燈系統(tǒng)設(shè)計,基于,51,單片機,路燈,系統(tǒng),設(shè)計
//宏定義
#define uchar unsigned char
#define uint unsigned int
//頭函數(shù)
#include
#include
#include
#include
#include
#include
#include
#include
//熱釋電傳感器
sbit RR=P3^7;
//是否有人狀態(tài) 光強顯示
uchar R=1,L=0;
//讀取光強
uint GQ=0;
//循環(huán)
uint XH=0;
//開機時分秒
uchar OS=0,OF=0,OM=0;
//關(guān)機時分秒
uchar ClS=0,ClF=0,ClM=0;
//模式
uchar Mode=0;
//調(diào)整位光標
uchar Modes=0;
//鍵盤
uchar K=0;
//PWM脈沖占空比調(diào)節(jié)
void pwm_set(uchar a);
//PWM初始化
void initpwm();
//光強讀取并顯示
void GQ_();
//主函數(shù)
void main()
{
//PWM脈沖
initpwm();
//初始化
pwm_set(0x00);
//屏幕初始化
Init_LCD();
//時鐘初始化
Ds1302_Init();
//ADC初始化
InitADC();
//EEPROM初始化
initeeprom();
//時鐘初始化
//Ds1302_Write_Time();
//寫
/*delay_LCD(20);
write_add(0x01,OS);
delay_LCD(20);
write_add(0x02,OF);
delay_LCD(20);
write_add(0x03,OM);
delay_LCD(20);
write_add(0x04,ClS);
delay_LCD(20);
write_add(0x05,ClF);
delay_LCD(20);
write_add(0x06,ClM);*/
//讀
delay_LCD(20);
OS=read_add(0x01);
delay_LCD(20);
OF=read_add(0x02);
delay_LCD(20);
OM=read_add(0x03);
delay_LCD(20);
ClS=read_add(0x04);
delay_LCD(20);
ClF=read_add(0x05);
delay_LCD(20);
ClM=read_add(0x06);
//讀取時間
while(1)
{
//人體判斷
if(RR==1)
{
R=1;
}
else
{
R=0;
}
//光強讀取并顯示
XH++;
if(XH>=10)
{
GQ_();
XH=0;
}
//讀取時間
Ds1302_Read_Time();
//判斷是否開啟
if(time_buf1[4]==OS&&time_buf1[5]==OF&&time_buf1[6]==OM)
{
Mode=1;
}
else if(time_buf1[4]==ClS&&time_buf1[5]==ClF&&time_buf1[6]==ClM)
{
Mode=0;
}
//開機狀態(tài)下控制LED
if(Mode==1&&R==1)
{
//PWM控制LED
pwm_set(GQ);
}
else
{
//PWM控制LED
pwm_set(0x00);
}
//時間及狀態(tài)顯示
LCD_Write_Time(time_buf1,R,L,Mode);
//鍵盤函數(shù)返回鍵值
K=Key();
//時間調(diào)整
if(K==3)
{
K=0;
//PWM控制LED
pwm_set(0x00);
write_com(0x0F);
write_com(0x80+0x40+7);
Modes=0;
//不為跳出
while(Modes!=6)
{
//鍵盤函數(shù)返回鍵值
K=Key();
//光標移動
if(K==3)
{
Modes++;
//光標
switch(Modes)
{
//秒
case 0:
{
write_com(0x80+0x40+7);
break;
}
//分
case 1:
{
write_com(0x80+0x40+4);
break;
}
//時
case 2:
{
write_com(0x80+0x40+1);
break;
}
//年
case 3:
{
write_com(0x80+3);
break;
}
//月
case 4:
{
write_com(0x80+6);
break;
}
//日
case 5:
{
write_com(0x80+9);
break;
}
}
}
//時間+
else if(K==1)
{
//相應(yīng)+
switch(Modes)
{
//秒++
case 0:
{
time_buf1[6]++;
if(time_buf1[6]>=60)
time_buf1[6]=0;
break;
}
//分++
case 1:
{
time_buf1[5]++;
if(time_buf1[5]>=60)
time_buf1[5]=0;
break;
}
//時++
case 2:
{
time_buf1[4]++;
if(time_buf1[4]>=24)
time_buf1[4]=0;
break;
}
//年++
case 3:
{
time_buf1[1]++;
if(time_buf1[1]>=100)
time_buf1[1]=0;
break;
}
//月++
case 4:
{
time_buf1[2]++;
if(time_buf1[2]>=13)
time_buf1[2]=1;
break;
}
//日++
case 5:
{
time_buf1[3]++;
if(time_buf1[3]>=YDay(time_buf1[1],time_buf1[2])+1)
time_buf1[3]=1;
break;
}
}
//星期更新
time_buf1[7]=Conver_week(time_buf1[1],time_buf1[2],time_buf1[3]);
//時間及狀態(tài)顯示
LCD_Write_Time(time_buf1,R,L,Mode);
//光標
switch(Modes)
{
//秒
case 0:
{
write_com(0x80+0x40+7);
break;
}
//分
case 1:
{
write_com(0x80+0x40+4);
break;
}
//時
case 2:
{
write_com(0x80+0x40+1);
break;
}
//年
case 3:
{
write_com(0x80+3);
break;
}
//月
case 4:
{
write_com(0x80+6);
break;
}
//日
case 5:
{
write_com(0x80+9);
break;
}
}
}
//時間-
else if(K==2)
{
//相應(yīng)-
switch(Modes)
{
//秒--
case 0:
{
time_buf1[6]--;
if(time_buf1[6]>=60)
time_buf1[6]=59;
break;
}
//分--
case 1:
{
time_buf1[5]--;
if(time_buf1[5]>=60)
time_buf1[5]=59;
break;
}
//時--
case 2:
{
time_buf1[4]--;
if(time_buf1[4]>=24)
time_buf1[4]=23;
break;
}
//年--
case 3:
{
time_buf1[1]--;
if(time_buf1[1]>=100)
time_buf1[1]=99;
break;
}
//月--
case 4:
{
time_buf1[2]--;
if(time_buf1[2]==0||time_buf1[2]>=13)
time_buf1[2]=12;
break;
}
//日--
case 5:
{
time_buf1[3]--;
if(time_buf1[3]==0||time_buf1[2]>=32)
time_buf1[3]=YDay(time_buf1[1],time_buf1[2]);
break;
}
}
//星期更新
time_buf1[7]=Conver_week(time_buf1[1],time_buf1[2],time_buf1[3]);
//時間及狀態(tài)顯示
LCD_Write_Time(time_buf1,R,L,Mode);
//光標
switch(Modes)
{
//秒
case 0:
{
write_com(0x80+0x40+7);
break;
}
//分
case 1:
{
write_com(0x80+0x40+4);
break;
}
//時
case 2:
{
write_com(0x80+0x40+1);
break;
}
//年
case 3:
{
write_com(0x80+3);
break;
}
//月
case 4:
{
write_com(0x80+6);
break;
}
//日
case 5:
{
write_com(0x80+9);
break;
}
}
}
}
//初始化
Init_LCD();
//寫時間
Ds1302_Write_Time();
//顯示
LCD_Write_Time(time_buf1,R,L,Mode);
}
//時間調(diào)整OVER
//開啟時間調(diào)整
else if(K==4)
{
//開啟時間顯示
Init_LCD_ONTime();
Modes=0;
//顯示
LCD_time(OS,OF,OM);
write_com(0x0f);
write_com(0x80+0x40+11);
while(Modes!=3)
{
//鍵盤函數(shù)返回鍵值
K=Key();
if(K==4)
{
Modes++;
//秒
if(Modes==0)
{
write_com(0x80+0x40+11);
}
//分
else if(Modes==1)
{
write_com(0x80+0x40+8);
}
//時
else if(Modes==2)
{
write_com(0x80+0x40+5);
}
}
//++
else if(K==1)
{
//秒++
if(Modes==0)
{
OM++;
if(OM>=60)
OM=0;
}
//分++
else if(Modes==1)
{
OF++;
if(OF>=60)
OF=0;
}
//時++
else if(Modes==2)
{
OS++;
if(OS>=24)
OS=0;
}
//顯示
LCD_time(OS,OF,OM);
//秒
if(Modes==0)
{
write_com(0x80+0x40+11);
}
//分
else if(Modes==1)
{
write_com(0x80+0x40+8);
}
//時
else if(Modes==2)
{
write_com(0x80+0x40+5);
}
}
//--
else if(K==2)
{
//秒--
if(Modes==0)
{
OM--;
if(OM>=60)
OM=59;
}
//分--
else if(Modes==1)
{
OF--;
if(OF>=60)
OF=59;
}
//時--
else if(Modes==2)
{
OS--;
if(OS>=24)
OS=23;
}
//顯示
LCD_time(OS,OF,OM);
//秒
if(Modes==0)
{
write_com(0x80+0x40+11);
}
//分
else if(Modes==1)
{
write_com(0x80+0x40+8);
}
//時
else if(Modes==2)
{
write_com(0x80+0x40+5);
}
}
}
delay_LCD(80);
write_add(0x01,OS);
delay_LCD(80);
write_add(0x02,OF);
delay_LCD(80);
write_add(0x03,OM);
delay_LCD(80);
write_add(0x04,ClS);
delay_LCD(80);
write_add(0x05,ClF);
delay_LCD(80);
write_add(0x06,ClM);
Init_LCD();
}//開啟時間調(diào)整
//關(guān)閉時間調(diào)整
else if(K==5)
{
//關(guān)機時間
Init_LCD_OFFTime();
Modes=0;
//顯示
LCD_time(ClS,ClF,ClM);
write_com(0x0f);
write_com(0x80+0x40+11);
while(Modes!=3)
{
//鍵盤函數(shù)返回鍵值
K=Key();
if(K==5)
{
Modes++;
//秒
if(Modes==0)
{
write_com(0x80+0x40+11);
}
//分
else if(Modes==1)
{
write_com(0x80+0x40+8);
}
//時
else if(Modes==2)
{
write_com(0x80+0x40+5);
}
}
//++
else if(K==1)
{
//秒++
if(Modes==0)
{
ClM++;
if(ClM>=60)
ClM=0;
}
//分++
else if(Modes==1)
{
ClF++;
if(ClF>=60)
ClF=0;
}
//時++
else if(Modes==2)
{
ClS++;
if(ClS>=24)
ClS=0;
}
//顯示
LCD_time(ClS,ClF,ClM);
//秒
if(Modes==0)
{
write_com(0x80+0x40+11);
}
//分
else if(Modes==1)
{
write_com(0x80+0x40+8);
}
//時
else if(Modes==2)
{
write_com(0x80+0x40+5);
}
}
//--
else if(K==2)
{
//秒--
if(Modes==0)
{
ClM--;
if(ClM>=60)
ClM=59;
}
//分--
else if(Modes==1)
{
ClF--;
if(ClF>=60)
ClF=59;
}
//時--
else if(Modes==2)
{
ClS--;
if(ClS>=24)
ClS=23;
}
//顯示
LCD_time(ClS,ClF,ClM);
//秒
if(Modes==0)
{
write_com(0x80+0x40+11);
}
//分
else if(Modes==1)
{
write_com(0x80+0x40+8);
}
//時
else if(Modes==2)
{
write_com(0x80+0x40+5);
}
}
}
delay_LCD(80);
write_add(0x01,OS);
delay_LCD(80);
write_add(0x02,OF);
delay_LCD(80);
write_add(0x03,OM);
delay_LCD(80);
write_add(0x04,ClS);
delay_LCD(80);
write_add(0x05,ClF);
delay_LCD(80);
write_add(0x06,ClM);
Init_LCD();
}//關(guān)閉時間調(diào)整
}//while end
}
//光強讀取并顯示
void GQ_()
{
uchar i=0;
//讀取光強
GQ=0;
for(i=0;i<50;i++)
{
GQ+=GetADCResult(0);
}
GQ=GQ/50;
//4級強度控制
if(GQ>=130)
{
GQ=100;
L=3;
}
else if(GQ>=70&&GQ<130)
{
GQ=10;
L=2;
}
else if(GQ>50&&GQ<70)
{
GQ=1;
L=1;
}
else
{
GQ=0;
L=0;
}
}
//PWM脈沖初始化
void initpwm()
{
CCON=0;
CL=0;
CH=0;
CMOD=0X00;//設(shè)定Pwm時鐘頻率11.0592/12
CCAPM0=0X42;
PCA_PWM0=0X00;
CCAP0H=0;
CCAP0L=0xff;
CR=1;
}
//PWM占空比設(shè)置
void pwm_set(uchar a)
{
CCAP0H=a;
}
//此函數(shù)提供與屏幕顯示相關(guān)的函數(shù)及聲明
//管腳定義
sbit rs=P2^6;
sbit lcden=P2^7;
//屏幕初始化
//時間顯示
uchar code table0[]=" ";
uchar code table1[]=" : : ";
//開機
uchar code table2[]=" Boot time ";
uchar code table3[]=" : : ";
//關(guān)機
uchar code table4[]=" Off time ";
uchar code table5[]=" : : ";
//星期
uchar code Data_table[7][3]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
//自定義字符
uchar code HZ[]={
0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02, //年
0x0f,0x09,0x0f,0x09,0x0f,0x09,0x0b,0x11, //月
0x0f,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00, //日
//光
0x04,0x15,0x0e,0x1f,0x0c,0x0d,0x17,0x00,
//人
0x0e,0x0e,0x04,0x0e,0x15,0x0e,0x0a,0x0a,
};
//等待函數(shù)
void delay_LCD(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=600;y>0;y--);
}
//LCD寫命令
void write_com(uchar com)
{
rs=0;
lcden=0;
P0=com;
delay_LCD(1);
lcden=1;
delay_LCD(1);
lcden=0;
}
//LCD寫數(shù)據(jù)
void write_date(uchar date)
{
rs=1;
lcden=0;
P0=date;
delay_LCD(1);
lcden=1;
delay_LCD(1);
lcden=0;
}
//自定義字符集
void Lcd_ram()
{
uint i,j,k=0,temp=0x40;
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
{
write_com(temp+j);
write_date(HZ[k]);
k++;
}
temp=temp+8;
}
}
//處理在LCD上顯示兩位
//add屏幕的位置,date數(shù)據(jù),h行
void write_sfm(uchar add,uchar date,uchar h)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
if(h==1)
{
write_com(0x80+add);
}
else
{
write_com(0x80+0x40+add);
}
write_date(0x30+shi);
write_date(0x30+ge);
}
//星期顯示
void Data_XS(uchar Data)
{
uchar num;
write_com(0x80+13);
for(num=0;num<3;num++)
{
//
write_date(Data_table[Data][num]);
}
}
// 顯示時間 人體檢測 光強顯示 關(guān)機/開機
void LCD_Write_Time(uchar *Data,uchar R,uchar L,uchar Mode)
{
write_sfm(0,Data[0],1); //寫年
write_sfm(2,Data[1],1);
write_sfm(5,Data[2],1); //寫月
write_sfm(8,Data[3],1);//寫日
write_sfm(0,Data[4],2); //寫時
write_sfm(3,Data[5],2); //寫分
write_sfm(6,Data[6],2);//寫秒
Data_XS(Data[7]); //寫星期
//光強
write_com(0x80+0x40+10);
write_date('0'+L);
//檢測是否有人
if(R==1)
{
write_com(0x80+0x40+12);
write_date(4);
}
else
{
write_com(0x80+0x40+12);
write_date(' ');
}
//關(guān)機
if(Mode==0)
{
write_com(0x80+0x40+13);
write_date('O');
write_date('F');
write_date('F');
}
//開機
else if(Mode==1)
{
write_com(0x80+0x40+13);
write_date('O');
write_date('N');
write_date(' ');
}
}
//顯示時間
void LCD_time(uchar S,uchar F,uchar M)
{
write_sfm(4,S,2); //寫時
write_sfm(7,F,2); //寫分
write_sfm(10,M,2);//寫秒
}
//初始化
void Init_LCD()
{
uchar num;
lcden=0;
//自定義字體
Lcd_ram();
//屏幕初始化
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
//時間
write_com(0x01);
write_com(0x80);
for(num=0;num<16;num++)
{
write_date(table0[num]);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_date(table1[num]);
}
//年
write_com(0x80+4);
write_date(0x00);
//月
write_com(0x80+7);
write_date(0x01);
//日
write_com(0x80+10);
write_date(0x02);
//光強
write_com(0x80+0x40+9);
write_date(0x03);
}
//開機時間
void Init_LCD_ONTime()
{
uchar num;
lcden=0;
//屏幕初始化
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
delay_LCD(5);
//時間
write_com(0x01);
write_com(0x80);
for(num=0;num<16;num++)
{
write_date(table2[num]);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_date(table3[num]);
}
}
//關(guān)機時間
void Init_LCD_OFFTime()
{
uchar num;
lcden=0;
//屏幕初始化
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
delay_LCD(5);
//時間
write_com(0x01);
write_com(0x80);
for(num=0;num<16;num++)
{
write_date(table4[num]);
}
write_com(0x80+0x40);
for(num=0;num<16;num++)
{
write_date(table5[num]);
}
}
//DS1302相關(guān)內(nèi)容的定義與聲明
//管腳定義
sbit SCK=P2^0;
sbit SDA=P2^1;
sbit RST=P2^2;
//復(fù)位腳
#define RST_CLR RST=0//電平置低
#define RST_SET RST=1//電平置高
//雙向數(shù)據(jù)
#define IO_CLR SDA=0//電平置低
#define IO_SET SDA=1//電平置高
#define IO_R SDA //電平讀取
//時鐘信號
#define SCK_CLR SCK=0//時鐘信號
#define SCK_SET SCK=1//電平置高
#define ds1302_sec_add 0x80 //秒數(shù)據(jù)地址
#define ds1302_min_add 0x82 //分數(shù)據(jù)地址
#define ds1302_hr_add 0x84 //時數(shù)據(jù)地址
#define ds1302_date_add 0x86 //日數(shù)據(jù)地址
#define ds1302_month_add 0x88 //月數(shù)據(jù)地址
#define ds1302_day_add 0x8a //星期數(shù)據(jù)地址
#define ds1302_year_add 0x8c //年數(shù)據(jù)地址
#define ds1302_control_add 0x8e //控制數(shù)據(jù)地址
#define ds1302_charger_add 0x90
#define ds1302_clkburst_add 0xbe
extern unsigned char time_buf1[8];//空年月日時分秒周
extern unsigned char time_buf[8] ;//空年月日時分秒周
unsigned char time_buf1[8] = {20,11,2,22,00,00,00,2};//空年月日時分秒周
unsigned char time_buf[8] ; //空年月日時分秒周
/*------------------------------------------------
向DS1302寫入一字節(jié)數(shù)據(jù)
------------------------------------------------*/
void Ds1302_Write_Byte(unsigned char addr, unsigned char d)
{
unsigned char i;
RST_SET;
//寫入目標地址:addr
addr = addr & 0xFE; //最低位置零
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
IO_SET;
}
else
{
IO_CLR;
}
SCK_SET;
SCK_CLR;
addr = addr >> 1;
}
//寫入數(shù)據(jù):d
for (i = 0; i < 8; i ++)
{
if (d & 0x01)
{
IO_SET;
}
else
{
IO_CLR;
}
SCK_SET;
SCK_CLR;
d = d >> 1;
}
RST_CLR; //停止DS1302總線
}
/*------------------------------------------------
從DS1302讀出一字節(jié)數(shù)據(jù)
------------------------------------------------*/
unsigned char Ds1302_Read_Byte(unsigned char addr)
{
unsigned char i;
unsigned char temp;
RST_SET;
//寫入目標地址:addr
addr = addr | 0x01;//最低位置高
for (i = 0; i < 8; i ++)
{
if (addr & 0x01)
{
IO_SET;
}
else
{
IO_CLR;
}
SCK_SET;
SCK_CLR;
addr = addr >> 1;
}
//輸出數(shù)據(jù):temp
for (i = 0; i < 8; i ++)
{
temp = temp >> 1;
if (IO_R)
{
temp |= 0x80;
}
else
{
temp &= 0x7F;
}
SCK_SET;
SCK_CLR;
}
RST_CLR; //停止DS1302總線
return temp;
}
/*------------------------------------------------
向DS1302寫入時鐘數(shù)據(jù)
------------------------------------------------*/
void Ds1302_Write_Time(void)
{
unsigned char i,tmp;
for(i=1;i<8;i++)
{ //BCD處理
tmp=time_buf1[i]/10;
time_buf[i]=time_buf1[i]%10;
time_buf[i]=time_buf[i]+tmp*16;
}
Ds1302_Write_Byte(ds1302_control_add,0x00); //關(guān)閉寫保護
Ds1302_Write_Byte(ds1302_sec_add,0x80); //暫停
//Ds1302_Write_Byte(ds1302_charger_add,0xa9); //涓流充電
Ds1302_Write_Byte(ds1302_year_add,time_buf[1]); //年
Ds1302_Write_Byte(ds1302_month_add,time_buf[2]); //月
Ds1302_Write_Byte(ds1302_date_add,time_buf[3]); //日
Ds1302_Write_Byte(ds1302_day_add,time_buf[7]); //周
Ds1302_Write_Byte(ds1302_hr_add,time_buf[4]); //時
Ds1302_Write_Byte(ds1302_min_add,time_buf[5]); //分
Ds1302_Write_Byte(ds1302_sec_add,time_buf[6]); //秒
Ds1302_Write_Byte(ds1302_day_add,time_buf[7]); //周
Ds1302_Write_Byte(ds1302_control_add,0x80); //打開寫保護
}
/*------------------------------------------------
從DS1302讀出時鐘數(shù)據(jù)
------------------------------------------------*/
void Ds1302_Read_Time(void)
{
unsigned char i,tmp;
time_buf[1]=Ds1302_Read_Byte(ds1302_year_add); //年
time_buf[2]=Ds1302_Read_Byte(ds1302_month_add); //月
time_buf[3]=Ds1302_Read_Byte(ds1302_date_add); //日
time_buf[4]=Ds1302_Read_Byte(ds1302_hr_add); //時
time_buf[5]=Ds1302_Read_Byte(ds1302_min_add); //分
time_buf[6]=(Ds1302_Read_Byte(ds1302_sec_add))&0x7F;//秒
time_buf[7]=Ds1302_Read_Byte(ds1302_day_add); //周
for(i=1;i<8;i++)
{ //BCD處理
tmp=time_buf[i]/16;
time_buf1[i]=time_buf[i]%16;
time_buf1[i]=time_buf1[i]+tmp*10;
}
}
/*------------------------------------------------
DS1302初始化
------------------------------------------------*/
void Ds1302_Init(void)
{
RST_CLR; //RST腳置低
SCK_CLR; //SCK腳置低
Ds1302_Write_Byte(ds1302_sec_add,0x00);
}
/*
函數(shù)功能:輸入BCD陽歷數(shù)據(jù),輸出BCD星期數(shù)據(jù)(只允許1901-2099年)
調(diào)用函數(shù)示例:Conver_week(c_sun,year_sun,month_sun,day_sun)
如:計算2004年10月16日Conversion(0,0x4,0x10,0x16);
c_sun,year_sun,month_sun,day_sun均為BCD數(shù)據(jù),c_sun為世紀標志位,c_sun=0為21世
紀,c_sun=1為19世紀
調(diào)用函數(shù)后,原有數(shù)據(jù)不變,讀week得出陰歷BCD數(shù)據(jù)
*/
code uchar table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正數(shù)據(jù)表
/*
算法:日期+年份+所過閏年數(shù)+月較正數(shù)之和除7 的余數(shù)就是星期但如果是在
閏年又不到3 月份上述之和要減一天再除7
星期數(shù)為0
*/
uchar Conver_week(uchar year,uchar month,uchar day)
{//c=0 為21世紀,c=1 為19世紀 輸入輸出數(shù)據(jù)均為BCD數(shù)據(jù)
uchar p1,p2,week;
year+=0x64; //如果為21世紀,年份數(shù)加100
p1=year/0x4; //所過閏年數(shù)只算1900年之后的
p2=year+p1;
p2=p2%0x7; //為節(jié)省資源,先進行一次取余,避免數(shù)大于0xff,避免使用整型數(shù)據(jù)
p2=p2+day+table_week[month-1];
if (year%0x4==0&&month<3)p2-=1;
week=p2%0x7;
return week;
}
//EEPROM
sbit sda=P2^4;
sbit scl=P2^5;
void delayEE()
{
uchar i;