📄 main.c
字号:
#include <REG52.H>
#include "Header.h"
unsigned char hex2asc(uchar hex);
unsigned char menu(unsigned char MenuNum) ;
unsigned char EditRing(unsigned char Num);
void COM_SendAll();
void ReadTime();
void Scan_RingTime(unsigned char ScanNum);
void Set_time();
void Set_NextRT(unsigned mode);
void X0() interrupt 0
{}
void Time0() interrupt 1
{
TH0=0xEE; //定时器中断,隔5ms
TL0=0x00;
if (RingFlag==1)
{
if (RingET==RingST) {RingFlag=2;Beep=0;}
}
tc++; //检测30秒内无操作则关背光
if (key_flag)
{
LCD_BK=1;
key_flag=0;
KeyT2=0;
}
else
{
KeyT++;
if (KeyT==200) {KeyT2++;KeyT=0;}
if (KeyT2==30) LCD_BK=0;
}
}
void X1() interrupt 2
{}
void Time1() interrupt 3
{}
void Com1() interrupt 4
{
if (RI)
{
RI=0;
Com_buff[Com_count]=SBUF;
In_flag=1;
}
}
void COM_SendAll()
{
unsigned char rMax,i,temp;
temp=Mem_Read(0x00);
rMax=1+3*temp;
for (i=0;i<rMax;i++)
{
temp=Mem_Read(i);
COM_Send(temp);
delay_ms(2);
}
}
unsigned char hex2asc(uchar hex) //16进制转ASC码
{
hex &=0x0f;
if (hex>9)
return ( hex - 10 + 'A' );
else
return ( hex + '0' );
}
unsigned char BCD2HEX(unsigned char BCD)
{
unsigned char temp1,temp2;
temp1=(BCD>>4)*10;
temp2=BCD & 0x0f;
return (temp1+temp2);
}
unsigned char HEX2BCD(unsigned char HEX)
{
unsigned char temp1,temp2;
temp1=(HEX/10)<<4;
temp2=HEX%10;
return (temp1+temp2);
}
void Init() //初始化
{
unsigned char temp;
RingFlag=0;
temp=DS1307_Read(Second ); //初始化时钟芯片
if(temp&0x80) DS1307_Write(Second,(temp&0x7f));
TH0=0xEE; //定时器中断,隔50ms读取一次时间
TL0=0x00;
TH1=0xFD; //串口波特率9600
TL1=0xFD;
TMOD=0x21; //T1以方式2工作,T0以方式0工作
SCON=0xd8; //设置串口工作方式
PCON&=0x7f; //波特率倍频
IE=0x9A; //设置中断允许
// ES=1; //开串口中断
// ET0=1; //定时器中断允许
// ET1=1;
// EA=1; //开总中断
PS=1;
LCD_init(); //初始化液晶
Com_flag=0;
LCD_BK=1;
key_flag=1;
RingFlag=0;
KeyT=0;
Com_over=0;
NextRingNum=1;
}
void ReadTime() //读取时间
{
uchar temp;
temp=DS1307_Read(Yearh);
str_date[0]=(temp>>4)+'0';
str_date[1]=(temp&0x0f)+'0';
temp=DS1307_Read(Year);
str_date[2]=(temp>>4)+'0';
str_date[3]=(temp&0x0f)+'0';
temp=DS1307_Read(Month);
str_date[6]=(temp>>4)+'0';
str_date[7]=(temp&0x0f)+'0';
temp=DS1307_Read(Date);
str_date[10]=(temp>>4)+'0';
str_date[11]=(temp&0x0f)+'0';
temp=DS1307_Read(Hours);
str_time[0]=(temp>>4)+'0';
str_time[1]=(temp&0x0f)+'0';
temp=DS1307_Read(Minute);
str_time[3]=(temp>>4)+'0';
str_time[4]=(temp&0x0f)+'0';
temp=DS1307_Read(Second);
RingST=temp;
str_time[6]=(temp>>4)+'0';
str_time[7]=(temp&0x0f)+'0';
temp=DS1307_Read(Day);
temp-=1;
str_day[4]=str_dayx[temp][0];
str_day[5]=str_dayx[temp][1];
}
unsigned char EditRing(unsigned char Num)
{
unsigned char temp,quit,m;
unsigned char Record[3]={0,0,0};
m=Read_RingTime(Num,Record);
quit=1;
while(quit)
{
while (quit==1)
{
LCD_write(0x01,COMM);
delay_ms(3);
LCD_write_string(0,0,"选择打铃类型");
LCD_write_string(0,1,"按上下键选择");
LCD_write_string(3,2,"普通");
LCD_write_string(3,3,"上课");
if (Record[2])
LCD_set_position(2,3);
else
LCD_set_position(2,2);
LCD_write(0x10,DATT);
key=0xff;
while(key==0xff) key=ScanKey();
if (0xff-key)
{
key_flag=1;
KeyT=0;
}
if ((key==KEY_UP)||(key==KEY_DOWN))
{
if (Record[2]) Record[2]=0;
else Record[2]=0x45;
}
else if (key==KEY_ENT) quit=2;
else if (key==KEY_ESC) quit=0;
}
temp=0;
while(quit==2)
{
LCD_write(0x01,COMM);
delay_ms(3);
LCD_write_string(0,0,"设置打铃时间");
LCD_write_string(0,2,"上下键移动光标");
LCD_write_string(0,3,"数字键输入时间");
LCD_set_position(2,1);
LCD_write((hex2asc(Record[0]>>4)),DATT);
LCD_write((hex2asc(Record[0])),DATT);
LCD_write_string(3,1,"点 分");
LCD_set_position(5,1);
LCD_write(hex2asc(Record[1]>>4),DATT);
LCD_write(hex2asc(Record[1]),DATT);
LCD_set_position(1+temp,1);
LCD_write(0x10,DATT);
key=0xff;
while(key==0xff) key=ScanKey();
if (0xff-key)
{
key_flag=1;
KeyT=0;
}
if ((key==KEY_UP)||(key==KEY_DOWN))
{
if (temp) temp=0;
else temp=3;
}
else if (key<10)
{
if (temp)
{
Record[1]<<=4;
Record[1]+=key;
if (Record[1]>0x59) Record[1]&=0x0f;
}
else
{
Record[0]<<=4;
Record[0]+=key;
if (Record[0]>0x23) Record[0]&=0x0f;
}
}
else if (key==KEY_ENT)
{
if(Record[2]) quit=3;
else quit=4;
}
else if (key==KEY_ESC) quit=1;
}
while(quit==3)
{
LCD_write(0x01,COMM);
delay_ms(3);
LCD_write_string(0,0,"设置上课时长");
LCD_write_string(0,3,"数字键输入时间");
LCD_write_string(4,1,"分钟");
LCD_set_position(3,1);
LCD_write(hex2asc(Record[2]>>4),DATT);
LCD_write(hex2asc(Record[2]),DATT);
key=0xff;
while(key==0xff) key=ScanKey();
if (0xff-key)
{
key_flag=1;
KeyT=0;
}
if (key<10)
{
Record[2]<<=4;
Record[2]+=key;
if (Record[2]==0) Record[2]=0x01;
}
else if (key==KEY_ENT) quit=4;
else if (key==KEY_ESC) quit=2;
}
if(quit==4)
{
Write_RingTime(Num,Record);
if (m==0)
{
temp=Mem_Read(0);
temp++;
Mem_Write(0,temp);
}
Ring_Paixu();
Set_NextRT(1);
return (Num);
}
}
return (0);
}
void Scan_RingTime(unsigned char ScanNum)
{
uchar RingCount,temp,quit,MC,Record[3];
RingCount=Mem_Read(0x00);
quit =1;
if (RingCount==0)
{
MC=menu(NORECORD);
if (MC==1) quit=EditRing(0x01);
else quit=0;
}
// ScanNum=1;
while (quit)
{
LCD_write(0x01,COMM);
delay_ms(3);
LCD_write_string(0,0,"===== 浏览 =====");
temp=Read_RingTime(ScanNum,Record);
if(temp)
{
LCD_set_position(0,1);
LCD_write(hex2asc(ScanNum>>4),DATT);
LCD_write(hex2asc(ScanNum),DATT);
LCD_write_string(1,1,"铃声类型:");
LCD_write_string(1,2,"时间: ");
LCD_write(hex2asc(Record[0]>>4),DATT);
LCD_write(hex2asc(Record[0]),DATT);
LCD_write(':',DATT);
LCD_write(hex2asc(Record[1]>>4),DATT);
LCD_write(hex2asc(Record[1]),DATT);
if(Record[2])
{
LCD_write_string(6,1,"上课");
LCD_write_string(1,3,"上课时长:");
LCD_write(hex2asc(Record[2]>>4),DATT);
LCD_write(hex2asc(Record[2]),DATT);
}
else
{
LCD_write_string(6,1,"普通");
}
}
else
{
if (ScanNum>0) ScanNum--;
else quit=0;
}
key=0xff;
while((key==0xff)&&(quit)) key=ScanKey();
if (0xff-key)
{
key_flag=1;
KeyT=0;
}
if((key==KEY_UP)&&(ScanNum>1)) ScanNum--;
else if((key==KEY_DOWN)&&(ScanNum<RingCount)) ScanNum++;
else if((key==KEY_ENT)||(key==KEY_FUNC))
{
MC=menu(SCANMENU);
if(MC==0) quit=0;
else if (MC==1)
{
quit=EditRing(ScanNum);
if (quit) ScanNum=quit;
}
else if (MC==2)
{
quit= EditRing(RingCount+1);//EditRing(ScanNum);
if (quit)
{
ScanNum=quit;
RingCount++;
}
}
else if (MC==3)
{
RingCount=DelRing(ScanNum);
if(RingCount)
{
if (ScanNum>RingCount)
ScanNum--;
Set_NextRT(1);
}
else
{Scan_RingTime(1);quit=0;}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -