基于51单片机的led显示程序1c.docx
《基于51单片机的led显示程序1c.docx》由会员分享,可在线阅读,更多相关《基于51单片机的led显示程序1c.docx(29页珍藏版)》请在冰豆网上搜索。
基于51单片机的led显示程序1c
#include
charcodeled[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};//时钟选择
charcodejiao[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};//管脚选择
chardatadis[8]={0x00,0x00,0x0a,0x00,0x00,0x0a,0x00,0x00};//数码管初始定义
chardatar_dis[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
charPP[]={0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,
0xF8,0xF9,0xF9,0xFA,0xFA,0xFB,
0xFB,0xFC,0xFC,//1,2,3,4,5,6,7,8,i
0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,
0xFF,};//闹钟
charmiao=0x00;//时间初始化
charfen=0x00;
charshi=0x09;
charn_miao=0x00;//闹钟初始化
charn_fen=0x00;
charn_shi=0x0a;
charday=0x01;charmon=0x01;intyear=2011;//日期初始化
chardatam50=0x00;
sbitkey0=P1^0;//定义按键
sbitkey1=P1^1;
sbitkey2=P1^2;
sbitkey3=P1^3;
sbitle=P3^0;
sbitan=P3^1;//2号闹钟按键
voidkeyscan();//函数声明
voidkeyscan1();
voidkeyscan2();
voidscan1();
voidscan2();voidsong();voidweeks();//voidweekk();
chari=0;//定义全局变量
charp=1;
chara=0;
charm=0;
charb=0;t1=0x0c;t2=0;//2
charweek=1;chary=0;
/*
公历年对应的农历数据,每年三字节,
格式第一字节BIT7-4位表示闰月月份,值为0为无闰月,BIT3-0对应农历第1-4月的大小
第二字节BIT7-0对应农历第5-12月大小,第三字节BIT7表示农历第13个月大小
月份对应的位为1表示本农历月大(30天),为0表示小(29天)
第三字节BIT6-5表示春节的公历月份,BIT4-0表示春节的公历日期
*/
charcodeyear_code[]={
0x04,0xAe,0x53,//19010
0x0A,0x57,0x48,//19023
0x55,0x26,0xBd,//19036
0x0d,0x26,0x50,//19049
0x0d,0x95,0x44,//190512
0x46,0xAA,0xB9,//190615
0x05,0x6A,0x4d,//190718
0x09,0xAd,0x42,//190821
0x24,0xAe,0xB6,//1909
0x04,0xAe,0x4A,//1910
0x6A,0x4d,0xBe,//1911
0x0A,0x4d,0x52,//1912
0x0d,0x25,0x46,//1913
0x5d,0x52,0xBA,//1914
0x0B,0x54,0x4e,//1915
0x0d,0x6A,0x43,//1916
0x29,0x6d,0x37,//1917
0x09,0x5B,0x4B,//1918
0x74,0x9B,0xC1,//1919
0x04,0x97,0x54,//1920
0x0A,0x4B,0x48,//1921
0x5B,0x25,0xBC,//1922
0x06,0xA5,0x50,//1923
0x06,0xd4,0x45,//1924
0x4A,0xdA,0xB8,//1925
0x02,0xB6,0x4d,//1926
0x09,0x57,0x42,//1927
0x24,0x97,0xB7,//1928
0x04,0x97,0x4A,//1929
0x66,0x4B,0x3e,//1930
0x0d,0x4A,0x51,//1931
0x0e,0xA5,0x46,//1932
0x56,0xd4,0xBA,//1933
0x05,0xAd,0x4e,//1934
0x02,0xB6,0x44,//1935
0x39,0x37,0x38,//1936
0x09,0x2e,0x4B,//1937
0x7C,0x96,0xBf,//1938
0x0C,0x95,0x53,//1939
0x0d,0x4A,0x48,//1940
0x6d,0xA5,0x3B,//1941
0x0B,0x55,0x4f,//1942
0x05,0x6A,0x45,//1943
0x4A,0xAd,0xB9,//1944
0x02,0x5d,0x4d,//1945
0x09,0x2d,0x42,//1946
0x2C,0x95,0xB6,//1947
0x0A,0x95,0x4A,//1948
0x7B,0x4A,0xBd,//1949
0x06,0xCA,0x51,//1950
0x0B,0x55,0x46,//1951
0x55,0x5A,0xBB,//1952
0x04,0xdA,0x4e,//1953
0x0A,0x5B,0x43,//1954
0x35,0x2B,0xB8,//1955
0x05,0x2B,0x4C,//1956
0x8A,0x95,0x3f,//1957
0x0e,0x95,0x52,//1958
0x06,0xAA,0x48,//1959
0x7A,0xd5,0x3C,//1960
0x0A,0xB5,0x4f,//1961
0x04,0xB6,0x45,//1962
0x4A,0x57,0x39,//1963
0x0A,0x57,0x4d,//1964
0x05,0x26,0x42,//1965
0x3e,0x93,0x35,//1966
0x0d,0x95,0x49,//1967
0x75,0xAA,0xBe,//1968
0x05,0x6A,0x51,//1969
0x09,0x6d,0x46,//1970
0x54,0xAe,0xBB,//1971
0x04,0xAd,0x4f,//1972
0x0A,0x4d,0x43,//1973
0x4d,0x26,0xB7,//1974
0x0d,0x25,0x4B,//1975
0x8d,0x52,0xBf,//1976
0x0B,0x54,0x52,//1977
0x0B,0x6A,0x47,//1978
0x69,0x6d,0x3C,//1979
0x09,0x5B,0x50,//1980
0x04,0x9B,0x45,//1981
0x4A,0x4B,0xB9,//1982
0x0A,0x4B,0x4d,//1983
0xAB,0x25,0xC2,//1984
0x06,0xA5,0x54,//1985
0x06,0xd4,0x49,//1986
0x6A,0xdA,0x3d,//1987
0x0A,0xB6,0x51,//1988
0x09,0x37,0x46,//1989
0x54,0x97,0xBB,//1990
0x04,0x97,0x4f,//1991
0x06,0x4B,0x44,//1992
0x36,0xA5,0x37,//1993
0x0e,0xA5,0x4A,//1994
0x86,0xB2,0xBf,//1995
0x05,0xAC,0x53,//1996
0x0A,0xB6,0x47,//1997
0x59,0x36,0xBC,//1998
0x09,0x2e,0x50,//1999294
0x0C,0x96,0x45,//2000297
0x4d,0x4A,0xB8,//2001
0x0d,0x4A,0x4C,//2002
0x0d,0xA5,0x41,//2003
0x25,0xAA,0xB6,//2004
0x05,0x6A,0x49,//2005
0x7A,0xAd,0xBd,//2006
0x02,0x5d,0x52,//2007
0x09,0x2d,0x47,//2008
0x5C,0x95,0xBA,//2009
0x0A,0x95,0x4e,//2010
0x0B,0x4A,0x43,//2011
0x4B,0x55,0x37,//2012
0x0A,0xd5,0x4A,//2013
0x95,0x5A,0xBf,//2014
0x04,0xBA,0x53,//2015
0x0A,0x5B,0x48,//2016
0x65,0x2B,0xBC,//2017
0x05,0x2B,0x50,//2018
0x0A,0x93,0x45,//2019
0x47,0x4A,0xB9,//2020
0x06,0xAA,0x4C,//2021
0x0A,0xd5,0x41,//2022
0x24,0xdA,0xB6,//2023
0x04,0xB6,0x4A,//2024
0x69,0x57,0x3d,//2025
0x0A,0x4e,0x51,//2026
0x0d,0x26,0x46,//2027
0x5e,0x93,0x3A,//2028
0x0d,0x53,0x4d,//2029
0x05,0xAA,0x43,//2030
0x36,0xB5,0x37,//2031
0x09,0x6d,0x4B,//2032
0xB4,0xAe,0xBf,//2033
0x04,0xAd,0x53,//2034
0x0A,0x4d,0x48,//2035
0x6d,0x25,0xBC,//2036
0x0d,0x25,0x4f,//2037
0x0d,0x52,0x44,//2038
0x5d,0xAA,0x38,//2039
0x0B,0x5A,0x4C,//2040
0x05,0x6d,0x41,//2041
0x24,0xAd,0xB6,//2042
0x04,0x9B,0x4A,//2043
0x7A,0x4B,0xBe,//2044
0x0A,0x4B,0x51,//2045
0x0A,0xA5,0x46,//2046
0x5B,0x52,0xBA,//2047
0x06,0xd2,0x4e,//2048
0x0A,0xdA,0x42,//2049
0x35,0x5B,0x37,//2050
0x09,0x37,0x4B,//2051
0x84,0x97,0xC1,//2052
0x04,0x97,0x53,//2053
0x06,0x4B,0x48,//2054
0x66,0xA5,0x3C,//2055
0x0e,0xA5,0x4f,//2056
0x06,0xB2,0x44,//2057
0x4A,0xB6,0x38,//2058
0x0A,0xAe,0x4C,//2059
0x09,0x2e,0x42,//2060
0x3C,0x97,0x35,//2061
0x0C,0x96,0x49,//2062
0x7d,0x4A,0xBd,//2063
0x0d,0x4A,0x51,//2064
0x0d,0xA5,0x45,//2065
0x55,0xAA,0xBA,//2066
0x05,0x6A,0x4e,//2067
0x0A,0x6d,0x43,//2068
0x45,0x2e,0xB7,//2069
0x05,0x2d,0x4B,//2070
0x8A,0x95,0xBf,//2071
0x0A,0x95,0x53,//2072
0x0B,0x4A,0x47,//2073
0x6B,0x55,0x3B,//2074
0x0A,0xd5,0x4f,//2075
0x05,0x5A,0x45,//2076
0x4A,0x5d,0x38,//2077
0x0A,0x5B,0x4C,//2078
0x05,0x2B,0x42,//2079
0x3A,0x93,0xB6,//2080
0x06,0x93,0x49,//2081
0x77,0x29,0xBd,//2082
0x06,0xAA,0x51,//2083
0x0A,0xd5,0x46,//2084
0x54,0xdA,0xBA,//2085
0x04,0xB6,0x4e,//2086
0x0A,0x57,0x43,//2087
0x45,0x27,0x38,//2088
0x0d,0x26,0x4A,//2089
0x8e,0x93,0x3e,//2090
0x0d,0x52,0x52,//2091
0x0d,0xAA,0x47,//2092
0x66,0xB5,0x3B,//2093
0x05,0x6d,0x4f,//2094
0x04,0xAe,0x45,//2095
0x4A,0x4e,0xB9,//2096
0x0A,0x4d,0x4C,//2097
0x0d,0x15,0x41,//2098
0x2d,0x92,0xB5,//2099
};
staticunsignedcharconsttable_week[12]={0,3,3,6,1,4,6,2,5,0,3,5};//月修正数据表
/*月份数据表*/
staticunsignedcharconstday_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};
staticunsignedshortconstday_code2[3]={0x111,0x130,0x14e};
charGetMoonDay(unsignedcharmonth_p,unsignedshorttable_addr);
unsignedcharGetChinaCalendar(
unsignedintyear,
unsignedcharmonth,
unsignedcharday);//阴历转换函数
voidy_scan1();
intN[4]={0,0,0,0};//定义阴历初始值
intGetWeek(
unsignedintyear,
unsignedcharmonth,
unsignedcharday);//星期转换
intq[1]={0};
/*************************************************************************/
voiddelay1ms(intt)//延时函数
{
inti,j;
for(i=0;ifor(j=0;j<120;j++);
}
/*****************************************************************************/
voidscan()//时间显示函数
{
chark;
dis[0]=miao%10;dis[1]=miao/10;
dis[3]=fen%10;dis[4]=fen/10;
dis[6]=shi%10;dis[7]=shi/10;
for(k=0;k<8;k++)
{
P2=jiao[k];P0=led[dis[k]];
delay1ms(10);
}
}
/***************************************************************************/
voidmain()//主函数
{le=0;
TH0=0x3c;TL0=0XB0;TMOD=0x01;ET0=1;TR0=1;EA=1;
while
(1)
{GetChinaCalendar(year,mon,day);
if(key3==0)
{
delay1ms(10);
while(key3==0);
p++;}
if(p==1)
scan();//时钟屏
if(p==2)
scan1();//日期屏
if(p==3)
scan2();//闹钟屏
if(p==4)weeks();
if(p==5)y_scan1();
if(p>=6)p=1;
if(p==1)
keyscan();//时键
if(p==2)
keyscan1();//日键
if(p==3)
keyscan2();//闹键
//if(p==4)weekk();
if(shi==t1&&fen==t2)//一次闹钟判断
song();
elseif(shi==n_shi&&fen==n_fen)//二次闹钟判断
song();
elsele=0;
}
}
/*********************************************************************************/
intleap(intyear)
{if(year%4==0&&year%100!
=0||year%400==0)//判断是否是闰年
return1;
elsereturn0;
}
intdays_month()//判断大月和小月
{
if(mon==1||mon==3||mon==5||mon==7||mon==8||mon==10||mon==12)//找出大月
return31;
if(mon==4||mon==6||mon==9||mon==11)//找出小月
return30;
if(mon==2&&leap(year)==1)return29;//判断二月是28天还是29天
elsereturn28;
}
////////////////////////////////////////////////////////////////////////////////////////
voidtime_in(void)interrupt1//中断程序
{
TR0=0;TH0=0X3C;TL0=0Xb0;TR0=1;m50++;
if(m50==20)
{m50=0;miao++;
if(miao==60)
{miao=0;fen++;
if(fen==60)
{fen=0;shi++;
if(shi==24)
{shi=0;day++;//week++;if(week==7)week=1;
if(day==days_month()+1)
{day=1;mon++;
if(mon==13)
{mon=1;year++;
if(year<=1991&&year>=2099)
{year=0;
}
}
}
}
}
}
}
}
/*********************************************时钟函数***********************************************************/
voidkeyscan()//按键时间处理函数
{
EA=0;
if(key0==0)
{delay1ms(10);
while(key0==0);
i++;TR0=0;ET0=0;
if(i>=4)
{i=0;TR0=1;ET0=1;}
}
if(i!
=0)
{
if(key1==0)
{delay1ms(10);
while(key1==0);
if(i==1)
{miao++;
if(miao>=60)
miao=0;}
if(i==2)
{fen++;
if(fen>=60)
fen=0;}
if(i==3)
{shi++;
if(shi>=24)
shi=0;}
}
}
if(i!
=0)
{
if(key2==0)
{delay1ms(10);
while(key2==0);
if(i==1)
{miao--;
if(miao<=0)
miao=59;}
if(i==2)
{fen--;
if(fen<=0)
fen=59;}
if(i==3)
{shi--;
if(shi<=0)
shi=23;}
}
}
EA=1;
}
/*******************************************时钟按键程序*************************************/
voidscan1()//日期显示函数
{intj;intt,a;t=year-(year/1000)*1000;a=t-(t/100)*100;
r_dis[0]=day%10;r_dis[1]=day/10;
r_dis[2]=mon%10;r_dis[3]=mon/10;
r_dis[4]=a%10;r_dis[5]=a/10;r_dis[6]=t/100;r_dis[7]=year/1000;
for(j=0;j<8;j++)
{
P2=jiao[j];P0=led[r_dis[j]];
delay1ms(10);
}
}
/****************