📄 main.c
字号:
/*
**********************************************************************************
* 此模块为主程序,包括初始化各模块变量值及联结各模块 *
**********************************************************************************
*/
#include <reg668.h>
#include <constant.h>
#include <Task.h>
//#pragma optimize(5)
bit Dot_Value;
//bit ClockFlag;
bit TimeOpenflag=0;
bit TimerFlag=0;
unsigned char SongCirFg=0;
bit Time250msFlag=0;
bit Time250msFlag1=0;
bit flash,UpDownFlag=1;
bit Adjust=0;
sbit Speaker = P3^5; //扬声器控制脚
sbit RedLight= P3^4;
sbit GreenLight= P3^3;
//sfr T2MOD = 0x0C9;
sfr button = 0x0A0;
extern unsigned char mon,dat,hou,min,sec;
extern unsigned int yea;
unsigned char CountTime0;
unsigned char CountTime2;
unsigned char Disp_array[Eight];
unsigned char idata Timer=2,TimerArray[20][3];
unsigned char formflag=0,segflag=0;
unsigned char hout,mint;
unsigned char song;
unsigned char delay1min=0;
unsigned char HL2[2];
unsigned char RcapCnt=0;
/*
**********************************************************************************
* 延时程序:void delayx (unsigned char x)
**********************************************************************************
*/
void delayx (unsigned char x)
{
unsigned int i,j,k;
for (i=0;i<=x;i++)
{
for (j=0;j<10000;j++)
{
k++;
}
}
}
/*
**********************************************************************************
* 系统主程序:main
**********************************************************************************
*/
void main (void)
{
unsigned char i=0;
PCON=0x00; //no double baud rate
S0CON=0x50; //10-bit UART
TL0=0x00; //40 加快3min per day
TH0=0x0E2; //Value of Time is 10ms,the crystal is 18.432Mhz
ET0=1; //Timer 0 interrupt enable
TH1=0x0F6; //baud-rate: 9600bps 18.432MHz
TL1=0x0F6;
//ES0=1; //UART interrupt enable
TCON=0x05; //INT0,INT1 is edge trigger
TMOD=0x21; //Timer1 is auto load eight timer
//Timer0 is 16-bit timer
T2CON = 0x00; //16-bit Auto-reload
T2MOD = 0x00; //Clear T2MOD
TH2 = 0x88; //Value of Time is 5ms
TL2 = 0x00;
RCAP2H = 0x88; //Auto reload
RCAP2L = 0x00;
HL2[0] = 0x88;
HL2[1] = 0x00;
ET2 = 1; //Timer 2 interrupt enable
P0=0x0FF; //Initialize P0~3
P1=0x0FF;
P2=0x0FF;
P3=0x0FF;
//TR1=1; //Start Timer 1
TR0=1; //Start Timer 0
TR2=1; //Start Timer 2
EA=1; //Global enable
Dot_Value=0;
//ClockFlag=1;
CountTime0=0x00;
CountTime2=0x00;
Disp_array[0]=0x00;
Disp_array[1]=0x07;
Disp_array[2]=0x01;
Disp_array[3]=0x08;
Disp_array[4]=0x00;
Disp_array[5]=0x00;
Disp_array[6]=0x00;
Disp_array[7]=0x00;
TimerArray[0][0]=5;
TimerArray[0][1]=30;
TimerArray[0][2]=1;
TimerArray[1][0]=22;
TimerArray[1][1]=0;
TimerArray[1][2]=1;
TimerArray[2][0]=14;
TimerArray[2][1]=0;
TimerArray[2][2]=1;
hou=min=sec=0;
mon=7;
dat=18;
yea=2005;
while (TRUE)
{
//while (Time0IntFlag==0);
//Time0IntFlag=0;
if (button!=0x0FF)
{
delayx(2);
if (button!=0x0FF)
{
switch(button)
{
case 0x0FE: //Press button1
switch(formflag)
{
case 0: //Main form
segflag=(segflag+1)%5;
if (segflag !=0)
{
Adjust=1;
sec=0;
}
else
{
Adjust=0;
}
break;
case 1: //Timer form
segflag=(segflag+1)%2;
break;
case 2:
segflag=(segflag+1)%5;
break;
case 3: //Song form
segflag=(segflag+1)%3;
break;
case 4:
segflag=(segflag+1)%4; //Display clock adjust
break;
default:
formflag=0;
break;
}
break;
case 0x0FD: //Press button2
switch(formflag)
{
case 0: //Adjust clock
switch(segflag)
{
case 1: //调整月
mon=mon+1;
if (mon>12)
{
mon=1;
}
break;
case 2: //调整天
dat++;
switch (mon)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (dat>31)
{
dat=0x01;
}
break;
case 4:
case 6:
case 9:
case 11:
if (dat>30)
{
dat=0x01;
}
break;
case 2:
if (yea%4==0&&yea%100!=0||yea%400==0)
{
if (dat>29)
{
dat=0x01;
}
}
else
{
if (dat>28)
{
dat=0x01;
}
}
break;
default:
mon=0x01;
break;
}
break;
case 3: //调整时
hou=(hou+1)%24;
break;
case 4: //调整分
min=(min+1)%60;
break;
default:
segflag=0;
break;
}
break;
case 1: //定时器开关设定
if (segflag==1)
TimeOpenflag = ~TimeOpenflag;
else
segflag=0;
break;
case 2: //定时时钟调节
switch(segflag)
{
case 1:
Timer = (Timer+1)%20;
break;
case 2:
TimerFlag = ~TimerFlag;
break;
case 3:
hout = (hout+1)%24;
break;
case 4:
mint = (mint+1)%60;
break;
default:
segflag=0;
break;
}
break;
case 3: //乐曲设定
switch(segflag)
{
case 1:
song = (song+1)%20;
break;
case 2:
SongCirFg = (SongCirFg+1)%3;
break;
default:
segflag=0;
break;
}
break;
case 4:
switch(segflag)
{
case 1:
RcapCnt = (RcapCnt+1)%2;
break;
case 2:
UpDownFlag =~ UpDownFlag;
break;
case 3:
if (UpDownFlag==1)
{
HL2[RcapCnt] = HL2[RcapCnt]+1;
}
else
{
HL2[RcapCnt] = HL2[RcapCnt]-1;
}
break;
default:
segflag=0;
break;
}
break;
default:
formflag=0;
break;
}
break;
case 0x0FB: //Press button3
formflag = (formflag+1)%5;
Adjust=0;
segflag=0;
break;
case 0x0F7: //Press button4(default)
TimeOpenflag =~ TimeOpenflag; //Switch On/Off speaker
break;
default:
break;
}
}
}
switch(formflag) //Display
{
case 0:
switch(segflag)
{
case 0:
Disp_array[0]=mon/10;
Disp_array[1]=mon-10*(mon/10);
Disp_array[2]=dat/10;
Disp_array[3]=dat-10*(dat/10);
Disp_array[4]=hou/10;
Disp_array[5]=hou-10*(hou/10);
Disp_array[6]=min/10;
Disp_array[7]=min-10*(min/10);
break;
case 1:
if (Time250msFlag==1)
{
Time250msFlag=0;
if (flash==1)
{
flash=0;
Disp_array[0]=mon/10;
Disp_array[1]=mon-10*(mon/10);
}
else
{
flash=1;
Disp_array[0]=16;
Disp_array[1]=16;
}
}
break;
case 2:
if (Time250msFlag==1)
{
Time250msFlag=0;
if (flash==1)
{
flash=0;
Disp_array[2]=dat/10;
Disp_array[3]=dat-10*(dat/10);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -