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

📄 16f877_modbus.c

📁 ModBus通讯
💻 C
📖 第 1 页 / 共 5 页
字号:
  writee(0xbb,210);  
}
/*******************************************************************************
* Function Name  : rom_toram
* Description    : rom_toram
* Input          : the range of rom to ram total bytes number
* Output         : None
* Return         : None
*******************************************************************************/
void rom_toram(uchar u)  
{
  uchar i;
  for(i=0;i<u;i++){       
      x_number[i]=pre_number[i];
      /* to prevent the user's password when reset the equipment */
      // if(i==25)   x_number[25]=88; 
  }

}
/*******************************************************************************
* Function Name  : eecrc
* Description    : check the E2PROM,if two memory block the same,then change the
*                  third one,else write the three memory blocks read from ROM
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void eecrc(void)
{
  uchar aaa,bbb,ccc,i;
  for(i=0;i<52;i++){
    aaa=readee(i);
    bbb=readee(i+52);
    if(aaa!=bbb){
       ccc=readee(i+104);
       if(aaa==ccc)writee(ccc,i+52);        
       else 
         if(bbb==ccc) writee(ccc,i);      
         else rom_toee();                     
       ee_toram();                                      
     }
  }
}


/*******************************************************************************
* Function Name  : GlobalInit
* Description    : GlobalInit
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GlobalInit(void)
{  
 GIE=0;
 TRISA=0xFF;
 /* 1110 0010 , rb2 nouse define out */
 TRISB=0xE2;
 TRISC=0x83;    
 TRISD=0xc0;
 PORTD=0X00;
 PORTB=0x1e;
 // relay_jcq=1;    // 0 close , 1 open
 // relay_ba=0; 
 // relay_tx=1;
 RC2=1;
 /* RBIE=1 */
 INTCON=0xc8;  
 /* TMR1 set */
 TMR1IE=1;
 TMR1IF=0;  
 T1CON=0X81;
 TMR1L=T1LValue;
 TMR1H=T1HValue;
 /* WDT set */
 SWDTEN=1;
 /* the ADC is 32 prescale,and the 7th channel,and open the adc */
 ADCON0=0xb9;
 /* right justified,IO is digital mode */
 ADCON1=0x86;
#ifdef _40M
 ADCS2=1;
#endif
 /* set the duty cycle is 1/2 */
 CCPR1L=0X7F;
 PR2=0XFF;   
 /* select the PWM mode */
 CCP1CON=0X3C; 

 /* if 40M, T2CON=0x0a; */
 /* start the timer2,and set the prescale is 2 if 0.1u every instruction */ 
 T2CON=0X04;    

 SPEN=1;
 TXIF=0;
 RCIF=0;
 TXIE=0;
 RCIE=0;
 TX9=0;
 RX9=0;

//pid_plc=0;
//pid_u=0;
//pid_i=0;
//pid_flag=0;
//pid_key=0;
 
//gya_t=0;
//qya_t=0;
 GIE=1;
} 
/*******************************************************************************
* Function Name  : LcdInit
* Description    : LcdInit
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LcdInit(void)
{
   /* the function set */
   WriteCommand(0x30);     
   delay(200);
   /* fan bai is on */
   WriteCommand(0x09);     
   delay(200);
   /* clear the display */
   WriteCommand(0x01);     
   delay(200);
   /* the direction of the cursor is increase */
   WriteCommand(0x06);     
   delay(200);
   /* open the display */
   WriteCommand(0x0c); 
   delay(200); 
}

/*******************************************************************************
* Function Name  : LcdInit
* Description    : LcdInit
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void delay(uint t)
{
 uint m,n;
 for(m=0;m<t;m++)
    {
    for(n=0;n<2;n++);
     CLRWDT;
    }
 }
/*******************************************************************************
* Function Name  : WriteByte
* Description    : WriteByte for Lcd
* Input          : the value want to write
* Output         : None
* Return         : None
*******************************************************************************/
void WriteByte(uchar value)
{
   uchar i;
   uchar t;
   SCK=0;
   for(i=0;i<8;i++){
       if((value&0x80)==0) SD=0;
       else 
         SD=1;
         t=0;
         t=1;
         t=0;
         t=1;
       SCK=1;
         t=0;
         t=1;
         t=0;
         t=1;
         t=0;
         t=1;
       value=value<<1;
       SCK=0;
   }
}
/*******************************************************************************
* Function Name  : WriteCommand
* Description    : WriteCommand
* Input          : the value include command and data
* Output         : None
* Return         : None
*******************************************************************************/
void WriteCommand(uchar value)
{
   uchar hdata;
   uchar ldata;   
   hdata=value&0xf0; 
   ldata=value&0x0f;
   ldata=ldata<<4;
   WriteByte(0xf8);
   WriteByte(hdata);
   WriteByte(ldata);
}
/*******************************************************************************
* Function Name  : WriteData
* Description    : WriteData
* Input          : the value of data
* Output         : None
* Return         : None
*******************************************************************************/
void WriteData(uchar value)
{
   uchar hdata;
   uchar ldata;   
   hdata=value&0xf0;
   ldata=value&0x0f;
   ldata=ldata<<4;
   WriteByte(0xfa); 
   WriteByte(hdata);
   WriteByte(ldata);
}
/*******************************************************************************
* Function Name  : NumberNoPoint
* Description    : NumberNoPoint
* Input          : the input number
* Output         : None
* Return         : None
*******************************************************************************/
void NumberNoPoint(uint number)
{ 
 if(number<1000)num_xi[0]=0xa0;
 else num_xi[0]=(number/1000+0xb0);

 if(number<100)num_xi[1]=0xa0;
 else num_xi[1]=((number%1000)/100+0xb0);
 if(number<10)num_xi[2]=0xa0;
 else num_xi[2]=((number%100)/10+0xb0);
 num_xi[3]=number%10+0xb0;             
}
/*******************************************************************************
* Function Name  : KeyJudge
* Description    : KeyJudge
* Input          : KeyScanFlag
* Output         : KeyPress
* Return         : None
*******************************************************************************/
void KeyScan(void)
{
   /* if pid is runing can,t set menu */
   KeyPress=0;  
   if(KeyScanFlag==0)return;
   KeyScanFlag=0;
   
   if((PORTA&KEYMASK)==KEYMASK){
     KeyScanCnt=0;
     KeyValueBuf=0x07;
     return;
   }
   
   if(KeyValueBuf !=(PORTA&KEYMASK)){
     KeyValueBuf=(PORTA&KEYMASK);
     KeyScanCnt=0;
     return;
   }
   
   KeyScanCnt++;
   if(KeyScanCnt==3){
     KeyScanCnt=6;
     KeyValue=KeyValueBuf;
     KeyPress=1;
     return;
   }
   
   if(KeyScanCnt==46){
     KeyScanCnt=6;
     KeyPress=1;
   }
 }
/*******************************************************************************
* Function Name  : KeyFunction
* Description    : KeyFunction
* Input          : 
* Output         : None
* Return         : None
*******************************************************************************/
void KeyFunction(void)
{ 
  if(KeyPress==0)return;
  KeyPress=0;
 
    if(KeyValue==0x01){
      if((MainSlave&0x01)==0x01){
        /* reset now */
        asm("RESET");
      }
      else{
        if(State==0){
          asm("RESET");
        }
        if(State==1){
           xx1:
          data_buf[0]=1;
          data_buf[1]=6;
          data_buf[2]=0;
          data_buf[3]=0x1f;
          data_buf[4]=0;
          data_buf[5]=1;
          UsartSendMain(6);
          while(MainSendNoack==0){
            UsartReceveSlave();
            if(rece_ok==1){
              rece_ok=0;
              if(data_buf[1]==0x86)goto xx1;
              return;
            }
          }
          MainSendNoack=0;
          LcdErro=1; 
          LcdSelectBrush(line3,20);
          LcdFlag=1;
          LcdScanTime=100;
          return;
        }
        if(State==2){
          xx2:
          data_buf[0]=2;
          data_buf[1]=6;
          data_buf[2]=0;
          data_buf[3]=0x1f;
          data_buf[4]=0;
          data_buf[5]=1;
          UsartSendMain(6);
          while(MainSendNoack==0){
            UsartReceveSlave();
            if(rece_ok==1){
              rece_ok=0;
              if(data_buf[1]==0x86)goto xx2;
              return;
            }
          }
          MainSendNoack=0;
          LcdErro=1; 
          LcdSelectBrush(line3,20);
          LcdFlag=1;
          LcdScanTime=100;
          return;
        }
        if(State==3){
          xx3:
          data_buf[0]=3;
          data_buf[1]=6;
          data_buf[2]=0;
          data_buf[3]=0x1f;
          data_buf[4]=0;
          data_buf[5]=1;
        
          UsartSendMain(6);
          while(MainSendNoack==0){
            UsartReceveSlave();
            if(rece_ok==1){
              rece_ok=0;
              if(data_buf[1]==0x86)goto xx3;
              return;
            }
          }
          MainSendNoack=0;
          LcdErro=1; 
          LcdSelectBrush(line3,20);
          LcdFlag=1;
          LcdScanTime=100;
          return;
        }
      }
    }
    if(KeyValue==0x04){
      /* stop */
      Tree=0;
      PidStartFlag=0;
      LcdFlag=1;
      LcdScanTime=100;
      return;
    } 
  
  LcdFlag=1;
  LcdScanTime=100;
  if(KeyValue==0x01){
    /* reset now */
    asm("RESET");
  }
  if(KeyValue==0x04){
    /* stop */
    Tree=0;
    PidStartFlag=0;
    return;
  }  
  //if(PidStartFlag != 0)return;
  switch(Tree){
     case 0:{
       switch(KeyValue&0x07){
         case 0x06:{
           /* pid start */
           if((MainSlave&0x01)==0x00){
             PidStartFlag=1;
           }
           return;
         }
         case 0x03:{
           Tree=1;
           return;
         }
         default:return;
       }
     }
     case 1:{
       switch(KeyValue&0x07){
         case 6:{
           Menu++;
           if(Menu==3)Menu=0;
           return;
         }
         case 3:{
           if(Menu==0)Menu=3;
           Menu--;
           return;
         }
         case 5:{
           switch(Menu){
             case 0:{
               XValue=0;
               Tree=2;
               return;
             }
             case 1:{
               XValue=0x0d;
               Tree=2;
               return;
             }
             case 2:{
               Menu=0;
               Tree=0;
               point=x_number;
               return;
             } 
             default:{
               Menu=0;
               Tree=0;
               return;
             }
           }          
         }  
       } 
     } 
     case 2:{
       switch(KeyValue&0x07){

⌨️ 快捷键说明

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