Ⅰ 跪求 51單片機+12864液晶+1302時鍾製成的萬年歷c程序
頂層文件 萬年歷.C
#include<reg51.h>
#include "LCD1602.h"
#include "DS1302.h"
#define uchar unsigned char
#define uint unsigned int
sbit speaker=P2^4;
bit key_flag1=0,key_flag2=0;
SYSTEMTIME adjusted;
uchar sec_add=0,min_add=0,hou_add=0,day_add=0,mon_add=0,yea_add=0;
uchar data_alarm[7]={0};
/************鍵盤控制******************************/
int key_scan() //掃描是否有鍵按下
{ int i=0;
uint temp;
P1=0xf0;
temp=P1;
if(temp!=0xf0)
i=1;
else
i=0;
return i;
}
uchar key_value() //確定按鍵的值
{
uint m=0,n=0,temp;
uchar value;
uchar v[4][3]={'2','1','0','5','4','3','8','7','6','b','a','9'} ;
P1=0xfe; temp=P1; if(temp!=0xfe)m=0;
P1=0xfd;temp=P1 ;if(temp!=0xfd)m=1;
P1=0xfb;temp=P1 ;if(temp!=0xfb)m=2;
P1=0xf7;temp=P1 ;if(temp!=0xf7)m=3;
P1=0xef;temp=P1 ;if(temp!=0xef)n=0;
P1=0xdf;temp=P1 ;if(temp!=0xdf)n=1;
P1=0xbf;temp=P1 ;if(temp!=0xbf)n=2;
value=v[m][n];
return value;
}
/***************************設置鬧鈴函數*******************************/
void naoling(void)
{
uchar i=0,l=0,j;
init1602();
while(key_flag2&&i<12)
if(key_scan()){j=key_value();write_data(j);if(i%2==0)data_alarm[l]=(j-'0')*10;else {data_alarm[l]+=(j-'0');l++;}i++;delay(600);}
write_com(0x01);
}
uchar according(void)
{ uchar k;
if(data_alarm[0]==adjusted.Year&&data_alarm[1]==adjusted.Month&&data_alarm[2]==adjusted.Day&&data_alarm[3]==adjusted.Hour&&data_alarm[4]==adjusted.Minute&&data_alarm[5]==adjusted.Second)
k=1;
else k=0;
return k;
}
void speak(void)
{uint i=50;
while(i)
{speaker=0;
delay(1);
speaker=1;
delay(1);
i--;
}
}
void alarm(void)
{uint i=10;
while(i)
{
speak();
delay(10);
i--;
}
}
/**************************修改時間操作********************************/
void reset(void)
{
sec_add=0;
min_add=0;
hou_add=0;
day_add=0;
mon_add=0;
yea_add=0 ;
}
void adjust(void)
{
if(key_scan()&&key_flag1)
switch(key_value())
{case '0':sec_add++;break;
case '1':min_add++;break;
case '2':hou_add++;break;
case '3':day_add++;break;
case '4':mon_add++;break;
case '5':yea_add++;break;
case 'b':reset();break;
default: break;
}
adjusted.Second+=sec_add;
adjusted.Minute+=min_add;
adjusted.Hour+=hou_add;
adjusted.Day+=day_add;
adjusted.Month+=mon_add;
adjusted.Year+=yea_add;
if(adjusted.Second>59) adjusted.Second=adjusted.Second%60;
if(adjusted.Minute>59) adjusted.Minute=adjusted.Minute%60;
if(adjusted.Hour>23) adjusted.Hour=adjusted.Hour%24;
if(adjusted.Day>31) adjusted.Day=adjusted.Day%31;
if(adjusted.Month>12) adjusted.Month=adjusted.Month%12;
if(adjusted.Year>100) adjusted.Year=adjusted.Year%100;
}
/**************************中斷處理函數*********************************/
void changing(void) interrupt 0 using 0 //需要修改時間和日期,或者停止修改
{
if(key_flag1)key_flag1=0;
else key_flag1=1;
}
void alarming(void) interrupt 3 using 0 //需要設置鬧鈴或者停止設置
{
if(key_flag2)key_flag2=0;
else key_flag2=1;
}
/********************************主函數***********************************/
main()
{uint i;
uchar *l;
uchar p1[]="D:",p2[]="T:";
SYSTEMTIME T;
EA=1;
EX0=1;
IT0=1;
EA=1;
EX1=1;
IT1=1;
init1602();
Initial_DS1302() ;
while(1)
{ write_com(0x80);
write_string(p1,2);
write_com(0xc0);
write_string(p2,2);
DS1302_GetTime(&T) ;
adjusted.Second=T.Second;
adjusted.Minute=T.Minute;
adjusted.Hour=T.Hour;
adjusted.Week=T.Week;
adjusted.Day=T.Day;
adjusted.Month=T.Month;
adjusted.Year=T.Year;
for(i=0;i<9;i++)
{
adjusted.DateString[i]=T.DateString[i];
adjusted.TimeString[i]=T.TimeString[i];
}
adjust();
if(key_flag2)naoling();
if(according())alarm();
DateToStr(&adjusted);
TimeToStr(&adjusted);
write_com(0x82);
write_string(adjusted.DateString,8);
write_com(0xc2);
write_string(adjusted.TimeString,8);
delay(10);
}
(二)頭文件1 顯示模塊 LCD1602.H
#ifndef LCD_CHAR_1602_2009_5_9
#define LCD_CHAR_1602_2009_5_9
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs = P2^0;
sbit lcdrw = P2^1;
sbit lcden = P2^2;
void delay(uint z) // 延時
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) // 寫入指令數據到 lcd
{
lcdrw=0;
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date) // 寫入字元顯示數據到 lcd
{
lcdrw=0;
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init1602() // 初始化設定
{
lcdrw=0;
lcden=0;
write_com(0x3C);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void write_string(uchar *pp,uint n)
{
int i;
for(i=0;i<n;i++)
write_data(pp[i]);
}
#endif
(三)頭文件2 時鍾模塊 DS1302.H
#ifndef _REAL_TIMER_DS1302_2009_5_20_
#define _REAL_TIMER_DS1302_2003_5_20_
sbit DS1302_CLK = P2^6; //實時時鍾時鍾線引腳
sbit DS1302_IO = P2^7; //實時時鍾數據線引腳
sbit DS1302_RST = P2^5; //實時時鍾復位線引腳
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;
typedef struct SYSTEM_TIME
{
unsigned char Second;
unsigned char Minute;
unsigned char Hour;
unsigned char Week;
unsigned char Day;
unsigned char Month;
unsigned char Year;
unsigned char DateString[9]; //用這兩個字元串來放置讀取的時間
unsigned char TimeString[9];
}SYSTEMTIME; //定義的時間類型
#define AM(X) X
#define PM(X) (X+12) // 轉成24小時制
#define DS1302_SECOND 0x80
#define DS1302_MINUTE 0x82
#define DS1302_HOUR 0x84
#define DS1302_WEEK 0x8A
#define DS1302_DAY 0x86
#define DS1302_MONTH 0x88
#define DS1302_YEAR 0x8C
#define DS1302_RAM(X) (0xC0+(X)*2) //用於計算 DS1302_RAM 地址的宏
/******內部指令**********/
void DS1302InputByte(unsigned char d) //實時時鍾寫入一位元組(內部函數)
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
DS1302_IO = ACC0;
DS1302_CLK = 1;
DS1302_CLK = 0;
ACC = ACC >> 1; //因為在前面已經定義了ACC0 = ACC^0;以便再次利用DS1302_IO = ACC0;
}
}
unsigned char DS1302OutputByte(void) //實時時鍾讀取一位元組(內部函數)
{
unsigned char i;
for(i=8; i>0; i--)
{
ACC = ACC >>1;
ACC7 = DS1302_IO;
DS1302_CLK = 1;
DS1302_CLK = 0;
}
return(ACC);
}
/********************************/
void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要寫的數據
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr); // 地址,命令
DS1302InputByte(ucDa); // 寫1Byte數據
DS1302_CLK = 1;
DS1302_RST = 0;
}
unsigned char Read1302(unsigned char ucAddr) //讀取DS1302某地址的數據
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr|0x01); // 地址,命令
ucData = DS1302OutputByte(); // 讀1Byte數據
DS1302_CLK = 1;
DS1302_RST = 0;
return(ucData);
}
void DS1302_SetProtect(bit flag) //是否防寫
{
if(flag)
Write1302(0x8E,0x10);
else
Write1302(0x8E,0x00);
}
void DS1302_SetTime(unsigned char Address, unsigned char Value) // 設置時間函數
{
DS1302_SetProtect(0);
Write1302(Address, ((Value/10)<<4 | (Value%10))); //將十進制數轉換為BCD碼
} //在DS1302中的與日歷、時鍾相關的寄存器存放的數據必須為BCD碼形式
void DS1302_GetTime(SYSTEMTIME *Time)
{
unsigned char ReadValue;
ReadValue = Read1302(DS1302_SECOND);
Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); //將BCD碼轉換為十進制數
ReadValue = Read1302(DS1302_MINUTE);
Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_HOUR);
Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_DAY);
Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_WEEK);
Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_MONTH);
Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
ReadValue = Read1302(DS1302_YEAR);
Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);
}
unsigned char *DataToBCD(SYSTEMTIME *Time)
{
unsigned char D[8];
D[0]=Time->Second/10<<4+Time->Second%10;
D[1]=Time->Minute/10<<4+Time->Minute%10;
D[2]=Time->Hour/10<<4+Time->Hour%10;
D[3]=Time->Day/10<<4+Time->Day%10;
D[4]=Time->Month/10<<4+Time->Month%10;
D[5]=Time->Week/10<<4+Time->Week%10;
D[6]=Time->Year/10<<4+Time->Year%10;
return D;
}
void DateToStr(SYSTEMTIME *Time)
{
//將十進制數轉換為液晶顯示的ASCII值
Time->DateString[0] = Time->Year/10 + '0';
Time->DateString[1] = Time->Year%10 + '0';
Time->DateString[2] = '-';
Time->DateString[3] = Time->Month/10 + '0';
Time->DateString[4] = Time->Month%10 + '0';
Time->DateString[5] = '-';
Time->DateString[6] = Time->Day/10 + '0';
Time->DateString[7] = Time->Day%10 + '0';
Time->DateString[8] = '\0';
}
void TimeToStr(SYSTEMTIME *Time)
{
//將十進制數轉換為液晶顯示的ASCII值
Time->TimeString[0] = Time->Hour/10 + '0';
Time->TimeString[1] = Time->Hour%10 + '0';
Time->TimeString[2] = ':';
Time->TimeString[3] = Time->Minute/10 + '0';
Time->TimeString[4] = Time->Minute%10 + '0';
Time->TimeString[5] = ':';
Time->TimeString[6] = Time->Second/10 + '0';
Time->TimeString[7] = Time->Second%10 + '0';
Time->DateString[8] = '\0';
}
void Initial_DS1302(void)
{
unsigned char Second;
Second=Read1302(DS1302_SECOND);
if(Second&0x80) //初始化時間
DS1302_SetTime(DS1302_SECOND,0);
}
void DS1302_TimeStop(bit flag) // 是否將時鍾停止
{
unsigned char Data;
Data=Read1302(DS1302_SECOND);
DS1302_SetProtect(0);
if(flag)
Write1302(DS1302_SECOND, Data|0x80);
else
Write1302(DS1302_SECOND, Data&0x7F);
}
#endif
Ⅱ 跪求個利用51單片機1602顯示屏DS1302做的萬年歷c編程序,帶有公歷,農歷,星期,時間功能,一定要有農歷
農歷計算方式,
///月份數據表
code uchar day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};
code uint day_code2[3]={0x111,0x130,0x14e};
/*
函數功能:輸入BCD陽歷數據,輸出BCD陰歷數據(只允許1901-2099年)
調用函數示例:Conversion(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數據,c_sun為世紀標志位,c_sun=0為21世
紀,c_sun=1為19世紀
調用函數後,原有數據不變,讀c_moon,year_moon,month_moon,day_moon得出陰歷BCD數據
*/
bit c_moon;
data uchar year_moon,month_moon,day_moon,week;
/*子函數,用於讀取數據表中農歷月的大月或小月,如果該月為大返回1,為小返回0*/
bit get_moon_day(uchar month_p,uint table_addr)
{
uchar temp;
switch (month_p)
{
case 1:{temp=year_code[table_addr]&0x08;
if (temp==0)return(0);else return(1);}
case 2:{temp=year_code[table_addr]&0x04;
if (temp==0)return(0);else return(1);}
case 3:{temp=year_code[table_addr]&0x02;
if (temp==0)return(0);else return(1);}
case 4:{temp=year_code[table_addr]&0x01;
if (temp==0)return(0);else return(1);}
case 5:{temp=year_code[table_addr+1]&0x80;
if (temp==0) return(0);else return(1);}
case 6:{temp=year_code[table_addr+1]&0x40;
if (temp==0)return(0);else return(1);}
case 7:{temp=year_code[table_addr+1]&0x20;
if (temp==0)return(0);else return(1);}
case 8:{temp=year_code[table_addr+1]&0x10;
if (temp==0)return(0);else return(1);}
case 9:{temp=year_code[table_addr+1]&0x08;
if (temp==0)return(0);else return(1);}
case 10:{temp=year_code[table_addr+1]&0x04;
if (temp==0)return(0);else return(1);}
case 11:{temp=year_code[table_addr+1]&0x02;
if (temp==0)return(0);else return(1);}
case 12:{temp=year_code[table_addr+1]&0x01;
if (temp==0)return(0);else return(1);}
case 13:{temp=year_code[table_addr+2]&0x80;
if (temp==0)return(0);else return(1);}
}
}
/*
函數功能:輸入BCD陽歷數據,輸出BCD陰歷數據(只允許1901-2099年)
調用函數示例:Conversion(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數據,c_sun為世紀標志位,c_sun=0為21世
紀,c_sun=1為19世紀
調用函數後,原有數據不變,讀c_moon,year_moon,month_moon,day_moon得出陰歷BCD數據
*/
void Conversion(bit c,uchar year,uchar month,uchar day)
{ //c=0 為21世紀,c=1 為19世紀 輸入輸出數據均為BCD數據
uchar temp1,temp2,temp3,month_p;
uint temp4,table_addr;
bit flag2,flag_y;
temp1=year/16; //BCD->hex 先把數據轉換為十六進制
temp2=year%16;
year=temp1*10+temp2;
temp1=month/16;
temp2=month%16;
month=temp1*10+temp2;
temp1=day/16;
temp2=day%16;
day=temp1*10+temp2;
//定位數據表地址
if(c==0)
{
table_addr=(year+0x64-1)*0x3;
}
else
{
table_addr=(year-1)*0x3;
}
//定位數據表地址完成
//取當年春節所在的公歷月份
temp1=year_code[table_addr+2]&0x60;
temp1=_cror_(temp1,5);
//取當年春節所在的公歷月份完成
//取當年春節所在的公歷日
temp2=year_code[table_addr+2]&0x1f;
//取當年春節所在的公歷日完成
// 計算當年春年離當年元旦的天數,春節只會在公歷1月或2月
if(temp1==0x1)
{
temp3=temp2-1;
}
else
{
temp3=temp2+0x1f-1;
}
// 計算當年春年離當年元旦的天數完成
//計算公歷日離當年元旦的天數,為了減少運算,用了兩個表
//day_code1[9],day_code2[3]
//如果公歷月在九月或前,天數會少於0xff,用表day_code1[9],
//在九月後,天數大於0xff,用表day_code2[3]
//如輸入公歷日為8月10日,則公歷日離元旦天數為day_code1[8-1]+10-1
//如輸入公歷日為11月10日,則公歷日離元旦天數為day_code2[11-10]+10-1
if (month<10)
{
temp4=day_code1[month-1]+day-1;
}
else
{
temp4=day_code2[month-10]+day-1;
}
if ((month>0x2)&&(year%0x4==0))
{ //如果公歷月大於2月並且該年的2月為閏月,天數加1
temp4+=1;
}
//計算公歷日離當年元旦的天數完成
//判斷公歷日在春節前還是春節後
if (temp4>=temp3)
{ //公歷日在春節後或就是春節當日使用下面代碼進行運算
temp4-=temp3;
month=0x1;
month_p=0x1; //month_p為月份指向,公歷日在春節前或就是春節當日month_p指向首月
flag2=get_moon_day(month_p,table_addr);
//檢查該農歷月為大小還是小月,大月返回1,小月返回0
flag_y=0;
if(flag2==0)temp1=0x1d; //小月29天
else temp1=0x1e; //大小30天
temp2=year_code[table_addr]&0xf0;
temp2=_cror_(temp2,4); //從數據表中取該年的閏月月份,如為0則該年無閏月
while(temp4>=temp1)
{
temp4-=temp1;
month_p+=1;
if(month==temp2)
{
flag_y=~flag_y;
if(flag_y==0)
month+=1;
}
else month+=1;
flag2=get_moon_day(month_p,table_addr);
if(flag2==0)temp1=0x1d;
else temp1=0x1e;
}
day=temp4+1;
}
else
{ //公歷日在春節前使用下面代碼進行運算
temp3-=temp4;
if (year==0x0)
{
year=0x63;c=1;
}
else year-=1;
table_addr-=0x3;
month=0xc;
temp2=year_code[table_addr]&0xf0;
temp2=_cror_(temp2,4);
if (temp2==0)
month_p=0xc;
else
month_p=0xd; //
/*month_p為月份指向,如果當年有閏月,一年有十三個月,月指向13,無閏月指向12*/
flag_y=0;
flag2=get_moon_day(month_p,table_addr);
if(flag2==0)temp1=0x1d;
else temp1=0x1e;
while(temp3>temp1)
{
temp3-=temp1;
month_p-=1;
if(flag_y==0)month-=1;
if(month==temp2)flag_y=~flag_y;
flag2=get_moon_day(month_p,table_addr);
if(flag2==0)temp1=0x1d;
else temp1=0x1e;
}
day=temp1-temp3+1;
}
c_moon=c; //HEX->BCD ,運算結束後,把數據轉換為BCD數據
temp1=year/10;
temp1=_crol_(temp1,4);
temp2=year%10;
year_moon=temp1|temp2;
temp1=month/10;
temp1=_crol_(temp1,4);
temp2=month%10;
month_moon=temp1|temp2;
temp1=day/10;
temp1=_crol_(temp1,4);
temp2=day%10;
day_moon=temp1|temp2;
}
/*************************************************************************
/*函數功能:輸入BCD陽歷數據,輸出BCD星期數據(只允許1901-2099年)
調用函數示例: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數據,c_sun為世紀標志位,c_sun=0為21世
紀,c_sun=1為19世紀
調用函數後,原有數據不變,讀week得出陰歷BCD數據
*/
code uchar table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正數據表
/*
演算法:日期+年份+所過閏年數+月較正數之和除7 的余數就是星期但如果是在
閏年又不到3 月份上述之和要減一天再除7
星期數為0
*/
void Conver_week(bit c,uchar year,uchar month,uchar day)
{//c=0 為21世紀,c=1 為19世紀 輸入輸出數據均為BCD數據
uchar temp1,temp2;
temp1=year/16; //BCD->hex 先把數據轉換為十六進制
temp2=year%16;
year=temp1*10+temp2;
temp1=month/16;
temp2=month%16;
month=temp1*10+temp2;
temp1=day/16;
temp2=day%16;
day=temp1*10+temp2;
if (c==0){year+=0x64;} //如果為21世紀,年份數加100
temp1=year/0x4; //所過閏年數只算1900年之後的
temp2=year+temp1;
temp2=temp2%0x7; //為節省資源,先進行一次取余,避免數大於0xff,避免使用整型數據
temp2=temp2+day+table_week[month-1];
if (year%0x4==0&&month<3)temp2-=1;
week=temp2%0x7;
}
Ⅲ 想用單片機設計一個萬年歷,如何實現陽歷到農歷的轉換最好是給出思路和相關程序。
<%
' 用途:
' 當前日期計算該日期的農歷天乾地支及當年屬相
' ##-------------------------------------------##
Dim objChinaDay
Dim sDay, sWeekDay, sChinaDay, sChinaYear,sChinaAni
Set objChinaDay = New ChinaDay
Call objChinaDay.Action("",sDay,sWeekDay,sChinaYear,sChinaDay,sChinaAni)
Response.Write sDay&"" '當前日期
Response.Write sWeekDay&"" '星期
Response.Write sChinaYear&"" '農歷年份
Response.Write sChinaDay&"" '農歷日期
Response.Write sChinaAni&"" '生肖
' Copyright: 本代碼非原創,原作者未知。
'*********************************************************************************
Class ChinaDay
Dim arrWeekName(7), MonthAdd(11), NongliData(99)
Dim arrTianGan(9), arrDiZhi(11), arrShuXiang(11), arrDayName(30), arrMonName(12)
Dim curTime, curYear, curMonth, curDay, curWeekday
Dim i, m, n, k, isEnd, bit, TheDate
'初始化數據
Sub Class_Initialize()
'-------------------------------------------------------------------------
'定義顯示字串
'星期名
arrWeekName(0) = "*"
arrWeekName(1) = "星期日"
arrWeekName(2) = "星期一"
arrWeekName(3) = "星期二"
arrWeekName(4) = "星期三"
arrWeekName(5) = "星期四"
arrWeekName(6) = "星期五"
arrWeekName(7) = "星期六"
'天干名稱
arrTianGan(0) = "甲"
arrTianGan(1) = "乙"
arrTianGan(2) = "丙"
arrTianGan(3) = "丁"
arrTianGan(4) = "戊"
arrTianGan(5) = "己"
arrTianGan(6) = "庚"
arrTianGan(7) = "辛"
arrTianGan(8) = "壬"
arrTianGan(9) = "癸"
'地支名稱
arrDiZhi(0) = "子"
arrDiZhi(1) = "丑"
arrDiZhi(2) = "寅"
arrDiZhi(3) = "卯"
arrDiZhi(4) = "辰"
arrDiZhi(5) = "巳"
arrDiZhi(6) = "午"
arrDiZhi(7) = "未"
arrDiZhi(8) = "申"
arrDiZhi(9) = "酉"
arrDiZhi(10) = "戌"
arrDiZhi(11) = "亥"
'屬相名稱
arrShuXiang(0) = "鼠"
arrShuXiang(1) = "牛"
arrShuXiang(2) = "虎"
arrShuXiang(3) = "兔"
arrShuXiang(4) = "龍"
arrShuXiang(5) = "蛇"
arrShuXiang(6) = "馬"
arrShuXiang(7) = "羊"
arrShuXiang(8) = "猴"
arrShuXiang(9) = "雞"
arrShuXiang(10) = "狗"
arrShuXiang(11) = "豬"
'農歷日期名
arrDayName(0) = "*"
arrDayName(1) = "初一"
arrDayName(2) = "初二"
arrDayName(3) = "初三"
arrDayName(4) = "初四"
arrDayName(5) = "初五"
arrDayName(6) = "初六"
arrDayName(7) = "初七"
arrDayName(8) = "初八"
arrDayName(9) = "初九"
arrDayName(10) = "初十"
arrDayName(11) = "十一"
arrDayName(12) = "十二"
arrDayName(13) = "十三"
arrDayName(14) = "十四"
arrDayName(15) = "十五"
arrDayName(16) = "十六"
arrDayName(17) = "十七"
arrDayName(18) = "十八"
arrDayName(19) = "十九"
arrDayName(20) = "二十"
arrDayName(21) = "廿一"
arrDayName(22) = "廿二"
arrDayName(23) = "廿三"
arrDayName(24) = "廿四"
arrDayName(25) = "廿五"
arrDayName(26) = "廿六"
arrDayName(27) = "廿七"
arrDayName(28) = "廿八"
arrDayName(29) = "廿九"
arrDayName(30) = "卅十"
'農歷月份名
arrMonName(0) = "*"
arrMonName(1) = "正"
arrMonName(2) = "二"
arrMonName(3) = "三"
arrMonName(4) = "四"
arrMonName(5) = "五"
arrMonName(6) = "六"
arrMonName(7) = "七"
arrMonName(8) = "八"
arrMonName(9) = "九"
arrMonName(10) = "十"
arrMonName(11) = "冬"
arrMonName(12) = "臘"
'-------------------------------------------------------------------------
'公差數據定義
'公歷每月前面的天數
MonthAdd(0) = 0
MonthAdd(1) = 31
MonthAdd(2) = 59
MonthAdd(3) = 90
MonthAdd(4) = 120
MonthAdd(5) = 151
MonthAdd(6) = 181
MonthAdd(7) = 212
MonthAdd(8) = 243
MonthAdd(9) = 273
MonthAdd(10) = 304
MonthAdd(11) = 334
'農歷數據
NongliData(0) = 2635
NongliData(1) = 333387
NongliData(2) = 1701
NongliData(3) = 1748
NongliData(4) = 267701
NongliData(5) = 694
NongliData(6) = 2391
NongliData(7) = 133423
NongliData(8) = 1175
NongliData(9) = 396438
NongliData(10) = 3402
NongliData(11) = 3749
NongliData(12) = 331177
NongliData(13) = 1453
NongliData(14) = 694
NongliData(15) = 201326
NongliData(16) = 2350
NongliData(17) = 465197
NongliData(18) = 3221
NongliData(19) = 3402
NongliData(20) = 400202
NongliData(21) = 2901
NongliData(22) = 1386
NongliData(23) = 267611
NongliData(24) = 605
NongliData(25) = 2349
NongliData(26) = 137515
NongliData(27) = 2709
NongliData(28) = 464533
NongliData(29) = 1738
NongliData(30) = 2901
NongliData(31) = 330421
NongliData(32) = 1242
NongliData(33) = 2651
NongliData(34) = 199255
NongliData(35) = 1323
NongliData(36) = 529706
NongliData(37) = 3733
NongliData(38) = 1706
NongliData(39) = 398762
NongliData(40) = 2741
NongliData(41) = 1206
NongliData(42) = 267438
NongliData(43) = 2647
NongliData(44) = 1318
NongliData(45) = 204070
NongliData(46) = 3477
NongliData(47) = 46
NongliData(48) = 1386
NongliData(49) = 2413
NongliData(50) = 330077
NongliData(51) = 1197
NongliData(52) = 2637
NongliData(53) = 268877
NongliData(54) = 3365
NongliData(55) = 531109
NongliData(56) = 2900
NongliData(57) = 2922
NongliData(58) = 398042
NongliData(59) = 2395
NongliData(60) = 1179
NongliData(61) = 267415
NongliData(62) = 2635
NongliData(63) = 661067
NongliData(64) = 1701
NongliData(65) = 1748
NongliData(66) = 398772
NongliData(67) = 2742
NongliData(68) = 2391
NongliData(69) = 330031
NongliData(70) = 1175
NongliData(71) = 1611
NongliData(72) = 200010
NongliData(73) = 3749
NongliData(74) = 527717
NongliData(75) = 1452
NongliData(76) = 2742
NongliData(77) = 332397
NongliData(78) = 2350
NongliData(79) = 3222
NongliData(80) = 268949
NongliData(81) = 3402
NongliData(82) = 3493
NongliData(83) = 133973
NongliData(84) = 1386
NongliData(85) = 464219
NongliData(86) = 605
NongliData(87) = 2349
NongliData(88) = 334123
NongliData(89) = 2709
NongliData(90) = 2890
NongliData(91) = 267946
NongliData(92) = 2773
NongliData(93) = 592565
NongliData(94) = 1210
NongliData(95) = 2651
NongliData(96) = 395863
NongliData(97) = 1323
NongliData(98) = 2707
NongliData(99) = 265877
End Sub
'#################################################################
'主要方法 Action
' inDay 輸入日期,如果不輸入則默認為當前日期
' sDay 中文格式日期
' sWeekDay 周幾
' sChinaYear 農歷年
' sChinaDay 農歷日
' sChinaAni 屬相
'#################################################################
Public Function Action(inDay,sDay,sWeekDay,sChinaYear,sChinaDay,sChinaAni)
'轉換要轉換的日期
If inDay="" Or Not IsDate(inDay) Then
'獲取當前系統時間
curTime = Now()
Else
curTime = CDate(inDay)
End If
If Datediff("d",curTime,Cdate("1921-2-8"))>0 Then
Exit Function
End If
'生成當前公歷年、月、日 ==> sDay
curYear = Year(curTime)
curMonth = Month(curTime)
curDay = Day(curTime)
sDay = curYear&"年"
If (curMonth < 10) Then
sDay = sDay&"0"&curMonth&"月"
Else
sDay = sDay&curMonth&"月"
End If
If (curDay < 10) Then
sDay = sDay&"0"&curDay&"日"
Else
sDay = sDay&curDay&"日"
End If
'生成當前公歷星期 ==> sWeekDay
curWeekday = Weekday(curTime)
sWeekDay = arrWeekName(curWeekday)
'計算到初始時間1921年2月8日的天數:1921-2-8(正月初一)
TheDate = (curYear - 1921) * 365 + Int((curYear - 1921) / 4) + curDay + MonthAdd(curMonth - 1) - 38
If ((curYear Mod 4) = 0 AND curMonth > 2) Then
TheDate = TheDate + 1
End If
'計算農歷天干、地支、月、日
isEnd = 0
m = 0
'------------------------------------
Do
If (NongliData(m) < 4095) Then
k = 11
Else
k = 12
End if
n = k
'------------------------------------
Do
If (n < 0) Then
Exit Do
End If
'獲取NongliData(m)的第n個二進制位的值
bit = NongliData(m)
For i = 1 To n Step 1
bit = Int(bit / 2)
Next
bit = bit Mod 2
If (TheDate <= 29 + bit) Then
isEnd = 1
Exit Do
End If
TheDate = TheDate - 29 - bit
n = n - 1
Loop
'------------------------------------
If (isEnd = 1) Then
Exit Do
End If
m = m + 1
Loop
'------------------------------------
curYear = 1921 + m
curMonth = k - n + 1
curDay = TheDate
If (k = 12) Then
If (curMonth = (Int(NongliData(m) / 65536) + 1)) Then
curMonth = 1 - curMonth
ElseIf (curMonth > (Int(NongliData(m) / 65536) + 1)) Then
curMonth = curMonth - 1
End if
End If
'生成農歷天干、地支==> sChinaYear
sChinaYear = "農歷"&arrTianGan(((curYear - 4) Mod 60) Mod 10)&arrDiZhi(((curYear - 4) Mod 60) Mod 12)&"年"
'生成屬相 == > sChinaAni
sChinaAni = arrShuXiang(((curYear - 4) Mod 60) Mod 12)
'生成農歷月、日 ==> NongliDayStr
If (curMonth < 1) Then
sChinaDay = "閏"&arrMonName(-1 * curMonth)
Else
sChinaDay = arrMonName(curMonth)
End If
sChinaDay = sChinaDay&"月"
sChinaDay = sChinaDay & arrDayName(curDay)
End Function
End Class
%>
Ⅳ C51單片機的萬年歷程序設計
我這有個數碼管顯示的程序
以前做的,。。
你可以在我這個程序上修改修改
包括鍵盤掃描,還有動態顯示
。
。
【。。。】
#include
//常量參數
#define TMODW 0x01;
#define SCONW 0x00;
#define xplay 0x04;//顯示分頻系數
//顯示位選
unsigned char data stb;
//鍵值緩存,0xFF無鍵命令
unsigned char data keynum;
//顯示字型變數
unsigned char data play[8];
//工作參數
unsigned char data l,m;
//字型碼
unsigned char code BCDPC[10]=
{0x3F,0x06,0x5B,0x4F,0x66,
0x6D,0x7D,0x07,0x7F,0x6F};
//字位碼
unsigned char code STBCODE[8]=
{0x01,0x02,0x04,0x08,
0x10,0x20,0x40,0x80 };
//400Hz xplay分頻計數
unsigned char data cttime;
//時鍾參數
unsigned char data hr,min,sec,sec100;
//調整時鍾參數(時鍾「走」)
void ct1()
{sec100++;
if (sec100==100)
{sec100=0;sec++;
if (sec==60)
{sec=0;min++;
if (min==60)
{min=0;hr++;
if (hr==24) hr=0;
}
}
}
}
//時鍾參數→LED 顯示緩存7段參數轉換函數;
void xcplay()
{play[0]=BCDPC[hr/10];
play[1]=BCDPC[hr%10];
play[2]=BCDPC[min/10];
play[3]=BCDPC[min%10];
play[4]=BCDPC[sec/10];
play[5]=BCDPC[sec%10];
play[6]=BCDPC[sec100/10];
play[7]=BCDPC[sec100%10];
}
//顯示掃描
void cplay()
{T0=1;//T0-高電平消隱
T1=0;//T1-低電平準備發脈沖前沿
TI=0;//?
P1=0;//?
SBUF=STBCODE[stb];
while (TI==0)
{
};
TI=0;
SBUF=play[stb];
while (TI==0)
{
};
T1=1;
T0=0;
stb=++stb&0x07;
}
extern void cthl0();
//定時器0中斷處理程序
void ct0(void) interrupt 1 using 1
{cthl0();
cttime--;
if (cttime==0)
{cttime=xplay;
ct1();//調用時鍾「走」函數
xcplay();//調用時鍾參數→Led顯示緩存轉換函數
};
cplay();
}
void w20ms()
{for (l=0;l<41;l++)
{for (m=0;m<81;m++)
{
}
}
}
void tkey()
{P1=0xF0;
keynum=0xFF;
if (P1!=0xF0)
{w20ms();
P1=0xF0;
if (P1!=0xF0)
{P1=0xFE;
switch (P1)
{case 0xEE:keynum=0;break;
case 0xDE:keynum=1;break;
case 0xBE:keynum=2;break;
case 0x7E:keynum=3;break;
}
P1=0xFD;
switch (P1)
{case 0xED:keynum=4;break;
case 0xDD:keynum=5;break;
case 0xBD:keynum=6;break;
case 0x7B:keynum=7;break;
}
P1=0xFB;
switch (P1)
{
case 0xEB:keynum=8;break;
}
};
};
P1=0x00;
}
void command()
{switch (keynum)
{
case 0:{hr=hr+1;
if (hr==24)
hr=0;
}
break;
case 1:{min=min+1;
if (min==60)
min=0;
}
break;
case 2:{sec=sec+1;
if (sec==60)
sec=0;
}
break;
case 3:{sec100=0;
}
break;
case 4:{
while(!(P1=0xED))
{
hr=0;
min=0;
sec=0;
}
}
break;
case 5:{hr=hr-1;
if (hr==00)
hr=24;
}
break;
case 6:{min=min-1;
if (min==00)
min=59;
}
break;
case 7:{sec=sec-1;
if (sec==00)
sec=0;
}
break;
case 0xFF:break;
}
keynum=0xFF;
}
main ()
{ hr=8;
min=5;
sec=8;
sec100=0;
TMOD=TMODW;
SCON=SCONW;
ET0=1;
TR0=1;
EA=1;
cttime=xplay;
while (1)
{w20ms();
tkey();
command();
};
}