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

📄 userspc3.c

📁 从事Profibus的开发人员可以利用改源代码理解profibus协议。添加除通讯以外的代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Reset the User und DPS */
user_dps_reset();


/* Enable the SPC3 interrupt in the interupt controller of the microprocessor */
#if defined __C51__
  SPC3_INTERRUPT_ENABLE = I_ENABLE;
  GLOBAL_INTERRUPT_ENABLE = I_ENABLE;
#elif _C166
  CC11IE = 1;
#endif


for (;;)
	{   /*=== Begin  of the endless loop ===*/
	zyk_wd_state = SPC3_GET_WD_STATE();    /*for info.: the actuall WD State*/

	zyk_dps_state = DPS2_GET_DP_STATE();   /*for info.: the actuall PROFIBUS DP State*/


	DPS2_RESET_USER_WD();           /* Trigger the user watchdog of the SPC3 */

#ifdef __C51__
	HW_WATCHDOG_TRIGGER = 1;        /* Retrigger the HW Watchdog of the IM183*/
	HW_WATCHDOG_TRIGGER = 0;
#endif

/*============ Handling of the output data =================*/

	if (DPS2_POLL_IND_DX_OUT()) /* are new output date available? */
		{
		/* Confirm the receiving */
		DPS2_CON_IND_DX_OUT();

		/* Get the pointer to the actual output data */
		user_output_buffer_ptr = DPS2_OUTPUT_UPDATE();


		/* Example: Copy the output data to the IO */
		for (i=0; i<user_io_data_len_ptr->outp_data_len; i++)
		   {
		   (*((io_byte_ptr) + i)) = (*(((UBYTE SPC3_PTR_ATTR*) user_output_buffer_ptr) + i));
		   }
		}


/*============ Handling of the input data =================*/

	/* Write the input data from the periphery to the ASIC */
	for (i=0; i<user_io_data_len_ptr->inp_data_len; i++)
		{
		*(((UBYTE SPC3_PTR_ATTR*) user_input_buffer_ptr) + i) = *((io_byte_ptr) + i);
		}

	/* Give the actuall pointer / data to the SPC3/DPS2 an get a new pointer,
		   where the next input data can be written */
	user_input_buffer_ptr = DPS2_INPUT_UPDATE();


/*== Handling of the external diagnosis and other user defined actions =====*/
/* ATTENTION:         this is only an example              */

/* Use the first Byte of the Input data as a service byte  */
/*  for the change diag function                           */

	dps_chg_diag_srvc_byte_new = *((UBYTE*)(io_byte_ptr));

	if (user_diag_flag) /* is a diagnosis buffer available? */
		{
		/* Is there a change in the service byte (1.input byte) */
		if (dps_chg_diag_srvc_byte_new == dps_chg_diag_srvc_byte_old)
			{
			 /* no action */
			 }
		else
			{
			/*== Handling of the external diagnosis  =====*/
			/* only the least significant 3 byte are used */
			if ((dps_chg_diag_srvc_byte_new & 0x07) !=
				  (dps_chg_diag_srvc_byte_old & 0x07))
				{
				/* Mask the 3 bits */
				diag_service_code = dps_chg_diag_srvc_byte_new & 0x07;

				/* Write the length of the diagnosis data to the SPC3 */
				if (dps_chg_diag_srvc_byte_new & 0x01)
					diag_len = 16;   /* max. value of the IM308B */
				else
					diag_len = 6;
				diag_len = DPS2_SET_DIAG_LEN(diag_len);

				/* Write the external diagnosis data to the SPC3 */
				build_diag_data_blk ((struct diag_data_blk *)user_diag_buffer_ptr);

				/* Set the service code             */
				/* 0x01 External diagnosis          */
				/* 0x02 Static   diagnosis          */
				/* 0x04 External diagnosis Overflow */
				DPS2_SET_DIAG_STATE(diag_service_code);


				/* Trigger the diagnosis update in the SPC3*/
				DPS2_DIAG_UPDATE();

				/* Store "no diagnosis buffer available"  */
				user_diag_flag = FALSE;

				}

			dps_chg_diag_srvc_byte_old = dps_chg_diag_srvc_byte_new;

			}
		}


/*================ Check the buffers and the state =================*/

/* Is a new diagnosis buffer available */
	 if (DPS2_POLL_IND_DIAG_BUFFER_CHANGED())
		{
		DPS2_CON_IND_DIAG_BUFFER_CHANGED(); /* Confirm the indication */
		user_diag_buffer_ptr = DPS2_GET_DIAG_BUF_PTR(); /* Fetch the pointer */
		user_diag_flag = TRUE; /* Set the Notice "Diag. buffer availble      */
		}

	}   /*=== endless loop        ===*/

	return;

}

/* #pragma public */


/************************************************************************/
/*  D e s c r i p t i o n :                                             */
/*                                                                      */
/* Reset the USER and DPS                                               */
/************************************************************************/


void user_dps_reset (void)
{
enum SPC3_INIT_RET dps2_init_result;            /* result of the initial. */


DPS2_SET_IDENT_NUMBER_HIGH(ident_numb_high);    /* Set the Identnumber    */
DPS2_SET_IDENT_NUMBER_LOW(ident_numb_low);

SPC3_SET_STATION_ADDRESS(this_station);         /* Set the station address*/

SPC3_SET_HW_MODE(SYNC_SUPPORTED | FREEZE_SUPPORTED | INT_POL_LOW | USER_TIMEBASE_10m);
												/* Set div. modes of the  */
												/* SPC3                   */
if (!real_no_add_chg)
	{
	DPS2_SET_ADD_CHG_ENABLE();                  /* Allow or allow not the */
	}                                           /* address change         */
else
	{
	DPS2_SET_ADD_CHG_DISABLE();
	}


/* initialize the length of the buffers for DPS2_INIT() */
dps2_buf.din_dout_buf_len = 244;
dps2_buf.diag_buf_len = sizeof(struct diag_data_blk);
dps2_buf.prm_buf_len = 20;
dps2_buf.cfg_buf_len = 10;

/* dps2_buf.ssa_buf_len = 5;    reserve buffer if address change is possible */
dps2_buf.ssa_buf_len = 0;       /* Suspend the address change service  */
								/* No storage in the IM183 is possible */


/* initialize the buffers in the SPC3                   */
dps2_init_result = SPC3_INIT(&dps2_buf);
if(dps2_init_result != SPC3_INIT_OK)
	{       /* Failure */
	for(;;)
		{
		error_code = INIT_ERROR;
		user_error_function(error_code);
		}
	}

/* Get a buffer for the first configuration */   
real_config_data_ptr = (UBYTE SPC3_PTR_ATTR*) DPS2_GET_READ_CFG_BUF_PTR();

/* Set the length of the configuration data */
DPS2_SET_READ_CFG_LEN(CFG_LEN);

/* Write the configuration bytes in the buffer */
*(real_config_data_ptr) = CONFIG_DATA_INP;      /* Example 0x13 */
*(real_config_data_ptr + 1) = CONFIG_DATA_OUTP; /* Example 0x23 */

/* Store the actuall configuration in RAM for the check in the 
   check_configuration sequence (see the modul intspc3.c)       */
cfg_akt[0] = CONFIG_DATA_INP;
cfg_akt[1] = CONFIG_DATA_OUTP;
cfg_len_akt = 2;


/* Calculate the length of the input and output using the configuration bytes*/
user_io_data_len_ptr = dps2_calculate_inp_outp_len (real_config_data_ptr,(UWORD)CFG_LEN);
if (user_io_data_len_ptr != (DPS2_IO_DATA_LEN *)0)
	{
	/* Write the IO data length in the init block */
	DPS2_SET_IO_DATA_LEN(user_io_data_len_ptr);
	}
else
	{
	for(;;)
		{
		error_code =IO_LENGTH_ERROR;
		user_error_function(error_code);
		}
	}


/* Fetch the first input buffer */
user_input_buffer_ptr = DPS2_GET_DIN_BUF_PTR();

/* Fetch the first diagnosis buffer, initialize service bytes */
dps_chg_diag_srvc_byte_new = dps_chg_diag_srvc_byte_old = 0;
user_diag_buffer_ptr = DPS2_GET_DIAG_BUF_PTR();
user_diag_flag = TRUE; 

/* for info: get the baudrate                */
user_baud_value = SPC3_GET_BAUD();

/* Set the Watchdog for the baudrate control */
SPC3_SET_BAUD_CNTRL(0x1E);

/* and finally, at last, los geht's start the SPC3 */
SPC3_START();

}




/************************************************************************/
/*  D e s c r i p t i o n :                                             */
/*                                                                      */
/*  user_error_function                                                 */
/************************************************************************/
void user_error_function(enum ERRORCODES errors)
{
	char	ch;
	/* An error has occured -> user defined actions */
	/* you can check the error_code                 */
	ch=errors;
	while (1);

}


/************************************************************************/
/*  D e s c r i p t i o n :                                             */
/*                                                                      */
/*  build diagnostics datablock                                         */
/*                                                                      */
/*  In this EXAMPLE the received user parameter data are copied to      */
/*  the user diagnosis data!                                            */
/************************************************************************/

void build_diag_data_blk (struct diag_data_blk * diag_ptr)
  {
  diag_ptr -> header                 = (diag_len - 6);       /* subtract the fixed */
  diag_ptr -> ext_diag_0             = prm_tst_buf[0];   /* DP diagnosis       */
  diag_ptr -> ext_diag_1             = prm_tst_buf[1];
  diag_ptr -> ext_diag_2             = prm_tst_buf[2];
  diag_ptr -> ext_diag_3             = prm_tst_buf[3];
  diag_ptr -> ext_diag_4             = prm_tst_buf[4];
  diag_ptr -> ext_diag_5             = prm_tst_buf[5];
  diag_ptr -> ext_diag_6             = prm_tst_buf[6];
  diag_ptr -> ext_diag_7             = prm_tst_buf[7];
  diag_ptr -> ext_diag_8             = prm_tst_buf[8];
  diag_ptr -> ext_diag_9             = prm_tst_buf[9];
  diag_ptr -> ext_diag_10            = prm_tst_buf[10];
  diag_ptr -> ext_diag_11            = prm_tst_buf[11];
  diag_ptr -> ext_diag_12            = prm_tst_buf[12];
  diag_ptr -> ext_diag_13            = 0;
  diag_ptr -> ext_diag_14            = 0;
  diag_ptr -> ext_diag_15            = 0;
  diag_ptr -> ext_diag_16            = 0;
  diag_ptr -> ext_diag_17            = 0;
  }



⌨️ 快捷键说明

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