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

📄 main.c

📁 在飞思卡尔MC9S08GB60单片机环境下实现了无线监控
💻 C
📖 第 1 页 / 共 3 页
字号:



/******************************************************************************
*	Function: 	Flash2IsFull
*	Parameters: 判断报警信息有没有超过规定的条数N
*	Return:		none
*	Actions:  超过60,提醒界面
*******************************************************************************/

void Flash2IsFull(){
  if(p2_rom >= (char*)0x13A0){ //报警记录超过60条,提醒
    
  } 
  else  return;
 }


/*****************************************************************************
*	Function: 	WriteAlarm
*	Parameters: DeviceID是报警的子机号码。Type是报警类型。
*	Return:		none
*	Actions:	将报警信息写进flash. 部分第二页flash,0x1210~0x13ff,共496bytes,最多62条
******************************************************************************/

void WriteAlarm(UINT8 DeviceID,UINT8 Type) {
    
     UINT8 Length1 = 0;     
     UINT8 Length2 = 0;
     UINT16 Length3 = 0; 
     UINT16 End;
     UINT16 p2_temp = 0x1200;
     DisableInterrupts;
     
     //从0x1200开始
     //start指针从1200开始到1201
     //end指针从1202到1203

     //第一个报警信息从0x1210开始到0x1217
     //第二个报警信息从0x1218开始到0x121F
     //倒数第二个报警信息从0x12F0开始到0x12F7
     //倒数第一个报警信息从0x12F8开始到0x12FF

     p2_rom =	(UINT8 *)(p2_temp + 0x0002);
     
		 if((*p2_rom	== 0xFF)&&(*(++p2_rom) == 0xFF))  //Flash中没有信息进这里
		   {		 
		 	 	 FlashUnion.flash2.pEnd_Buff[0]	 =0x12;
		 	 	 FlashUnion.flash2.pEnd_Buff[1]  =0x10;
		 	 	 FlashArray(&FlashUnion.flash2.pEnd_Buff[0],2,(UINT16)0x1202);
		 	 	 EnableInterrupts;
		   } 
		 else if(Type != 0)
		   {
     
		 p2_rom = (UINT8 *)(p2_temp + 0x0002);
     while(Length2 < 2)
      {
         FlashUnion.flash2.pEnd_Buff[Length2] = *p2_rom;
         Length2++;
         p2_rom++;
      }
      
     p2_rom = (UINT8 *)(p2_temp + 0x0010);
     while(Length3 < 496)
      {
         FlashUnion.flash2.AlarmInfo_Buff[Length3] = *p2_rom;
         Length3++;
         p2_rom++;
      }
		 
		 End  = (((FlashUnion.flash2.pEnd_Buff[0]<<8)+FlashUnion.flash2.pEnd_Buff[1]) -0x1210);

   /*  
    Slave_Alarm_Info.SlaveID = DeviceID;
    Slave_Alarm_Info.AlarmType = Type;
    Slave_Alarm_Info.year = Date_Time_Buff.year;
    Slave_Alarm_Info.month = Date_Time_Buff.month;
    Slave_Alarm_Info.day = Date_Time_Buff.day;
    Slave_Alarm_Info.hour = Date_Time_Buff.hour;
    Slave_Alarm_Info.minute = Date_Time_Buff.minute;   
    //写flash

    FlashArray(&Slave_Alarm_Info.SlaveID,7,(UINT16)p2_rom_z);
    p2_rom_z = p2_rom_z + 8; */
    
    //Feed_SoftDog();					//喂软狗
    
    if(End >=496) 
      {
         End = 0;
         Length3 = 0;
         while(Length3 < 496)
           {
             FlashUnion.flash2.AlarmInfo_Buff[Length3] = 0xFF;
             Length3++;
           }
         FlashUnion.flash2.pEnd_Buff[1] = 0x10;
         FlashUnion.flash2.pEnd_Buff[0] = 0x12;  
      }
    
    FlashUnion.flash2.AlarmInfo_Buff[End]     = DeviceID;
    FlashUnion.flash2.AlarmInfo_Buff[End + 1] = Type;
    FlashUnion.flash2.AlarmInfo_Buff[End + 2] = Date_Time_Buff.year;
    FlashUnion.flash2.AlarmInfo_Buff[End + 3] = Date_Time_Buff.month;
    FlashUnion.flash2.AlarmInfo_Buff[End + 4] = Date_Time_Buff.day;
    FlashUnion.flash2.AlarmInfo_Buff[End + 5] = Date_Time_Buff.hour;
    FlashUnion.flash2.AlarmInfo_Buff[End + 6] = Date_Time_Buff.minute;  
    FlashUnion.flash2.AlarmInfo_Buff[End + 7] = 0x01;
    
    if(FlashUnion.flash2.pEnd_Buff[1] < 0xF8)
      {      
        FlashUnion.flash2.pEnd_Buff[1]  = FlashUnion.flash2.pEnd_Buff[1]+8;
      } 
    else
      {
        FlashUnion.flash2.pEnd_Buff[1]  = 0x00;
        FlashUnion.flash2.pEnd_Buff[0]  = FlashUnion.flash2.pEnd_Buff[0]+0x01;
      }
		EraseFlash(2);
    p2_rom = (UINT8 *)(p2_temp + 0x0002);   //写报警信息结束指针
    FlashArray(FlashUnion.flash2.pEnd_Buff,2,(UINT16)p2_rom);
    p2_rom = (UINT8 *)(p2_temp + 0x0010);	 //写报警信息
    FlashArray(FlashUnion.flash2.AlarmInfo_Buff,496,(UINT16)p2_rom);
    
    EnableInterrupts; 
    
  }
}


/***********************************************************************
*	CheckMeOut: 	MCU Check Me Out Handler
*	Parameters: none
*	Return:		none
*	Actions:	Check the MCU Flash OUT
*           读取MCUFlash中所具有的数据:第一页的子机ID,第三页的电话号码
************************************************************************/
void CheckMeOut()			//当座机掉电之后,上电之后用于检验有多少个子机已经加入系统
{										 	//并且知道相应子机的地址

	  	 
}  


/*************************************************************
*检查子机接收到信息的XOR是否正确
*************************************************************/

char CheckXor(UINT8 ByLen,UINT8 *lpdata){
  UINT8 xor_s = 0;
  int i=0;
  for (i = 0;i<ByLen;i++){
    xor_s = lpdata[i]^xor_s;
  }
  if (xor_s == 0)
    return 1;
  else
    return 0;
}


/*****************************************************************************
*	Interrupt: 	Timer2 interrupt handler
*	Parameters: none
*	Return:		none
*	Actions:	Enable the timer interrupt
*           响应时间中断,1.0s一次。用作时间计时。
*           每次响应,就改变时间变量。
*           并且检测现在有没有显示时间的界面,有就显示新时间。
******************************************************************************/

interrupt void VTimer2(void){
 //  UINT8  i = 0; 
   UINT8  year;
   UINT8  month;
   UINT8  day;
   UINT8  hour;
   UINT8  minute;
   UINT8  FebDay;	 //二月是几天
   UINT8  i=0;
   UINT16  finishtime=320;
  // PTAD ^= 0xFE;
   //关中断
   TPM2SC_TOF = 0;  // clear timer2 overflow flag.
   TPM2SC_TOIE = 0; // disable timer2 overflow flag.
         
 	 //每次响应timer_count2加1
 	 timer_count2++;
 	 
 	 
 	 //计算时间
 	 if(timer_count2 == 64){  //64.0007的时候是准确的1分钟
 	   timer_count2 = 0;
 	      
 	   //时间处理
 	   year =  Date_Time_Buff.year;
     month =  Date_Time_Buff.month;
     day =  Date_Time_Buff.day;
     hour =  Date_Time_Buff.hour;
     minute =  Date_Time_Buff.minute;
         
   	 //判断是不是闰年,是不是闰年二月
   	 FebDay = Feb(year);
 	          	      
 	   //修改时间
 	   minute++; //60s,分钟加1
 	   if(minute == 60){
 	     minute = 0;
 	     hour++; //60m,小时加1
 	     if(hour == 24){ //24h,天加1
         hour = 0;
 	       switch(month){
 	         case 1:
 	           if(day == 31){
 	             day = 1;	 //转为1号
 	             month = 2;//转为2月
 	           }else{
 	             day++;
 	           }
 	         break;
 	         case 2:
 	           if(day == FebDay){
 	             day = 1;	 //转为1号
 	             month = 3;//转为3月
 	           }else{
 	              day++;
 	           }
 	         break;
 	         case 3:
 	           if(day == 31){
 	             day = 1;	 //转为1号
 	             month = 4;//转为4月
 	           }else{
 	              day++;
 	           }
 	         break;
 	         case 4:
 	           if(day == 30){
 	             day = 1;	 //转为1号
 	             month = 5;//转为5月
 	           }else{
 	              day++;
 	           }
 	         break;
 	         case 5:
 	           if(day == 31){
 	             day = 1;	 //转为1号
 	             month = 6;//转为6月
 	           }else{
 	              day++;
 	           }
 	         break;
 	         case 6:
 	           if(day == 30){
 	             day = 1;	 //转为1号
 	             month = 7;//转为7月
 	           }else{
 	              day++;
 	           }
 	         break;
 	         case 7:
 	           if(day == 31){
 	             day = 1;	 //转为1号
 	             month = 8;//转为8月
 	           }else{
 	              day++;
 	           }
 	         break;
 	         case 8:
 	           if(day == 31){
 	             day = 1;	 //转为1号
 	             month = 9;//转为9月
 	           }else{
 	              day++;
 	           }
 	         break;
 	         case 9:
 	           if(day == 30){
 	             day = 1;	 //转为1号
 	             month = 10;//转为10月
 	           }else{
 	              day++;
 	           }
 	         break;
 	         case 10:
 	           if(day == 31){
 	             day = 1;	 //转为1号
 	             month = 11;//转为11月
 	           }else{
 	              day++;
 	           }
 	         break;
 	         case 11:
 	           if(day == 30){
 	             day = 1;	 //转为1号
 	             month = 12;//转为12月
 	           }else{
 	              day++;
 	           }
 	         break;
 	         case 12:
 	           if(day == 31){
 	             day = 1;	 //转为1号
 	             month = 1;//转为1月
 	             year++;	 //年份加1
 	           }else{
 	              day++;
 	           }
 	         break;
 	                     
 	         default:
 	         break;
 	       }
 	     }
 	   }
 	   
 	   //change Time
 	   Date_Time_Buff.year = year;
     Date_Time_Buff.month = month;
     Date_Time_Buff.day = day;
     Date_Time_Buff.hour = hour;
     Date_Time_Buff.minute = minute;
 	 }
 	 
 	 /**************************************/
 	 /****报警结束计时**********************/
 	 for(i=0;i<SlaveNumber;i++){
 	   if(Slave_Ram_Buff[i].lost_finishtime==finishtime){
 	     Slave_Ram_Buff[i].lost_finishflag=0;//计5分钟,将标志位置为0
 	     Slave_Ram_Buff[i].lost_finishtime=0;
 	   }
 	   if(Slave_Ram_Buff[i].lost_finishflag==1){
 	    Slave_Ram_Buff[i].lost_finishtime++; 	  
 	   }
 	   if(Slave_Ram_Buff[i].low_batt_finishtime==finishtime){
 	     Slave_Ram_Buff[i].low_batt_finishflag=0;//计5分钟,将标志位置为0
 	     Slave_Ram_Buff[i].low_batt_finishtime=0;
 	   }
 	   if(Slave_Ram_Buff[i].low_batt_finishflag==1){
 	    Slave_Ram_Buff[i].low_batt_finishtime++; 	  
 	   }
 	   if(Slave_Ram_Buff[i].move_finishtime==finishtime){
 	     Slave_Ram_Buff[i].move_finishflag=0;//计5分钟,将标志位置为0
 	     Slave_Ram_Buff[i].move_finishtime=0;
 	   }
 	   if(Slave_Ram_Buff[i].move_finishflag==1){
 	    Slave_Ram_Buff[i].move_finishtime++; 	  
 	   }
 	   if(commu_error_finishtime==finishtime){
 	     commu_error_finishflag=0;
 	     commu_error_finishtime=0;
 	   }
 	   if(commu_error_finishflag==1){
 	     commu_error_finishtime++;
 	   } 
 	   if(Master_Ram_Buff.mspower_finishtime==finishtime){
 	     IRQSC = 0x13; //过五分钟才开中断
 	     Master_Ram_Buff.mspower_finishflag=0;
 	     Master_Ram_Buff.mspower_finishtime=0;
 	   }
 	   if(Master_Ram_Buff.mspower_finishflag==1){
 	     Master_Ram_Buff.mspower_finishtime++;
 	   }
 	 }
 	 
 	  /***********************************/
    /***preparing计时****************/
    if(RingTimeCount==18){
      RingTime=1;
      RingTimeCount=0;
      RingTimeFlag=0;
    }
    if(RingTimeFlag ==1){
      RingTimeCount++;
    }
    /**********************************/
   //开中断
   TPM2SC = 0x4F;
   TPM2SC_TOIE = 1; 	 
}

interrupt void IRQ(void)
 {
 		IRQSC = 0x05;
 		MasterPower_Flag = 1;
 		//IRQSC = 0x13;
 }

/***************************************************************
*	Function: 	Feb
*	Parameters: year是当年年份,只输入后两位
*	Return:		二月的天数
*	Actions: 计算当年是否闰年,得出当年的二月天数
***************************************************************/

UINT8 Feb(UINT8 year_buff)
{
  UINT8 FebDay;
  UINT16 year;
   
  year = (UINT8) year_buff;
   
  //判断是不是闰年,是不是闰年二月
  //因为存的只是年份的后两位,所以这里要加上2000
  year = year + 2000;
  if(year%400==0 || (year%4 == 0 &&year%100 != 0)){
    FebDay = 29;
  }else{
     FebDay = 28;
  }
  return FebDay;
} 		



/*****************************************************************************
*	Function: 	WriteConfig
*	Parameters: pTelNum是载荷起始地址

⌨️ 快捷键说明

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