?? calendar.h
字號:
#ifndef _SUN_MOON
#define _SUN_MOON
/*************************************************************************/
#define uchar unsigned char
#define uint unsigned int
/********************************************************************************
* 名稱: get_moon_day(uchar month_p,uint table_addr)
* 功能: 讀取數據表中農歷的大月或小月 ,如果大月返回1, 小月返回0
* 入口參數:
* 出口參數:
*********************************************************************************/
bit get_moon_day( uchar month_p,uint calendar_address )
{
uchar temp;
switch(month_p)
{
case 1: { temp = year_code[calendar_address] & 0x08; if(temp==0) return(0); else return(1); }
case 2: { temp = year_code[calendar_address] & 0x04; if(temp==0) return(0); else return(1); }
case 3: { temp = year_code[calendar_address] & 0x02; if(temp==0) return(0); else return(1); }
case 4: { temp = year_code[calendar_address] & 0x01; if(temp==0) return(0); else return(1); }
case 5: { temp = year_code[calendar_address + 1] & 0x80; if(temp==0) return(0); else return(1); }
case 6: { temp = year_code[calendar_address + 1] & 0x40; if(temp==0) return(0); else return(1); }
case 7: { temp = year_code[calendar_address + 1] & 0x20; if(temp==0) return(0); else return(1); }
case 8: { temp = year_code[calendar_address + 1] & 0x10; if(temp==0) return(0); else return(1); }
case 9: { temp = year_code[calendar_address + 1] & 0x08; if(temp==0) return(0); else return(1); }
case 10: { temp = year_code[calendar_address + 1] & 0x04; if(temp==0) return(0); else return(1); }
case 11: { temp = year_code[calendar_address + 1] & 0x02; if(temp==0) return(0); else return(1); }
case 12: { temp = year_code[calendar_address + 1] & 0x01; if(temp==0) return(0); else return(1); }
case 13: { temp = year_code[calendar_address + 2] & 0x80; if(temp==0) return(0); else return(1); }
}
}
/**************************************************************************
* 名稱: void Calendar_Convert( uchar * clock_time )
* 功能: 輸入BCD的陽歷數據, 輸出BCD陰歷數據( 1901 - 2099 )
* 入口參數: c_flag:陽歷的世紀標志 clock_time: 時鐘地址
* 出口參數: 無
* 說明: c_flag = 0 :21世紀 c_flag = 1 :19世紀
*****************************************************************************/
void Calendar_Convert( uchar c_flag, uchar * clock_time )
{
bit flag_month, flag_year;
uchar year, month, day, month_point; //定義 年 月 天
uchar temp1, temp2, temp3;
uint calendar_address; //定義農歷地址
uint day_number;
uchar clock_moon[3]; //定義陰歷
clock_time += 3; //指向日
day = ( * clock_time >> 4 ) * 10 + ( *clock_time & 0x0f ); //BCD轉換十進制
clock_time ++; //指向月
month = ( * clock_time >> 4 ) * 10 + ( * clock_time & 0x0f ); //BCD轉換十進制
clock_time ++; //指向年
year = ( * clock_time >> 4 ) * 10 + ( * clock_time & 0x0f ); //BCD轉換十進制
//定位日歷地址
if( c_flag == 0 )
calendar_address = ( year + 99 ) * 3;
else
calendar_address = ( year - 1 ) * 3;
//春節(正月初一)所在的陽歷月份
temp1 = year_code[ calendar_address + 2 ] & 0x60; //Bit6~~Bit5:春節所在的陽歷月份
temp1 >>= 5 ;
//春節(正月初一)所在的陽歷日期
temp2 = year_code[ calendar_address + 2 ] & 0x1f; //Bit4~~Bit0:春節所在的陽歷日期
//計算春節(正月初一)離當年元旦{ 1月1日(陽歷) }的天數;春節只會在陽歷的1月 或 2月
if( temp1 == 1 )
temp3 = temp2 - 1;
else
temp3 = temp2 + 31 - 1;
//計算陽歷月離當年元旦{ 1月1日(陽歷) }的天數
if( month < 10 )
day_number = day_code1[ month - 1 ] + day - 1;
else
day_number = day_code2[ month - 10 ] + day - 1;
//如果陽歷的月大于2 且該年的2月為閏月,天數加1
//閏年指的就是陽歷有閏日或陰歷有閏月的一年;
//陽歷四年一閏,在二月加一天,這一天叫做閏日:
//農歷三年一閏,五年兩閏,十九年七閏,每逢閏年所加的一個月叫做閏月。
if( ( month > 2 ) && ( year % 4 == 0) )
day_number ++;
//判斷陽歷日 在春節(正月初一) 之前 還是 之后
if( day_number >= temp3 ) //陽歷在春節之后 或者 春節當日
{
day_number -= temp3;
month = 1;
month_point = 1; // month_point 為月份指向,陽歷日在春季前就是春季
flag_month = get_moon_day( month_point, calendar_address ); //檢查該陰歷月的大小 大月返回1 小月返回0
flag_year = 0;
if( flag_month )
temp1 = 30; //大月30天
else
temp1 = 29; //小月29天
//閏月所在的月分
temp2 = year_code[ calendar_address ] & 0xf0;
temp2 >>= 4; //提取高四位 假如是0 表示沒有閏月
while( day_number >= temp1 )
{
day_number -= temp1;
month_point ++;
if( month == temp2 )
{
flag_year = ~ flag_year;
if( flag_year == 0 )
month +=1;
}
else
month ++ ;
flag_month = get_moon_day( month_point, calendar_address );
if( flag_month )
temp1 = 30;
else
temp1 = 29;
}
day = day_number + 1;
}
else //陽歷在春節之前使用以下代碼進行運算
{
temp3 -= day_number;
if( year == 0 )
{ year = 99; c_flag = 1; }
else
year -= 1;
calendar_address -= 0x03;
month = 12;
temp2 = year_code[ calendar_address ] & 0xf0;
temp2 >>= 4; //提取高4位
if( temp2 == 0 )
month_point = 12;
else
month_point = 13;
flag_year = 0;
flag_month = get_moon_day( month_point, calendar_address );
if( flag_month )
temp1 = 30;
else
temp1 = 29;
while( temp3 > temp1 )
{
temp3 -= temp1;
month_point --;
if( flag_year == 0 )
month -=1;
if( month == temp2 )
flag_year = ~ flag_year;
flag_month = get_moon_day( month_point, calendar_address );
if( flag_month )
temp1 = 30;
else
temp1 = 29;
}
day = temp1 - day_number + 1;
}
//HEX->BCD ,運算結束后,把數據轉換為BCD數據
temp1 = year / 10;
temp1 <<= 4;
clock_moon[2] = temp1 | ( year % 10 );
temp1 = month / 10;
temp1 <<= 4;
clock_moon[1] = temp1 | ( month % 10 );
temp1 = day / 10;
temp1 <<= 4;
clock_moon[0] = temp1 | ( day % 10 );
Lcd_Lunar_Calendar( clock_moon );
}
/********************************************************************************
* 名稱: void Week_convert( uchar * clock_time )
* 功能: 輸入BCD陽歷數據;輸出BCD星期
* 入口參數:
* 出口參數:
* 說明: 算法: ( 日期 + 年份 + 所過閏年 + 月校正 ) / 7 的余數就是星期 如果是閏年又不到 3 月份上述之和 要減一天 再
********************************************************************************/
void Week_Convert( bit c, uchar * clock_time )
{
uchar year, month, day; //定義 年 月 天
uchar temp;
clock_time += 3; //指向日
day = ( * clock_time >> 4 ) * 10 + ( *clock_time & 0x0f ); //BCD轉換十進制
clock_time ++; //指向月
month = ( * clock_time >> 4 ) * 10 + ( * clock_time & 0x0f );
clock_time ++; //指向年
year = ( * clock_time >> 4 ) * 10 + ( * clock_time & 0x0f );
if( c == 0 ) //如果為21世紀,年份數加100
year += 100;
temp = year / 4; //所過閏年數只算1900年之后的
temp = year + temp;
temp = temp % 0x07; //為節省資源,先進行一次取余,避免數大于0xff,避免使用整型數據
temp = temp + day + table_week[ month - 1 ];
if( ( year % 4 == 0 ) && ( month <3 ) )
temp -=1;
Lcd_Week( temp % 7 );
}
/********************************************************************************/
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -