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

📄 intspc3.c

📁 profibus通信协议
💻 C
字号:

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

	公司名称:	 扬州恒春电子
	模块名	:	 Profibus DP从站接口程序 
	创建人	:	                                          
	日期	:	 2007/03/01	                                         
	功能描述:   利用ATMEG64L对SPC3进行配置,实现Profibus通讯                                  
	其它说明:   编译环境为Keil uVision2                                            
	版本	:	 V1.0

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

#include "spc3dps2.h"
#include "iom64v.h"
#include <macros.h>					

/*----------------------------------------------------------------------*/
/*                       INTERNAL FUNCTIONS                             */
/*----------------------------------------------------------------------*/
//void address_data_function (void *, UBYTE);
void wd_dp_mode_timeout_function (void);
void global_ctrl_command_function (void);
void go_leave_data_ex_function (void);

/*----------------------------------------------------------------------*/
/*                       LITERALLIES                                    */
/*----------------------------------------------------------------------*/


//sbit 	run_led=P1^0;				//show SPC3'states

#define DPS_CFG_OK      0
#define DPS_CFG_FAULT   1 
#define DPS_CFG_UPDATE  2

#define   Clr_run_led()      PORTB&=~BIT(PB2)	 /* show SPC3'states				*/
#define   Set_run_led()      PORTB|=BIT(PB2)	 /* show SPC3'states				*/


/*----------------------------------------------------------------------*/
/*                       MACROS                                         */
/*----------------------------------------------------------------------*/

/*----------------------------------------------------------------------*/
/*                       GLOBAL VARIABLES                               */
/*----------------------------------------------------------------------*/

UBYTE                   user_dps_state;
UBYTE                   user_global_ctrl_command;
UBYTE                   user_wd_state;
UBYTE                   store_mintsdr;  



extern UBYTE             real_no_add_chg;
extern UBYTE             this_station;
extern UBYTE             cfg_akt [20];
extern UBYTE             cfg_len_akt;
extern DPS2_IO_DATA_LEN  *user_io_data_len_ptr;  /* Aenderung 14.11.95 */
extern UBYTE             prm_tst_buf[];
extern UBYTE             Isr_Flag0;
extern UBYTE             Isr_Flag1;

/************************************************************************/
/*  D e s c r i p t i o n :                                             */
/*                                                                      */
/*  dps2_ind                                                            */
/*                                                                      */
/*  This function is called by the hardware interrupt                   */
/************************************************************************/

#pragma interrupt_handler int4_isr:6

void int4_isr(void)   
{
   

if(!Isr_Flag1)  Isr_Flag1=1,   Clr_run_led();
else            Isr_Flag1=0,   Set_run_led();
	
	
if(DPS2_GET_IND_GO_LEAVE_DATA_EX())
//if (spc3.int_reg.com.b[_IML] & GO_LEAVE_DATA_EX_B)
	{   //=== Start or the end of the Data-Exchange-State ===//
	go_leave_data_ex_function();
	DPS2_CON_IND_GO_LEAVE_DATA_EX();    // confirm this indication //
	//spc3.int_reg.ack.b[_IML] = (UBYTE)GO_LEAVE_DATA_EX_B;
	}

if(DPS2_GET_IND_NEW_GC_COMMAND())
	{   //===  New Global Control Command ===//
	global_ctrl_command_function();
	DPS2_CON_IND_NEW_GC_COMMAND();  // confirm this indication //
	}

if(DPS2_GET_IND_NEW_PRM_DATA())
	{   //=== New parameter  data ===//
	UBYTE    * prm_ptr;
	UBYTE   param_data_len, prm_result;
	UBYTE   ii;

	prm_result = DPS2_PRM_FINISHED;
	do
		{ // Check parameter until no conflict behavior //
		prm_ptr = DPS2_GET_PRM_BUF_PTR();
		param_data_len = DPS2_GET_PRM_LEN();

		// data_length_netto of parametration_telegram > 7 //
		if (param_data_len > 7)
			{
			
			//if (( *(prm_ptr+8) == 0xAA)  && ( *(prm_ptr+9) == 0xAA))   //20080731
				prm_result = DPS2_SET_PRM_DATA_NOT_OK(); // as example !!! //
			//else               20080731
				//{              20080731
				//for (ii= 0; ii<param_data_len && ii <10; ii++)  // store in the interim buffer //
					//prm_tst_buf[ii] = *(prm_ptr+ii+7);          // for the diagnostic //
															// !!!!!! as example !!!! //

				//prm_result = DPS2_SET_PRM_DATA_OK();
				//}              20080731
			}
		else
			prm_result = DPS2_SET_PRM_DATA_OK();

		} while(prm_result == DPS2_PRM_CONFLICT);

	store_mintsdr =  *(prm_ptr+3);    // store the mintsdr for restart after //
									  // baudrate search //

	}

if(DPS2_GET_IND_NEW_CFG_DATA())
	{   //=== New Configuration data ===//
	UBYTE  * cfg_ptr;
	UBYTE config_data_len, cfg_result, result;

	cfg_result = DPS2_CFG_FINISHED;
	result = DPS_CFG_OK;

	do
		{   // check configuration data until no conflict behavior m//
		cfg_ptr = DPS2_GET_CFG_BUF_PTR();             // pointer to the config_data_block //
		config_data_len = DPS2_GET_CFG_LEN();

		// In this example the only possible configurations are 0x13 and 0x23
		 //  (4 Byte I/O) or 0x11 and 0x21 (2 Byte I/O) are possible 

		if ( config_data_len != 2)
			cfg_result = DPS2_SET_CFG_DATA_NOT_OK();
		else
			{   // Length of the configuration data  o.k. //
				// check the configuratin bytes           //

			if ((cfg_akt[0] == cfg_ptr[0]) && (cfg_akt[1] == cfg_ptr[1]))
				result = DPS_CFG_OK;
				// the desired conf. is equal the actuall configuration //
			else
				{
				if (((cfg_ptr[0] == 0x13) && (cfg_ptr[1]) ==0x23)
				  || ((cfg_ptr[0] == 0x11) && (cfg_ptr[1]) ==0x21))
					{
					cfg_akt[0] = cfg_ptr[0];
					cfg_akt[1] = cfg_ptr[1];
					result = DPS_CFG_UPDATE;
					}
				else
					result = DPS_CFG_FAULT;              // as example !!!!! //


				if (result == DPS_CFG_UPDATE)
					{
					user_io_data_len_ptr = dps2_calculate_inp_outp_len (cfg_ptr,(UWORD)config_data_len);
					if (user_io_data_len_ptr != (DPS2_IO_DATA_LEN *)0)
						{
						DPS2_SET_IO_DATA_LEN(user_io_data_len_ptr);
						}
					else
						result = DPS_CFG_FAULT;
					}
				}
			switch (result)
				{
				case DPS_CFG_OK: cfg_result = DPS2_SET_CFG_DATA_OK();
					break;

				case DPS_CFG_FAULT: cfg_result = DPS2_SET_CFG_DATA_NOT_OK();
					break;

				case DPS_CFG_UPDATE: cfg_result = DPS2_SET_CFG_DATA_UPDATE();
					break;
				}
			}

		} while(cfg_result == DPS2_CFG_CONFLICT);
	}


if(DPS2_GET_IND_WD_DP_MODE_TIMEOUT())
	{   //=== Watchdog is run out ===//
	wd_dp_mode_timeout_function();
	DPS2_CON_IND_WD_DP_MODE_TIMEOUT();  // confirm this indication //
	}
if(SPC3_GET_IND_USER_TIMER_CLOCK())
	{   //==== Timer tick received ====//
	SPC3_CON_IND_USER_TIMER_CLOCK();
	}

if(SPC3_GET_IND_BAUDRATE_DETECT())
	{   //==== Baudrate found  ====//

	// If the baudrate has lost and again found in the state WAIT_CFG,  //
	//  DATA_EX the SPC3 would answer to the next telegramms            //
	//  with his default mintsdr.                                       //
	// But he should answer in the meantime parametrized mindstr        //

	     if ((DPS2_GET_DP_STATE() ==  DPS2_DP_STATE_WAIT_CFG )||(DPS2_GET_DP_STATE()  ==  DPS2_DP_STATE_DATA_EX))
		 
		          SPC3_SET_MINTSDR(store_mintsdr);

	     SPC3_CON_IND_BAUDRATE_DETECT();
	  }
    SPC3_SET_EOI(); 


}   // End dps2_ind() //



/*
void address_data_function (void *(address_data_ptr), UBYTE address_data_len)
{
char	ch;

//change the slave-address 
struct dps_address_data  * addr_ptr;

ch=address_data_len;

addr_ptr = address_data_ptr;

this_station = addr_ptr->new_address;
real_no_add_chg = addr_ptr->no_add_chg;

// store the new address and the bit real_no_add_chg for the next startup 

DPS2_FREE_SSA_BUF(); // release the Set Slave Address buffer 
}
*/
	  		

void wd_dp_mode_timeout_function (void)
{
UBYTE rueck;
	/* Watchdog is run out. The SPC3 reset the PROFIBUS DP Statemachine */

user_wd_state = SPC3_GET_WD_STATE();

	/*SX    small weakness of the SPC3
			Update of the diagnosis with 00
			otherwise the bit "static diagnosis" would appear in the
			next startup */


rueck = DPS2_SET_DIAG_LEN(6);
rueck = DPS2_SET_DIAG_STATE(0x00);
DPS2_DIAG_UPDATE();


}



void global_ctrl_command_function (void)
{

user_global_ctrl_command = DPS2_GET_GC_COMMAND();

}



void go_leave_data_ex_function (void)
{

user_dps_state = DPS2_GET_DP_STATE();
}


⌨️ 快捷键说明

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