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

📄 3d can例程.c

📁 ControlCAN 开发程序例程1
💻 C
📖 第 1 页 / 共 2 页
字号:
		    DADAT = 0;
		else
		    DADAT = 1;
		DACLK = 0;
		ndata1 = ndata1 << 1;
        DACLK = 1;
    }
	DACS = 1;
	DACLK = 0;
}

//错误处理及状态监测
extern uchar Erro_can ( uchar can_ero )
{
    can_ero = StatusReg ;
    if ( can_ero & BS_Bit == 0x80 )
	{
        can_ero = 0xe8 ;    //总线在关闭状态
    }
	if ( can_ero & ES_Bit == 0x40 )
	{
        can_ero = 0xe4 ;    //错误计数 CAN 2.0A 不用,  接收程序中已处理
    }
	if ( can_ero & TS_Bit == 0x20 )
	{
        can_ero = 0xe2 ;    
    }
	if ( can_ero & RS_Bit == 0x10 )
	{
        can_ero = 0xe1 ;    
    }
	if ( can_ero & TCS_Bit == 0x08 )
	{
        can_ero = 0xe7 ;    
    }
	if ( can_ero & TBS_Bit == 0x04 )
	{
        can_ero = 0xe5 ;    
    }
	if ( can_ero & DOS_Bit == 0x02 )
	{
        can_ero = 0xe3 ;    
    }
	return ( can_ero ) ;
}
/*******************************************************************************/
 void main ( void )
 {
        AUXR = 0x02 ;       
        WDT_CONTR = 0x37 ;   

        EA = False ;

        dbt0 = DBT0 ;
        dbt1 = DBT1 ;

        init ( ) ;

        TMOD = 0x11 ;

        TH0 = 0xf8 ;
        TL0 = 0xcd ;  
        clrwdt ( ) ;
        IT0 = False ;  
        ET0 = True ;
        TR0 = True ;
        
        PT1 = True  ; 
		PX1 = False ;  
        PT0 = False ;  
        PX0 = False ;   
		PS = False  ;   

		TH1 = 0x00 ;
        TL1 = 0x00 ;  
        EX1 = False; 
	    ET1 = False;
        TR1 = True ;
        EA  = True ;
        
        runcount = 0x00 ;
		ResetCAN = 0 ;    
		while ( runcount < 11 ) clrwdt ( ) ;
		ResetCAN = 1 ;    
        runcount = 0x00 ;
		while ( runcount < 11 ) clrwdt ( ) ;


        init_can ( ) ;	  
        EX0 = True ;

        Sending = False ;
		Rcv_Head_Flag0 = False ;   

        WDG=0;WDG=1; 
        WDT_CONTR = 0x37 ;   
        /*********************************************************************/

		//for( XDAVLM = 0; XDAVLM < 32768 ; XDAVLM++)
		//{
        //  DAT_FX[ XDAVLM ] = 0x11;
        //}
        //DAT_FX[ 32767 ] = 0xff ;
        //txdbuff [ 0x00 ]=0x25 ; 
        //txdbuff [ 0x01 ]=0x02 ;
        //txdbuff [ 0x02 ]=DAT_FX[ 0 ] ;
        //txdbuff [ 0x03 ]=DAT_FX[ 32767 ] ;
		//SendTT_CAN ( ) ;      

        //txdbuff [ 0x00 ]=0x25 ; 
        //txdbuff [ 0x01 ]=0x02 ;
        //txdbuff [ 0x02 ]=DAT_FX[ 256 ] ;
        //txdbuff [ 0x03 ]=DAT_FX[ 16384 ] ;
   		//SendTT_CAN ( ) ;      

        /*********************************************************************/

        while ( True )
        {
            WDG=0;WDG=1;        
            WDT_CONTR = 0x37 ;   

            Erro_can (0xff) ;
            if ( Erro_can == 0xff )
			{
                
			}
            if ( RXDbuf_Er )  
			{
			    //init ( ) ;        
			    //init_can ( ) ;
			}
			if ( RXDbuf_Er ) 
 	        {    
			    //txdbuff [ 0x00 ]=0x25 ;
	            //txdbuff [ 0x01 ]=0x01 ;
				//txdbuff [ 0x02 ]=0xEE ; 
                //Send_CAN ();

            }

            /******************************************************************/
  
            //LEDCH3 = !DI1 ;
            //LEDCH2 = !DI3 ;
            //if ( DI2 == 0) LEDCH3 = !DI2 ;

            //if ( Rcv_Head_Flag3 )   
			//{
		        //dacs01 = 0x800 ;
			    //dacs02 = 0xfff ;

				//dacs01 = rxdbuff [ 0x04 ] ;
                //dacs01 = dacs01 << 8 ;
                //dacs01 = dacs01 + rxdbuff [ 0x05 ] ;
                //dacs02 = dacs01 / 2 ;
                //writeda(dacs01,dacs02);

				//ch = 0 ;  //AIN0
			    //ch2 = ch;
                //for(i=0;i<3;i++)
                //{
		 		//   advalue[ch2] = readad(ch) ;  
                //}
				//txdbuff [ 0x00 ]=0x25 ;
                //txdbuff [ 0x01 ]=0x02 ;
				//dacsvl = advalue[ch2] ;
				//dacsvl = dacsvl >> 8 ;
                //txdbuff [ 0x02 ]=  dacsvl ;
                //txdbuff [ 0x03 ] = advalue[ch2] ;

                //DELAYFOR(80) ;
                //SendTT_CAN ( ) ;

                //ch = 1 ;   //AIN1
			    //ch2 = ch;
                //for(i=0;i<3;i++)
                //{
		 		//   advalue[ch2] = readad(ch) ;  
                //}
				//txdbuff [ 0x00 ]=0x25 ;
                //txdbuff [ 0x01 ]=0x02 ;
				//dacsvl = advalue[ch2] ;
				//dacsvl = dacsvl >> 8 ;
                //txdbuff [ 0x02 ]=  dacsvl ;
                //txdbuff [ 0x03 ] = advalue[ch2] ;

                //DELAYFOR(80) ;
                //SendTT_CAN ( ) ;
 
				//ch = 2 ;   //AIN2
			    //ch2 = ch;
                //for(i=0;i<3;i++)
                //{
		 		//   advalue[ch2] = readad(ch) ; 
                //}
				//txdbuff [ 0x00 ]=0x25 ;
                //txdbuff [ 0x01 ]=0x02 ;
				//dacsvl = advalue[ch2] ;
				//dacsvl = dacsvl >> 8 ;
                //txdbuff [ 0x02 ]=  dacsvl ;
                //txdbuff [ 0x03 ] = advalue[ch2] ;

                //DELAYFOR(80) ;
                //SendTT_CAN ( ) ;

                //ch = 3 ;   //AIN3
			    //ch2 = ch;
                //for(i=0;i<3;i++)
                //{
		 		//   advalue[ch2] = readad(ch) ; 
                //}
				//txdbuff [ 0x00 ]=0x25 ;
                //txdbuff [ 0x01 ]=0x02 ;
				//dacsvl = advalue[ch2] ;
				//dacsvl = dacsvl >> 8 ;
                //txdbuff [ 0x02 ]=  dacsvl ;
                //txdbuff [ 0x03 ] = advalue[ch2] ;

                //DELAYFOR(80) ;
                //SendTT_CAN ( ) ;

				//Rcv_Head_Flag3 = False ;
			//}
            /******************************************************************/

			if ( DI0 == 0 )       
            {
			    DELAYFOR(1);      
                if ( DI0 == 0 )
		        {
			      Rcv_Head_Flag1 = True ;
				}
            }
			else
			{
                 Rcv_Head_Flag1 = False ;
            }
		    
            if ( Contrtimer > 19 )      
            {
			     Contrtimer = 0 ;
                 if ( Rcv_Head_Flag1 ) 
                 {
                    txdbuff [ 0x00 ]=0x25 ;
	                txdbuff [ 0x01 ]=0x01 ;
	                txdbuff [ 0x02 ]=0x80 ;
                    Send_CAN ();
  			     }
				 else
				 {
                    txdbuff [ 0x00 ]=0x25 ;
	                txdbuff [ 0x01 ]=0x01 ;
	                txdbuff [ 0x02 ]=0x00 ;
                    Send_CAN ();
				 }
            }
	        
            if ( ZSGcount >2000 )
			{
			       LEDCH2 = 0 ;              

				   ET1 = False ;              
                   TR1 = False ;              
				   F_outfpot = False ;       
				   RUNLED = 1 ;               
                   DO0 = 0  ;                
	               DO1 = 0  ;                
			}

            if ( FKYcount >2000 )
			{
			       LEDCH3 = 0 ;                 
                   
				   dacs01 = 0x000 ;             
			       dacs02 = 0x000 ;
                   writeda ( dacs01,dacs02 ) ;

			}

            /******************************************************************************************/
			//if ( Rcv_Head_Flag2 != Rcv_Head_Flag1 )     
            //{
			//    Rcv_Head_Flag2 = Rcv_Head_Flag1 ;
			//    Rcv_Head_Flag0 = True ;
			//}
            
		    //    if ( DI0 == 0 ) berkControl_b32 = False ;
	        //    if ( DI0 == 1 ) berkControl_b32 = True  ;

            //    if ( Rcv_Head_Flag0 )  // Send 3
            //       {
	        //          if ( berkControl_B7   ) berkControl =  0x80 ;     
	        //          if ( ! berkControl_B7 ) berkControl =  0x00 ;     
            //          berkControl_B7 = ! berkControl_B7 ;
     	    //          if (  berkControl_b32 )  berkControl_P = 0x08 ;   
	        //          if ( !berkControl_b32 )  berkControl_P = 0x04 ;   
            
            //          DELAYFOR(10);

      	    //          txdbuff [ 0x00 ]=0x25 ;
	        //          txdbuff [ 0x01 ]=0x01 ;
	        //          txdbuff [ 0x02 ]=berkControl | 0x10 | berkControl_P | 0x03 ;
            //          Send_CAN ();

  	        //          DELAYFOR(30);

            //          txdbuff [ 0x00 ]=0x25 ;
	        //          txdbuff [ 0x01 ]=0x01 ;
	        //          txdbuff [ 0x02 ]=berkControl | 0x20 | berkControl_P | 0x03 ;
            //          Send_CAN ();

  			//          DELAYFOR(30);

            //          txdbuff [ 0x00 ]=0x25 ;
	        //          txdbuff [ 0x01 ]=0x01 ;
	        //          txdbuff [ 0x02 ]=berkControl | 0x30 | berkControl_P | 0x03 ;
            //          Send_CAN ();
			  
            //          Rcv_Head_Flag0 = False ;

            //        }
            /******************************************************************************************/
			
            if ( Sending )
            {

			     if ( Rcv_Head_Flag4 )              
                 {
 
						ZSGcount =0 ;                
						LEDCH2 =  1 ;                
                		DO2 = 0     ;                

	            		zhuanS = rxdbuff [ 0x03 ] ;
                		zhuanS = zhuanS << 8 ;
                		zhuanS = zhuanS +rxdbuff [ 0x02 ] ;
                		zhuanS = zhuanS + 0 ;                  
                
                		TPtimer = fout_t ( zhuanS ) ;              

                		if ( TPtimer < 35746 )                    
                		{
                    		TPAtimer = fout_tt ( TPtimer ) - 5 ;  
                    		inth_h= TPAtimer/256 ;
                    		inth_l= TPAtimer%256  ;
                   	        ET1 = True ;
							TR1 = True ;                         
                    		F_outfpot = False ;
                 		}
                 		if ( TPtimer > 35745 )                    
                 		{
                      		TPAtimer = TPtimer/1000 ;             
                      		ET1 = False ;
                      		TR1 = False ;                        
					  		F_outfpot = True ;
                  		}

                        Rcv_Head_Flag4 = False ;                  
                        
			     } //Rcv_Head_Flag4

		         if ( Rcv_Head_Flag3 )                 
                 {
                       
                       FKYcount =0 ;                  
                       LEDCH3  = 1 ;                  
                	   DO3 =   0   ;   

				       dacs01 = rxdbuff [ 0x07 ] ;
                       dacs01 = dacs01 << 8 ;
                       dacs01 = dacs01 + rxdbuff [ 0x06 ] ;   

                       dacs02 = DA_out( dacs01 ) ;           

                       dacs01 = dacs02 ;                      
                       writeda ( dacs01,dacs02 ) ;

                       Rcv_Head_Flag3 = False ;       
                  }
			 
  	         } 
			   
             if ( F_outfpot )
			 {
                if ( TPAcount > TPAtimer - 2 )
                {
                   TPAcount = 0x00 ;
                   RUNLED = ! RUNLED ;
				   DO0 = ! DO0 ;      
	               DO1 = ! DO1 ;
                }
			 }
        }
 }

void ex0int ( void ) interrupt 0 using 1
{      
    EA = False ;               
     
    Sending = Receive ( ) ;    	  
	if ( Sending )
	{
        RXDbuf_Er = False ;    
    } 
    else                      
	{
	    RXDbuf_Er = True ;     
    }
	
	EA  = True ;   
 }
void TIME0INT ( void ) interrupt 1 using 2 
{
   
   TH0 = 0xf8 ; TL0 = 0xff ;  

   SendOverTime ++ ;  
   runcount ++ ;   
   TPAcount ++ ; 
   ZSGcount ++ ; 
   FKYcount ++ ;  
   Contrtimer ++ ;

}
void TIME1INT ( void ) interrupt 3 using 3 //
{
    RUNLED = ! RUNLED ;
    TH1 = inth_h ;
	TL1 =inth_l + 0x0d ;    
    DO0 = ! DO0 ;           
	DO1 = ! DO1 ;           
}

⌨️ 快捷键说明

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