當前位置:吉日网官网 - 紀念幣收藏 - 用c++寫壹個簡單的萬年歷

用c++寫壹個簡單的萬年歷

我給妳兩個程序,是我收集的。我已經完成了這個eda設計。

# include & ltIOM 16v . h & gt;

# include & ltmacros.h & gt

# define ext _ ram 1(*(volatile unsigned char *)0xf0a 0)

# define ext _ ram 2(*(volatile unsigned char *)0xf0c 0)

#define write_lcm_ins (*(易失性無符號字符*)0x6000)

# define write _ LCM _ data(*(volatile unsigned char *)0x 6001)

#define timer_sec (*(易失性無符號字符*)0x2000)

#define timer_min (*(易失性無符號字符*)0x2002)

#定義timer _ hour(*(volatile unsigned char *)0x 2004)

#define timer_week (*(易失性無符號字符*)0x2006)

#定義timer _ day(*(volatile unsigned char *)0x 2007)

#define timer_moth (*(易失性無符號字符*)0x2008)

#定義timer _ year(*(volatile unsigned char *)0x 2009)

# define timer _ ra(*(volatile unsigned char *)0x 2010)

# define timer _ Rb(*(volatile unsigned char *)0x 2011)

# define timer _ RC(*(volatile unsigned char *)0x 2012)

# define timer _ rd(*(volatile unsigned char *)0x 2013)

///////////////////////////////////////////////////////////

無符號char i,k,sn0,j,a,s,data

無符號字符sec_buf,min_buf,hour_buf,sec0,sec1,min0,min1,hour0,hour 1;

無符號字符年、月、日、周、小時、分鐘、秒;

無符號char year_buf,moth_buf,day_buf,week_buf,year0,year1,moth0,moth1,day0,day 1;

///////////////////////////////////////////////////////////

void delay1ms(無符號整數延遲時間)

{

while(delay _ time & gt;0)

{

for(I = 125;我& gt0;我-)

{;}

delay _ time-;

}

}

///////////

void delay1us(無符號字符delay_time1)

{

while(delay_time1!=0)

{

delay _ time 1-;

}

}

//////////////////

void顯示(unsigned char lcm_adr,unsigned char tab0[sn0])

{

write _ lcm _ ins = lcm _ adr

delay 1 ms(2);

無符號字符sn 1 = 16;

sn0 = 0;

while(sn 1 & gt;0)

{

write _ LCM _ data = tab 0[sn0];

sn0++;

delay 1 ms(2);

sn 1-;

write _ LCM _ data = tab 0[sn0];

sn0++;

delay 1 ms(2);

sn 1-;

}

}

//////////////////////////////

unsigned char tab2[]={'0 ',' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ',' 9 ' };

無符號char tab3[]={ "Day壹二三四五六" };

//////////////////

無效顯示(數據)

{

write _ LCM _ data = tab 2[data];

delay 1 ms(2);

}

/////////

void disp_week(無符號字符周)

{

wk = wk * 2;

write _ LCM _ data = tab 3[wk];

delay 1 ms(2);

wk++;

write _ LCM _ data = tab 3[wk];

delay 1 ms(2);

}

//////////////////////////////////////////

無效時間設置(年、月、日、周、小時、分鐘、秒)

{

timer _ year =年;

timer _ moth = moth

timer _ day =日;

timer _ week =周;

timer _ hour =小時;

timer _ min = min

timer _ sec =秒;

}

//////////////////////////////////////

void display_time(無符號字符time_adr)

{

sec _ buf = timer _ sec

min _ buf = timer _ min

hour _ buf = timer _ hour

//////////////

sec 1 = sec _ buf/10;

sec 0 = sec _ buf-sec 1 * 10;

///

min 1 = min _ buf/10;

min 0 = min _ buf-min 1 * 10;

///

小時1 =小時_緩沖/10;

hour 0 = hour _ buf-hour 1 * 10;

/////////////

write _ lcm _ ins = time _ adr

delay 1 ms(2);

disp(hour 1);

disp(hour 0);

write _ LCM _ data = ':';

delay 1 ms(2);

disp(min 1);

disp(min 0);

write _ LCM _ data = ':';

delay 1 ms(2);

disp(sec 1);

disp(sec 0);

}

/////////////////////////////////////

void display_date(無符號字符日期_adr)

{

year _ buf = timer _ year

moth _ buf = timer _ moth

day _ buf = timer _ day

week _ buf = timer _ week

/////////////////////////////

year 1 = year _ buf/10;

year 0 = year _ buf-year 1 * 10;

///

moth 1 = moth _ buf/10;

moth 0 = moth _ buf-moth 1 * 10;

///

day 1 = day _ buf/10;

day 0 = day _ buf-day 1 * 10;

///

week = week _ buf

///

////////////////////////////

write _ lcm _ ins = date _ adr

delay 1 ms(2);

write _ lcm _ ins = date _ adr

delay 1 ms(2);

disp(2);

disp(0);

disp(year 1);

disp(year 0);

write _ LCM _ data = '-';

delay 1 ms(2);

disp(方法1);

disp(方法0);

write _ LCM _ data = '-';

delay 1 ms(2);

disp(day 1);

disp(第0天);

write _ LCM _ data = '-';

delay 1 ms(2);

write _ LCM _ data = '(';

delay 1 ms(2);

disp_week(周);

write _ LCM _ data = ')';

delay 1 ms(2);

}

////////////////////////////////

int main (void)

{

SP = 0x 0 fff;

MCUCR = 0x80

EICRA = 0x00//擴展擴展整數

EICRB = 0x00//擴展擴展整數

EIMSK = 0x00

PORTA = 0xff

PORTB = 0XFF

PORTC = 0xff

PORTD = 0xff

PORTE = 0xff

PORTF = 0xff

PORTG = 0xff

DDRA = 0x ff;

DDRB = 0x ff;

DDRC = 0x ff;

DDRD = 0x ff;

DDRE = 0x ff;

DDRF = 0x ff;

DDRG = 0x ff;

PORTB = 0X7F

delay 1 ms(500);

PORTB = 0XFF

PORTG = 0XFF

MCUCR | = 0X80//使能ex_ram。

XM CRA = 0b 00001110;

XM CRB = 0b 1000000;

k = 2;

而(k!=0)

{

PORTF = 0b 00011000;

delay 1 ms(200);

PORTF = 0b 00100100;

delay 1 ms(200);

PORTF = 0b 01000010;

delay 1 ms(200);

PORTF = 0b 1000001;

delay 1 ms(200);

PORTF = 0b 01000010;

delay 1 ms(200);

PORTF = 0b 00100100;

delay 1 ms(200);

k-;

}

ext _ ram 2 = 0b 10101010;

delay 1 ms(500);

PORTF = ext _ ram2

delay 1 ms(500);

PORTD = 0X00

////////////////////////////////

write _ LCM _ ins = 0x 01;

delay 1 ms(5);

write _ lcm _ ins = 0x02

delay 1 ms(5);

write _ lcm _ ins = 0x06

delay 1 ms(5);

write _ lcm _ ins = 0x0C

delay 1 ms(5);

write _ LCM _ ins = 0x 1C;

delay 1 ms(5);

write _ lcm _ ins = 0x30

delay 1 ms(5);

write _ lcm _ ins = 0x80

delay 1 ms(5);

write _ lcm _ ins = 0x02

delay 1 ms(5);

write _ lcm _ data = ' a

delay 1 ms(5);

write _ lcm _ data = ' b

delay 1 ms(5);

write _ lcm _ data = ' c

delay 1 ms(5);

write _ lcm _ data = ' d

delay 1 ms(5);

write _ lcm _ data = ' e

delay 1 ms(5);

write _ lcm _ data = ' f

delay 1 ms(5);

write _ lcm _ data = ' g

delay 1 ms(5);

write _ lcm _ data = ' h

delay 1 ms(5);

write _ lcm _ data = ' i

delay 1 ms(5);

write _ lcm _ data = ' j

delay 1 ms(5);

write _ lcm _ data = ' k

delay 1 ms(5);

顯示器(0x80,“電子日歷程序”);

顯示(0x90,“-”);

顯示(0x88,“-”);

顯示(0x98," ");

//////////////////////////

timer _ ra = 0b 10100000;

timer _ Rb = 0b 00000110;

timer _ rc = 0b00000000

// time_set(06,01,20,5,17,40,sec);

s = 4;

while(1)

{

顯示日期(0x 88);

display _ time(0x9a);

while(s==0)

{

s = 4;

PORTF = ~ PORTF

}

PORTD | = 0b 00100000;

delay 1 ms(2);

波特德& amp= 0b 11011111;

s-;

}

}

再多壹個

#i包括& ltiom8v.h & gt

#i包括& ltmacros.h & gt

#定義數據端口上拉端口

#定義數據端口方向DDRD

#定義PIND readlcdp

#定義CtrlPortPullup端口c

#定義CtrlPortDirection DDRC

#定義RS_Bit PC0

#定義RW_Bit PC1

#定義E_Bit PC2

#i包括“LCD1620.h”

#定義ClkTcnt2 0x80

/* * * * * * * * * * * * * * * *變量聲明* * * * * * * * * * * * * *

無符號int Year = 2008//年度變量

無符號字符月份= 6;//每月變量

無符號char Day = 1;

無符號字符小時= 12;//小時

無符號字符最小值= 0;//分鐘

無符號字符秒= 0;//秒

無符號字符YearBit1,YearBit2,YearBit3,YearBit4

無符號char MonthBit1,MonthBit2

無符號字符DayBit1,DayBit2

無符號字符HourBit1,HourBit2

無符號字符MinBit1,MinBit2

無符號字符SecBit1,SecBit2

無符號字符周[][4] =

{ "周壹"、"周二"、"周三"、"周四"、" Fri "、"周六"、"周日" };

無符號字符ChangeSymbol[][5] = {"-Yar "、"-Mth "、"-Day "、"-Hor "、"-Min " };

無符號char LeapYearWeekTable[] = {3,6,0,3,5,1,3,6,2,4,0,2 };//閏年周偏差表

unsigned char UnLeapWeekTable[]= { 4,0,0,3,5,1,3,6,2,4,0,2 };

無符號字符today week = 0;

無符號字符LeapYearBit = 0;當//為0時,年份為平均值。

無符號char LunarData[] = {0,0,0,0 };

無符號char LunarYear1,LunarYear2,LunarYear3,LunarYear4,LunarMonth1,LunARMonth2,LunarDay1,LunarDay2

無符號字符LunarConvertBit//0,農歷轉換成功。

無符號字符CalendarStopBit//1,時鐘暫停

無符號字符OldKeyNum = 0;

無符號字符NewKeyNum = 0;

無符號字符模式select = 0;

無符號字符change mode bit = 0;//ModeSelect!= 0,就是1,標誌現在處於變時狀態。

無符號字符SecOddBit = 0;//秒是偶數,這個是0,否則是1。

靜態無符號字符常量LunarDayCode1[9]=

{0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0x F3 };

靜態無符號短整型常量lunar daycode 2[3]= { 0x 111,0x130,0x 14e };

/**************************

* * * * * * * *子功能* * * * * * * * * *

*************************/

Void PortInit(void) //端口初始化

{

DDRC & amp;= ~((1 & lt;& ltPC3)|(1 & lt;& ltPC4)|(1 & lt;& ltPC5));

PORTC | =(1 & lt;& ltPC3)|(1 & lt;& ltPC4)|(1 & lt;& ltPC5);

}

Void定時器2 init(void)//定時器2初始化

{

TCCR2 = 0x00//停止

ASSR = 0x08//設置異步模式

TCNT2 = ClkTcnt2//設置

TCCR2 = 0x06//開始

}

Void定時器2 OVF(void)//定時器2中斷功能

{

TCNT2 = ClkTcnt2//重新加載計數器值

sec+= 1;

AskLeapYear();

solar count();

AskLeapYear();

count week();

lunar count();//先判斷shi轉換是否成功,再計算顯示值。

if(模式選擇== 0)

{

LCDispClear();

disp update();

}

}

void AskLeapYear(void)

{

if(((Year % 4 = = 0)& amp;& amp(年份% 100!=0)) || Year%400 == 0)//判斷石是否是閏年。

{

LeapYearBit = 1;

}

其他

{

LeapYearBit = 0;

}

}

Void SolarCount(void)//計算每壹位的顯示值。

{

if(秒& gt59)

{

sec = 0;

min++;

}

if(Min & gt;59)

{

min = 0;

hour++;

}

如果(小時& gt23)

{

小時= 0;

day++;

}

if((Month = = 1)| |(Month = = 3)| |(Month = = 5)| |(Month = = 7)| |(Month = = 8)| |(Month = = 10)| |(Month = = 12))& amp;& amp(日& gt31) )

{

month++;

日= 1;

}

else if((Month = = 4)| |(Month = = 6)| |(Month = = 9)| |(Month = = 11))& amp;& amp(日& gt30) )

{

month++;

日= 1;

}

else if (Month==2)

{

If (LeapYearBit == 1)//如果石閏年。

{

if(Day & gt;29)

{

month++;

日= 1;

}

}

else if(Day & gt;28)//平年

{

month++;

日= 1;

}

}

if(Month & gt;12)

{

月= 1;

year++;

}

//計算每個位的de值。

YearBit1 =年/1000;

Year bit 2 = Year % 1000/100;

Year bit 3 = Year % 100/10;

Year bit 4 = Year % 10;

MonthBit1 =月/10;

Month bit 2 = Month % 10;

DayBit1 =日/10;

Day bit 2 = Day % 10;

HourBit1 =小時/10;

hourbit 2 = Hour % 10;

minbit 1 = Min/10;

Min bit 2 = Min % 10;

Sec bit 1 = Sec/10;

Sec bit 2 = Sec % 10;

}

Void DeviceInit(void)//MCU初始化

{

CLI();

PortInit();

timer 2 init();

MCUCR = 0x00

GICR = 0x 00;

TIMSK = 0x40

LCD _ Init();

SEI();

}

//更新顯示

無效顯示更新(無效)

{

if((sec bit 2 & gt;= 5) ||(模式選擇!= 0))//當最後壹位大於5時,顯示公歷。

{

PutStringLCD(0,0,“太陽能”);

PutNumberLCD(0,6,yearbit 1);

PutNumberLCD(0,7,year bit 2);

PutNumberLCD(0,8,year bit 3);

PutNumberLCD(0,9,year bit 4);

PutNumberLCD(0,11,month bit 1);

PutNumberLCD(0,12,month bit 2);

PutNumberLCD(0,14,day bit 1);

PutNumberLCD(0,15,day bit 2);

}

else if(lunarconvertbit = = 1)//秒當最後壹位數字小於5且農歷轉換成功時,顯示農歷。

{

PutStringLCD(0,0,“Lunar”);

PutNumberLCD(0,6,lunar year 1);

PutNumberLCD(0,7,lunar year 2);

PutNumberLCD(0,8,lunar year 3);

PutNumberLCD(0,9,lunar year 4);

PutNumberLCD(0,11,lunarmonth 1);

PutNumberLCD(0,12,lunarmonth 2);

PutNumberLCD(0,14,lunarday 1);

PutNumberLCD(0,15,lunar day 2);

}

其他

{

putstringcd(0,0,“超出範圍!”);

}

PutNumberLCD(1,0,hourbit 1);

PutNumberLCD(1,1,hourbit 2);

if(SecOddBit)

{

PutOneCharLCD(1,2,":);

}

PutNumberLCD(1,3,minbit 1);

PutNumberLCD(1,4,minbit 2);

if(SecOddBit)

{

PutOneCharLCD(1,5,":);

}

PutNumberLCD(1,6,sec bit 1);

PutNumberLCD(1,7,sec bit 2);

PutStringLCD(1,9,Week[today Week]);

if(ChangeModeBit & amp;& ampSecOddBit)

{

PutStringLCD(1,12,change symbol[mode select-1]);

}

}

void CountWeek(無效)

{

if(LeapYearBit == 1)

{

today week =((Year/100% 4 * 5)+(Year % 100/4 * 5)+(Year % 4)+1+Day+LeapYearWeekTable[Month-1])% 7;

}

其他

{

today week =((Year/100% 4 * 5)+(Year % 100/4 * 5)+(Year % 4)+1+Day+unleapwektable[Month-1])% 7;

}

}

空鍵掃描(空)

{ if((PINC & amp;(1 & lt;& ltPC5)) == 0)

{

OldKeyNum = 1;

}

else if((PINC & amp;(1 & lt;& ltPC4)) == 0)

{

OldKeyNum = 2;

}

else if((PINC & amp;(1 & lt;& ltPC3)) == 0)

{

OldKeyNum = 3;

}

如果((PINC & amp(1 & lt;& ltPC5))& amp;& amp(品& amp(1 & lt;& ltPC4))& amp;& amp(品& amp(1 & lt;& lt)//判斷是否有被石按下的按鍵。

{

NewKeyNum = OldKeyNum

OldKeyNum = 0;

}

}

無效時間更改(無效)

{

開關(模式選擇)

{

案例0:

{

if(NewKeyNum == 2)

{

ModeSelect+= 1;

}

}破;

案例1://更改年度變量

{

開關(NewKeyNum)

{

案例1:年份-;打破;

案例二:mode select++;打破;

案例三:year++;打破;

默認:break

}

}破;

情況2://更改月份變量

{

開關(NewKeyNum)

{

案例1:月-;打破;

案例二:mode select++;打破;

案例三:月++;打破;

默認:break

}

}破;

情況3://更改每日變量

{

開關(NewKeyNum)

{

案例1:日-;打破;

案例二:mode select++;打破;

案例三:day++;打破;

默認:break

}

}破;

情況4://更改小時變量

{

開關(NewKeyNum)

{

案例1:小時-;打破;

案例二:mode select++;打破;

案例三:小時++;打破;

默認:break

}

}破;

案例5:

{

開關(NewKeyNum)

{

案例1:Min-;打破;

情況二:mode select = 0;打破;

案例三:min++;打破;

默認:break

}

}破;

默認:break

}

}

/***********************

* * * * * * * *主要功能* * * * * * * *

***********************/

void main()

{

device init();

SEI();

for(;;)

{

if(模式選擇!= 0)//判斷石現在是否處於更改時間和日期的狀態。

{

change mode bit = 1;

}

其他

{

change mode bit = 0;

}

If(SecBit2% 2 == 0)//如果秒的最後壹位是偶數,則SecSymbol=0。

{

SecOddBit = 0;

}

其他

{

SecOddBit = 1;

}

key scan();

time change();

if(ChangeModeBit == 1)

{

CLI();

disp update();

SEI();

}

}

}

  • 上一篇:珍藏美好作文
  • 下一篇:請問這幅畫叫什麽名字?妳什麽意思?有
  • copyright 2024吉日网官网