Traditional Culture Encyclopedia - The 24 Solar Terms - Write a simple perpetual calendar with c++

Write a simple perpetual calendar with c++

I will give you two programs, which I collected. I have finished this eda design.

# 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 (* (volatile unsigned character *)0x6000)

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

#define timer_sec (* (volatile unsigned character *)0x2000)

#define timer_min (* (volatile unsigned character *)0x2002)

# Define timer _ hour (* (volatile unsigned char *) 0x2004)

#define timer_week (* (volatile unsigned character *)0x2006)

# Define timer _ day (* (volatile unsigned char *) 0x2007)

#define timer_moth (* (volatile unsigned character *)0x2008)

# Define timer _ year (* (volatile unsigned char *) 0x2009)

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

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

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

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

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

Unsigned char i, k, sn0, j, a, s, data

Unsigned characters sec_buf, min_buf, hour_buf, sec0, sec 1, min0, min 1, hour0, hour1;

Unsigned characters year, month, day, week, hour, minute and second;

Unsigned char year_buf, mother _ buf, day_buf, week_buf, year0, year 1, mother 0, mother1,day0, day1;

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

Void delay 1ms (unsigned integer delay time)

{

while(delay _ time & gt; 0)

{

for(I = 125; I>0; I-)

{; }

delay _ time-;

}

}

///////////

Void delay 1us (unsigned character delay_time 1)

{

while(delay_time 1! =0)

{

delay _ time 1-;

}

}

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

Void display (unsigned char lcm_adr, unsigned char tab0[sn0])

{

write _ lcm _ ins = lcm _ adr

delay 1 ms(2);

Unsigned character sn1=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 ' };

Unsigned chattab3 [] = {"day one two three four five six"};

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

Invalid display (data)

{

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

delay 1 ms(2);

}

/////////

Void disp_week (unsigned character 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);

}

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

Invalid time setting (year, month, day, week, hour, minute, second)

{

Timer _ year = year;

timer _ moth = moth

Timer _ day = day;

Timer _ week = week;

Timer _ hour = hours;

timer _ min = min

Timer _ sec = seconds;

}

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

Void display_time (unsigned character 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;

///

Hours 1 = hours _ buffer/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 (unsigned character 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 (method1);

Disp (method 0);

write _ LCM _ data = '-';

delay 1 ms(2);

disp(day 1);

Disp (day 0);

write _ LCM _ data = '-';

delay 1 ms(2);

write _ LCM _ data = '(';

delay 1 ms(2);

Disp_week (week);

write _ LCM _ data = ')';

delay 1 ms(2);

}

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

int main (void)

{

SP = 0x 0 fff;

MCUCR = 0x80

EICRA = 0x00///Extended Extended Integer

EICRB = 0x00// extended extended integer

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///enables ex_ram.

XM CRA = 0b 0000 1 1 10;

XM CRB = 0b 1000000;

k = 2;

And (k! =0)

{

PORTF = 0b 000 1 1000;

delay 1 ms(200);

PORTF = 0b 00 100 100;

delay 1 ms(200);

PORTF = 0b 0 10000 10;

delay 1 ms(200);

PORTF = 0b 100000 1;

delay 1 ms(200);

PORTF = 0b 0 10000 10;

delay 1 ms(200);

PORTF = 0b 00 100 100;

delay 1 ms(200);

k-;

}

ext _ ram 2 = 0b 10 10 10 10;

delay 1 ms(500);

PORTF = ext _ ram2

delay 1 ms(500);

PORTD = 0X00

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

write _ LCM _ ins = 0x 0 1;

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);

Display (0x80, "electronic calendar program");

Display (0x90, "-");

Display (0x88, "-");

Display (0x98, "");

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

timer _ ra = 0b 10 100000;

timer _ Rb = 0b 00000 1 10;

timer _ rc = 0b00000000

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

s = 4;

while( 1)

{

Display date (0x88);

display _ time(0x9a);

while(s==0)

{

s = 4;

PORTF = ~ PORTF

}

PORTD | = 0b 00 100000;

delay 1 ms(2);

Porter & amp = 0b110111;

s-;

}

}

One more

#i includes & ltiom8v.h & gt

#i including < macros.h >

# Define the data port pull-up port

# Define data port direction DDRD

# define PIND readlcdp

# define CtrlPortPullup port c

# Define CtrlPortDirection DDRC

# define RS_Bit PC0

# Define RW_Bit PC 1

# Define E_Bit PC2

#i includes "LCD 1620.h"

# Define ClkTcnt2 0x80

/* * * * * * * * * * * * * * variable declaration * * * * * * * * * * * *

Unsigned int Year = 2008// annual variable

Unsigned character month = 6; //Monthly variable

Unsigned charday =1;

Unsigned character hours =12; //hour

Unsigned character minimum value = 0; //minute

Unsigned character seconds = 0; //sec

Unsigned characters YearBit 1, YearBit2, YearBit3, YearBit4

Unsigned char MonthBit 1, MonthBit2

Unsigned characters DayBit 1, DayBit2

Unsigned characters HourBit 1, HourBit2

Unsigned characters MinBit 1, MinBit2

Unsigned characters SecBit 1, SecBit2

Unsigned character week [][4] =

{"Monday", "Tuesday", "Wednesday", "Thursday", "Fri", "Saturday" and "Sunday"};

Unsigned characters ChangeSymbol[][5] = {"-Yar ","-Mth ","-Day ","-Hor ","-min "};

Unsigned charleapearweektable [] = {3, 6, 0, 3, 5, 1, 3, 6, 2, 4, 0, 2}; //leap year week deviation table

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

Unsigned character todayweek = 0;

Unsigned character LeapYearBit = 0;; When//is 0, the year is the average.

Unsigned charlunardata [] = {0,0,0,0};

Unsigned char LunarYear 1, LunarYear2, LunarYear3, LunarYear4, LunarMonth 1, LunARMonth2, LunarDay 1, LunarDay2.

Unsigned character LunarConvertBit//0, lunar calendar conversion succeeded.

Unsigned character calendar stop bit/1,the clock is paused.

Unsigned character OldKeyNum = 0;;

Unsigned character NewKeyNum = 0;;

Unsigned character mode select = 0;;

Unsigned character changemode bit = 0; //ModeSelect! = 0, that is, 1, indicating that it is now in a time-varying state.

Unsigned character SecOddBit = 0;; //seconds is even, this is 0, otherwise it is 1.

Static unsigned character constant LunarDayCode 1[9]=

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

Static unsigned short integer constant Lunar Day Code2 [3] = {0x11,0x 130, 0x14e};

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

* * * * * * * subfunction * * * * * * * * *

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

Void PortInit(void) // port initialization

{

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

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

}

Void timer2init (void)//Timer2 initialization

{

TCCR2 = 0x00// Stop

ASSR = 0x08// Set asynchronous mode.

TCNT2 = ClkTcnt2// setting

TCCR2 = 0x06// Start

}

Void Timer2OVF (Void)//Timer2 interrupt function

{

TCNT2 = ClkTcnt2///Reload the counter value.

sec+= 1;

AskLeapYear();

solar count();

AskLeapYear();

count week();

lunar count(); //Judge whether the shi conversion is successful first, and then calculate the displayed value.

If (mode selection == 0)

{

LCDispClear();

disp update();

}

}

void AskLeapYear(void)

{

if(((Year % 4 = = 0)& amp; & (year% 100! =0)) || Year%400 == 0)// Judge whether Stone is a leap year.

{

LeapYearBit = 1;

}

other

{

LeapYearBit = 0;

}

}

Void SolarCount(void)// Calculate the display value of each bit.

{

If (second & gt59)

{

sec = 0;

min++;

}

if(Min & gt; 59)

{

min = 0;

hour++;

}

If (hours & gt23)

{

Hours = 0;

day++;

}

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

{

month++;

Day =1;

}

else if((Month = = 4)| |(Month = = 6)| |(Month = = 9)| |(Month = = 1 1))& amp; & (Japan & gt30))

{

month++;

Day =1;

}

else if (Month==2)

{

If (LeapYearBit == 1)// If it is a leap year.

{

if(Day & gt; 29)

{

month++;

Day =1;

}

}

else if(Day & gt; 28)// Pingnian

{

month++;

Day =1;

}

}

if(Month & gt; 12)

{

Month =1;

year++;

}

//Calculate the de value of each bit.

YearBit 1 = year/1000;

Year bit 2 = Year % 1000/ 100;

Year bit 3 = Year % 100/ 10;

Year bit 4 = Year % 10;

MonthBit 1 = month/10;

Month bit 2 = Month % 10;

DayBit 1 = day/10;

Day bit 2 = Day % 10;

HourBit 1 = hours/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 initialization

{

CLI();

PortInit();

timer 2 init();

MCUCR = 0x00

GICR = 0x 00;

TIMSK = 0x40

LCD _ Init();

SEI();

}

//Update the display

Invalid display update (invalid)

{

if((sec bit 2 & gt; = 5) || (mode selection! = 0))// When the last digit is greater than 5, the Gregorian calendar is displayed.

{

Putstringlicd (0,0, "solar energy");

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, 1 1,month bit 1);

PutNumberLCD(0, 12,month bit 2);

PutNumberLCD(0, 14,day bit 1);

PutNumberLCD(0, 15,day bit 2);

}

Elseif (lunarconvertbit =1)//sec When the last digit is less than 5 and the lunar calendar conversion is successful, the lunar calendar is displayed.

{

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, 1 1,lunarmonth 1);

PutNumberLCD(0, 12,lunarmonth 2);

PutNumberLCD(0, 14,lunarday 1);

PutNumberLCD(0, 15,lunar day 2);

}

other

{

Putstringcd (0,0, "Out of range!" );

}

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 (invalid)

{

if(LeapYearBit == 1)

{

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

}

other

{

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

}

}

Null key scan (null)

{ 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;

}

If ((Pinc & amp (1<; & ltPC5))& amp; & (Pin & (1<; & ltPC4))& amp; & (Pin & (1<; & lt)// Determine whether there is a key pressed by a stone.

{

NewKeyNum = OldKeyNum

OldKeyNum = 0;

}

}

Invalid time change (invalid)

{

Switch (mode selection)

{

Case 0:

{

if(NewKeyNum == 2)

{

ModeSelect+= 1;

}

} broken;

Case 1:// Changing annual variables

{

Switch (NewKeyNum)

{

Case 1: Year-; Break;

Case 2: mode select++;+; Break;

Case 3: year++;+; Break;

Default: break

}

} broken;

Case 2:// Change the month variable

{

Switch (NewKeyNum)

{

Case 1: month-; Break;

Case 2: mode select++;+; Break;

Case 3: Month++; Break;

Default: break

}

} broken;

Situation 3:// Changing daily variables

{

Switch (NewKeyNum)

{

Case 1: day-; Break;

Case 2: mode select++;+; Break;

Case 3: day++;+; Break;

Default: break

}

} broken;

Situation 4:// Change the hour variable

{

Switch (NewKeyNum)

{

Case 1: hour-; Break;

Case 2: mode select++;+; Break;

Case 3: Hours++; Break;

Default: break

}

} broken;

Case 5:

{

Switch (NewKeyNum)

{

Case1:min-; Break;

Case 2: mode select = 0;; Break;

Case 3: min++;+; Break;

Default: break

}

} broken;

Default: break

}

}

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

* * * * * * * Main functions * * * * * * *

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

void main()

{

device init();

SEI();

for(; ; )

{

If (mode selection! = 0)// Judge whether the stone is in the state of changing the time and date.

{

change mode bit = 1;

}

other

{

change mode bit = 0;

}

If(SecBit2% 2 == 0)// If the last bit of a second is even, SecSymbol=0.

{

SecOddBit = 0;

}

other

{

SecOddBit = 1;

}

key scan();

time change();

if(ChangeModeBit == 1)

{

CLI();

disp update();

SEI();

}

}

}