📄 main.c
字号:
/*-----------------------------------------------------*
*工 程 名:clock
*硬件连接:
*程序描述:
*/
/*[头文件]*/
#include "GP32C.h" //映像寄存器地址头文件
#include "KB.h" //键盘中断头文件
#define Light_P PTA //蜂鸣器接在PTA口
#define Light_D DDRA //相应的方向寄存器
#define Light_Pin 1 //蜂鸣器所在的引脚
/*[函数声明]*/
//在LCD第m行第n列显示一个字符
extern void LcdshowCharacter(unsigned char str,unsigned char m,unsigned char n);
extern void Lcd_Command(unsigned char cmd); //命令执行子程序
extern void Lcdshow(unsigned char temp[]) ; //显示时间
extern void Timer1init(void); //定时器1初始化
extern void LcdInit(void); //Lcd初始化子程序
extern void LcdShowCursor(unsigned char m,unsigned char n); //显示光标在第m行,第n列
//显示大小为n字符数组在第m行
extern void LcdShowString(unsigned char str[],unsigned char n,unsigned char m);
extern void Delay(void); //延时
//将输入的键值传送到相应位置的位
extern void Transmission(unsigned char Keyvalue,unsigned char temp[],unsigned char m,unsigned char n);
extern unsigned char Discriminant(unsigned char key); //判断按下的键是否数字键
extern void Changetime(int temp1[],unsigned char temp2[]); //将数组temp1拆分存储到数组temp2
extern void Change(void); //将数组time组合存储到数组Tdate
extern void Change2(void); //将数组alarm组合存储到数组Tdate2
extern void Controle(void); //控制时钟的日期时间合法
extern void Controle2(void); //控制闹钟的日期时间合法
/*全局变量声明*/
unsigned char time[14]; //存放时钟年、月、日、时、分、秒
int Tdate[3]; //存放时钟年、月、日
unsigned char Ttime[3]; //存放时钟时、分、秒
unsigned char alarm[14]; //存放闹钟的年、月、日、时、分、秒
int Tdate2[3]; //存放闹钟年、月、日
unsigned char Ttime2[3]; //存放闹钟时、分、秒
unsigned char Key; //存放按下键的定义值
unsigned char k; //光标向右移动的次数
unsigned char l; //光标向下移动的次数
unsigned char o; //闹钟功能标志位,等于1时开,等于0时关
/*[主函数]*/
void main()
{
unsigned char i;
unsigned char j;
unsigned char Keyvalue;
unsigned char x;
unsigned char y;
unsigned char t;
unsigned char str1[12]="1. Set Time";
unsigned char str2[13]="2. Set Alarm";
unsigned char str3[14]="1. Open Alarm";
unsigned char str4[15]="2. Close Alarm";
asm("SEI"); //关总中断
//[I/O初始化]
Light_D|=1<<Light_Pin; //令蜂鸣器引脚为输出
Light_P|=1<<Light_Pin; //初始时,蜂鸣器"停"
Timer1init(); //调定时器1初始化子程序
KB_Init();
INTKBSCR&= ~(1<<IntBit); //开放键盘中断(IMASKk=1)
asm("CLI"); //开总中断
//赋初时钟始值2007年6月1日12点0分0秒;闹钟初始值2007年6月1日0时0分0秒
Tdate[0]=2007;Tdate[1]=6;Tdate[2]=1;Ttime[0]=12;Ttime[1]=0;Ttime[2]=0;
Tdate2[0]=2007;Tdate2[1]=6;Tdate2[2]=1; Ttime2[0]=0;Ttime2[1]=0;Ttime2[2]=0;
Changetime((int *)Tdate,(unsigned char *)Ttime);
o=0; //初始时关闹钟
i=Ttime[2];y=time[9];
BEGING:
while(1)
{
j=Ttime[2];
if (i!=j) //秒增1
{
Lcdshow((unsigned char *)time); //显示日期时间
if(o==1) LcdshowCharacter('A',1,1); //左上角字符A标志开闹钟功能
if(o==0) LcdshowCharacter(' ',1,1); //关闹钟功能时消去左上角字符A
i=j;
}
x=time[9];
if(y!=x) //整点报时
{ for(t=0;t<3;t++) //报时三响
{ Light_P|=1<<Light_Pin; //蜂鸣器“鸣”
Delay(); //延时
Light_P&=~(1<<Light_Pin); //蜂鸣器“停”
Delay(); //延时
}
y=x;
}
if(Tdate[0]==Tdate2[0]&&Tdate[1]==Tdate2[1]&&Tdate[2]==Tdate2[2]&& //闹钟预设时间响
Ttime[0]==Ttime2[0]&&Ttime[1]==Ttime2[1]&&Ttime[2]==Ttime2[2])
{ for(t=0;t<15;t++) //闹钟
{ Light_P|=1<<Light_Pin; //蜂鸣器“鸣”
Delay(); //延时
Light_P&=~(1<<Light_Pin); //蜂鸣器“停”
Delay(); //延时
}
}
if(Key=='A') //弹出菜单
{
MENU: LcdShowString((unsigned char *)str1,11,1); //显示菜单:1. Set Time;
LcdShowString((unsigned char *)str2,12,2); // 2. Set Alarm
Key=0;
while(1)
{ if(Key=='B') //返回
{ Key=0;
goto BEGING;
}
if(Key=='1') //选择子菜单1
{ Lcdshow((unsigned char *)time);
LcdShowCursor(1,4); //光标初始位置(1,4),第一行第四列
k=1;l=1;
Key=0;
while(1)
{ if(Key=='#') //光标右移
{ ++k;
if(l%2==0) //现在光标在第一行
{ if(k==4||k==7) ++k;
if(k>9) k=0; //现在光标在末端
LcdShowCursor((1+l),(4+k));
}
else //现在光标在第二行
{ if(k==3||k==6) ++k;
if(k>8) k=1; //现在光标在末端
LcdShowCursor((1+l),(4+k));
}
Key=0;
}
if(Key=='*') //光标下移
{ ++l;
if(k==0) ++k; //现在光标在第一行始端
if(k==9) --k; //现在光标在第一行末端
if(l>1) l=0; //现在光标在第二行
LcdShowCursor((1+l),(4+k));
Key=0;
}
if(Key=='B')
{ Key=0;
goto MENU; //返回
}
if(Key=='D') //按下确定键
{ Key=0;
goto BEGING;
}
if(Key!=0&&Discriminant(Key))
{ Keyvalue=Key-0x30;
Transmission(Keyvalue,(unsigned char *) time,(1+l),(4+k));
Change(); //将数组time组合存储到数组Tdate
Controle(); //控制时钟的日期时间合法
Lcdshow((unsigned char *)time);
LcdShowCursor((1+l),(4+k));
Key=0;
}
}
}
if(Key=='2') //选择子菜单2
{ LcdShowString((unsigned char *)str3,13,1); //显示闹钟功能选择:1. Open Alarm
LcdShowString((unsigned char *)str4,14,2); // 2. Close Alarm
Key=0;
if(Key=='1') //选择1. Open Alarm
{ o=1; //开闹钟
Lcdshow((unsigned char *)alarm);
LcdShowCursor(2,5); //光标初始位置(2,5),第二行第五列
k=1;l=1;
Key=0;
while(1)
{ if(Key=='#') //光标右移
{ ++k;
if(l%2==0) //现在光标在第一行
{ if(k==4||k==7) ++k;
if(k>9) k=0; //现在光标在末端
LcdShowCursor((1+l),(4+k));
}
else //现在光标在第二行
{ if(k==3||k==6) ++k;
if(k>8) k=1; //现在光标在末端
LcdShowCursor((1+l),(4+k));
}
Key=0;
}
if(Key=='*') //光标下移
{ ++l;
if(k==0) ++k; //现在光标在第一行始端
if(k==9) --k; //现在光标在第一行末端
if(l>1) l=0; //现在光标在第二行
LcdShowCursor((1+l),(4+k));
Key=0;
}
if(Key=='B') //返回
{ Key=0;
goto MENU;
}
if(Key=='D') //确定
{ Key=0;
goto BEGING;
}
if(Key!=0&&Discriminant(Key))
{ Keyvalue=Key-0x30;
Transmission(Keyvalue,(unsigned char *) alarm,(1+l),(4+k));
Change2(); //将数组alarm组合存储到数组Tdate2
Controle2(); //控制闹钟的日期时间合法
Lcdshow((unsigned char *)alarm);
LcdShowCursor((1+l),(4+k));
Key=0;
}
}
if(Key=='2') //选择2. Close Alarm
{ o=0; //关闹钟
Key=0;
goto BEGING;
}
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -