?? main.c
字號:
else if(key==KEY_ESC) quit=0;
}
LCD_write(0x01,COMM); //清屏,字符緩沖區(qū)
delay_ms(3);
}
void Set_time() //時鐘設(shè)置
{
uchar Pdat[]={ 0,6,10,10,4,7,10,
0,0,0,1,2,2,2,
4,2,2,2,2,2,2
};
uchar Max[]={0x59,0x59,0x23,0x07,0x31,0x12};
uchar temp,temp2;
uchar P=0;
uchar quit=1;
uchar flag=1;
LCD_write(0x01,COMM); //清屏,字符緩沖區(qū)
delay_ms(3);
LCD_clr_pic(); //清圖形緩沖區(qū)
while (quit)
{
key=ScanKey();
if (0xff-key)
{
key_flag=1;
KeyT=0;
}
ReadTime();
LCD_write_string(0,0,str_date);
LCD_write_string(3,1,str_day);
LCD_write_string(2,2,str_time);
if(flag)
{
flag=0;
LCD_Convert(Pdat[P],Pdat[P+7],Pdat[P+14],1);
}
if ((key==KEY_UP)&&(P>0))
{
LCD_Convert(Pdat[P],Pdat[P+7],Pdat[P+14],0);
P--;
LCD_Convert(Pdat[P],Pdat[P+7],Pdat[P+14],1);
}
else if ((key==KEY_DOWN)&&(P<6))
{
LCD_Convert(Pdat[P],Pdat[P+7],Pdat[P+14],0);
P++;
LCD_Convert(Pdat[P],Pdat[P+7],Pdat[P+14],1);
}
else if (key<10)
{
if(P==0)
{
temp=DS1307_Read(Yearh);
temp<<=4;
temp2=DS1307_Read(Year);
temp2>>=4;
temp=temp|temp2;
DS1307_Write(Yearh,temp);
temp=DS1307_Read(Year);
temp<<=4;
temp+=key;
DS1307_Write(Year,temp);
flag=1;
}
else if(P==3)
{
if (key>0&&key<8) DS1307_Write(Day,key);
flag=1;
}
else
{
temp=DS1307_Read(6-P);
temp<<=4;
temp+=key;
if (temp>Max[6-P]) temp &= 0x0f;
if (temp==0&&P<3) temp+=1;
DS1307_Write(6-P,temp);
flag=1;
}
}
else if((key==KEY_ENT)||(key==KEY_ESC)) quit=0;
// delay_ms(45);
}
Set_NextRT(1);
}
unsigned char menu(unsigned char MenuNum) //主菜單,返回所選擇的菜單號
{
uchar i,rt;
uchar quit;
uchar Line=1;
LCD_write(0x01,COMM); //清屏,字符緩沖區(qū)
delay_ms(3);
LCD_clr_pic(); //清屏,圖形緩沖區(qū)
for(i=0;i<MenuCount[MenuNum];i++) //顯示菜單項
LCD_write_string(0,i,str_Menu[MenuNum][i]);
quit=1;
while (quit)
{
LCD_Convert(0,Line,16,1); //反白選中行
key=0xff;
while(key==0xff) key=ScanKey();
key_flag=1;
KeyT=0;
LCD_Convert(0,Line,16,0); //取消反白
if ((key==KEY_DOWN)&&(Line<MenuCount[MenuNum]-1))
{
Line ++;
}
else if ((key==KEY_UP)&&(Line>1))
{
Line --;
}
else if (key==KEY_ENT)
{
rt=Line;
quit=0;
}
else if(key>0 && key<MenuCount[MenuNum])
{rt=key; quit=0;}
else if (key==KEY_ESC)
{rt= 0;quit=0;}
}
LCD_write(0x01,COMM); //清屏,字符緩沖區(qū)
delay_ms(3);
LCD_clr_pic(); //清屏,圖形緩沖區(qū)
return (rt);
}
void Set_NextRT(unsigned mode) //mode=1 從頭開始查找時間表,mode=0 繼續(xù)查詢
{
unsigned char nh,nm,rc,rh,rm,rt,i;
nh=BCD2HEX(DS1307_Read(Hours));
nm=BCD2HEX(DS1307_Read(Minute));
rc=Mem_Read(0);
if (mode) i=0;
else
{
i=NextRingNum-1;
}
for (;i<rc;i++)
{
NextRingNum=i+1;
Read_RingTime(NextRingNum,NextRingTime);
rh=BCD2HEX(NextRingTime[0]);
rm=BCD2HEX(NextRingTime[1]);
rt=BCD2HEX(NextRingTime[2]);
if (rt) //上課鈴
{
if (rm<3) //計算打預(yù)備鈴的時間
{
rm+=57;
rh--;
}
else
{
rm-=3;
}
if ((nh<rh)||((nh==rh)&&(nm<rm)))
{
NextRingTime[0]=HEX2BCD(rh);
NextRingTime[1]=HEX2BCD(rm);
break;
}
else
{
rh=BCD2HEX(NextRingTime[0]); //計算打上課鈴的時間
rm=BCD2HEX(NextRingTime[1]);
if ((nh<rh)||((nh==rh)&&(nm<rm)))
{
NextRingTime[0]=HEX2BCD(rh);
NextRingTime[1]=HEX2BCD(rm);
break;
}
else
{
if (rm+rt>59) //計算打下課鈴的時間
{
rh++;
rm=rm+rt-60;
}
else
{
rm=rm+rt;
}
if ((nh<rh)||((nh==rh)&&(nm<rm)))
{
NextRingTime[0]=HEX2BCD(rh);
NextRingTime[1]=HEX2BCD(rm);
NextRingNum++;
break;
}
else
continue;
}
}
}
else //普通鈴
{
if (nh>rh) continue;
if ((nh==rh)&&(nm>rm)) continue;
NextRingNum++;
break;
}
}
}
void DoCom()
{
In_flag=0;
if (Com_flag==2) //串口狀態(tài)為二,接收數(shù)據(jù)
{
if (Com_buff[Com_count]==0xff) //如果為停止標(biāo)志,則停止接收,置串口狀態(tài)標(biāo)志為4,傳輸結(jié)束,準(zhǔn)備存儲數(shù)據(jù)
{
Com_flag=3;
}
else
Com_count++;
}
else
if ((Com_flag==0)&&(Com_buff[Com_count]=='R'))//未連接時,PC端發(fā)送'R',單片機接受后發(fā)送'J',實現(xiàn)握手,置已連接信號
{
COM_Send('J');
Com_flag=1;
}
else if(Com_flag==1) //已連接時
{
if (Com_buff[Com_count]=='O') //若接收到字母'O',表示讀取打鈴時間表數(shù)據(jù)并傳送到PC端
{
COM_Send('Y'); //先發(fā)送一個'Y'應(yīng)答,然后發(fā)送所有數(shù)據(jù)
COM_SendAll();
COM_Send(0xff);
Com_flag=0;
}
else if (Com_buff[Com_count]=='I') //若接收到'I',表示從PC端接收打鈴時間表并刷新內(nèi)存數(shù)據(jù)
{
COM_Send('R'); //先發(fā)送一個'R'應(yīng)答,然后設(shè)置串口狀態(tài)二,接受數(shù)據(jù)
Com_flag=2;
Com_count=0;
}
else
Com_flag=0;
}
else Com_flag=0;
}
void main()
{
uchar MC,temp,reflash;
Beep=0;
Init();
LCD_write(0x01,COMM);
Read_Temperature(Tp);
ReadTime();
LCD_write_string(0,0,str_date);
LCD_write_string(3,1,str_day);
LCD_write_string(2,2,str_time);
TR1=0;
Read_Temperature(Tp);
TR1=1;
TR0=1;
LCD_write_string(1,3,"現(xiàn)在溫度");
LCD_write_string(5,3,Tp);
Set_NextRT(1);
RingFlag=0;
while(1)
{
if (In_flag) DoCom();
if (Com_flag==3)
{
for (temp=0;temp<(3*Com_buff[0]+1);temp++)
Mem_Write(temp,Com_buff[temp]);
Com_flag=0;
Ring_Paixu();
COM_Send(0xff);
Set_NextRT(1);
}
if (tc) //每5ms刷新一次時間
{
ReadTime();
tc=0;
reflash=1;
if (Com_flag!=2 && KeyT/80)
{
TR1=0; //刷新溫度
Read_Temperature(Tp);
TR1=1;
}
if (RingFlag==0) //檢查是否該打鈴
{
temp=DS1307_Read(Hours);
if (temp==NextRingTime[0])
{
temp=DS1307_Read(Minute);
if (temp==NextRingTime[1])
{
Beep=1;
RingET=DS1307_Read(Second);
if (RingET<0x30) RingET+=0x30;
else RingET-=0x30;
RingFlag=1;
}
}
}
if (RingFlag==2&&(NextRingTime[1]!=DS1307_Read(Minute)))
{
Set_NextRT(0);
RingFlag=0;
}
}
if (reflash)
{
reflash=0;
LCD_write_string(0,0,str_date);
LCD_write_string(3,1,str_day);
LCD_write_string(2,2,str_time);
LCD_write_string(1,3,"現(xiàn)在溫度");
LCD_write_string(5,3,Tp);
}
key = ScanKey();
if (0xff-key)
{
key_flag=1;
KeyT=0;
if (key==KEY_FUNC)
{
MC=menu(0);
if (MC==1)
{
temp=Mem_Read(0);
temp=EditRing(temp+1);
if (temp) Scan_RingTime(temp);
}
else if (MC==2)
{
Set_time();
}
LCD_write(0x01,COMM); //清屏,字符緩沖區(qū)
delay_ms(2);
LCD_clr_pic(); //清屏,圖形緩沖區(qū)
MC=0;
reflash=1;
}
else if(key==KEY_UP||key==KEY_DOWN) {Scan_RingTime(1);reflash=1;}
else if(key<10) {Scan_RingTime(key);reflash=1;}
else if (key==KEY_RST) COM_Send('s');
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -