⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 智能打铃系统(MCU)端程序
💻 C
📖 第 1 页 / 共 2 页
字号:
			else if(key==KEY_ESC) quit=0;
	}
	LCD_write(0x01,COMM);		//清屏,字符缓冲区
	delay_ms(3);
}

void Set_time()									//时钟设置
{
	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);				//清屏,字符缓冲区
	delay_ms(3);
	LCD_clr_pic();								//清图形缓冲区

	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);		//清屏,字符缓冲区
	delay_ms(3);
	LCD_clr_pic();					//清屏,图形缓冲区
	
	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);		//清屏,字符缓冲区
	delay_ms(3);
	LCD_clr_pic();					//清屏,图形缓冲区
	return (rt);
}

void Set_NextRT(unsigned mode)				//mode=1 从头开始查找时间表,mode=0 继续查询
{
	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) 		//计算打预备铃的时间
				{
					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)					//串口状态为二,接收数据
	{
		if (Com_buff[Com_count]==0xff)				  //如果为停止标志,则停止接收,置串口状态标志为4,传输结束,准备存储数据	
		{
			Com_flag=3;
		}
		else
			Com_count++;
	}
	else
	if ((Com_flag==0)&&(Com_buff[Com_count]=='R'))//未连接时,PC端发送'R',单片机接受后发送'J',实现握手,置已连接信号
	{
		COM_Send('J');
		Com_flag=1;
	}
	else if(Com_flag==1)						//已连接时
	{
		if (Com_buff[Com_count]=='O')							//若接收到字母'O',表示读取打铃时间表数据并传送到PC端
		{
			COM_Send('Y');						//先发送一个'Y'应答,然后发送所有数据
			COM_SendAll();
			COM_Send(0xff);
			Com_flag=0;						
		}
		else if (Com_buff[Com_count]=='I')					//若接收到'I',表示从PC端接收打铃时间表并刷新内存数据
		{
			COM_Send('R');						//先发送一个'R'应答,然后设置串口状态二,接受数据
			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,"现在温度");
	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,"现在温度");
			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);		//清屏,字符缓冲区
				delay_ms(2);
				LCD_clr_pic();					//清屏,图形缓冲区
				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');
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -