LED时钟|语音同步LED时钟农历计算代码由深圳市立显光电有限公司技术部提供,欢迎指正,谢谢!
void Lunar_Calculation(void){
unsigned char temp1,temp2,temp3,month_p;
unsigned int temp4,table_addr;
unsigned char flag2,flag_y;
unsigned char year=time_buf[1];//阳历年
unsigned char month=time_buf[2];//阳历月
unsigned char day=time_buf[3];//阳历日
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;
//固定为21世纪
table_addr=(year+0x64-1)*0x3;
//取当年春节所在的公历月份
temp1=year_code[table_addr+2]&0x60;
temp1=temp1>>5;
//取当年春节所在的公历日
temp2=year_code[table_addr+2]&0x1f;
//计算当年春节离元旦的天数
if(temp1==0x01){
temp3=temp2-1;
}else{
temp3=temp2+0x1f-1;
}
//计算公历日离元旦的天数
if (month<10){
temp4=day_code1[month-1]+day-1;
}else{
temp4=day_code2[month-10]+day-1;
}
if ((month>0x02)&&(year%0x04==0)){ //公历日大于2月并且当年的2月为闰月
temp4+=1;
}
//判断公历日在春节前还是春节后
if(temp4>=temp3){//公历日在春节后或是当日
temp4-=temp3;
month=0x01;
month_p=0x01;//month_p为月份指向,公历日在春节前或是春节当日month_p指向首月
flag2=get_moon_day(month_p,table_addr);//检查农历月为大月还是小月
flag_y=0;
if(flag2==0){
temp1=0x1d;//小月29天
}else{
temp1=0x1e;//大小30天
}
temp2=year_code[table_addr]&0xf0;
temp2=temp2>>4;//从数据表中取该年的闰月份
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==0x00){
year=0x63;
}else{
year-=1;
}
table_addr-=0x03;
month=0x0c;
temp2=year_code[table_addr]&0xf0;
temp2=temp2>>4;
if(temp2==0){
month_p=0x0c;
}else{
month_p=0x0d;
}
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;
}
//将数据加入到NUM中
Num[0]=month/10;
Num[1]=month%10;
Num[2]=day/10;
Num[3]=day%10;
}