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

📄 userspc3.c

📁 profibus TO RS485 profibus TO RS485
💻 C
📖 第 1 页 / 共 2 页
字号:


#pragma    	code                        //
#define    	SPC3_INTEL_MODE    TRUE    //单片机为intel模式
#define    	SPC3_FAR                   //

#define    	SPC3_DPS2		  //
#define    	SPC3_DATA_XDATA           //spc3数据存储地址


#include    "spc3dps2.h" 		//profibus协议头文件
#include	"link.h"
#include    "profibus.h"
#include        <intrins.h>
#include 	<reg52.h>

#define 	huge
#define 	global
#define 	public

#define  RECEIVE_ENABLE  INT1



//#define uchar unsigned char
//#define uint unsigned int

#define IS_CMD 	0xaa	//是命令
#define	IS_DATA 	0x55	//是数据

#define CONSIS_DIAG_BIT_N  			2	/*第3位为从站诊断区读写一致性标志*/
#define SET_CONSIS_DIAG_FLAG   		0X04	/*置位从站诊断区读写一致性标志常数*/
#define CLR_CONSIS_DIAG_FLAG  		0Xfb	/*复位从站诊断区读写一致性标志常数*/
#define CONSIS_IN_BIT_N				1	/*第2位为从站输入区读写一致性标志*/
#define SET_CONSIS_IN_FLAG   		0X02	/*置位从站输入区读写一致性标志常数*/
#define CLR_CONSIS_IN_FLAG  		0Xfd	/*复位从站输入区读写一致性标志常数*/
#define CONSIS_OUT_BIT_N			0	/*第1位为从站输出区读写一致性标志*/
#define SET_CONSIS_OUT_FLAG   		0X01	/*置位从站输出区读写一致性标志常数*/
#define CLR_CONSIS_OUT_FLAG  		0Xfe	/*复位从站输出区读写一致性标志常数*/
/*----------------------------------------------------------------------*/
UBYTE   in_data_map[255] _at_ 0x4000;
UBYTE   out_data_map[255] _at_ 0x5000;
UBYTE   prm_data_map[255] _at_ 0x6000;
extern uchar	data	uc_system_status_flag;
/*----------------------------------------------------------------------*/


	/* 定义spc3的管脚 */
//sbit  HW_WATCHDOG_TRIGGER = P3^5;   /* HW-WD输入端   */
sbit  SPC3_RESET = P3^4;            /* SPC3复位     */
sbit  oe_switch = P3^3;             //片选          */





#define SPC3_RESET_SET 1            /* reset-polarity               */
#define SPC3_INTERRUPT_ENABLE  EX0  /* external interrupt of SPC3   */
#define I_ENABLE 1                  /* enable interrupt             */
#define SPC3_INTERRUPT_PRIOR   PX0  /* ext Interrupt SPC3 polarity  */
#define P_HIGH  1                   /* ext Interrupt SPC3 prio high */
#define GLOBAL_INTERRUPT_ENABLE EA  /* global interrupt             */

/*----------------------------------------------------------------------*/
/*                       STRUCTURES AND TYPEDEFS                        */
/*----------------------------------------------------------------------*/

/* 用户诊断数据结构 */
struct  diag_data_blk
	{
  	UBYTE                               stationstatus_1;
  	UBYTE                               stationstatus_2;
  	UBYTE                               stationstatus_3;
  	UBYTE                               master_add;
  	UBYTE                               ident_high;
  	UBYTE                               ident_low;

  /*------------------ 外扩诊断数据  -----------------*/
  	UBYTE                               header;
	UBYTE   diag_data_map[255];
        };

enum ERRORCODES {IO_LENGTH_ERROR,INIT_ERROR,UNUSED};
//UBYTE xdata temp_serial[256] _at_ 0x2000;
//UBYTE SBUF;
//UBYTE a1 = 0,a2 = 0;
void main (void);                 //主函数
void init_buffer(void);
void user_dps_reset (void);       //SPC3复位
void build_diag_data_blk (struct diag_data_blk*);  //建立诊断数据
void user_error_function (enum ERRORCODES);      //出错处理函数
UBYTE get_addr(void);//读取站地址
//uchar update_data(struct LINK_DATA_BUF *p_link_data_buf1);
void inteface_flag_p(void);
#define TSDRMIN                 11

#define OWN_ADDRESS             3			//01.12.09修改为由拨码读入站地址

#define IDENT_HIGH              0x0A			//ID标志高位
#define IDENT_LOW               0x0F			//ID标志低位


#define DPS2_DISI()             IEN = 0
#define DPS2_ENI()              IEN = 1

#define CFG_LEN                 16
#define CONFIG_DATA_INP         0x13			//输入配置数据
#define CONFIG_DATA_OUTP        0x23			//输入配置数据


#pragma global                                      //全局变量


UBYTE                   real_no_add_chg;
UBYTE                   this_station;
UBYTE                   cfg_akt [20];			//配置应答
UBYTE                   cfg_len_akt;			//配置应答长度


UBYTE			glab_diag_flag;			//全局的诊断标
UBYTE                   glab_prm_flag = 0x0;                     //第一次配置

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



struct LINK_DATA_BUF xdata *p_link_data_buf1 ;
struct LINK_DATA_BUF xdata str_link_data_buf ;


/************************************************************/
DPS2_IO_DATA_LEN SPC3_PTR_ATTR  *user_io_data_len_ptr;

#pragma public
struct diag_data_blk    *diag_ptr;
struct diag_data_blk    ext_diag;
DPS2_BUFINIT            dps2_buf;

UBYTE   SPC3_PTR_ATTR   *user_output_buffer_ptr;

UBYTE   SPC3_PTR_ATTR   *user_input_buffer_ptr;
UBYTE   SPC3_PTR_ATTR   *real_config_data_ptr;

void    SPC3_PTR_ATTR   *user_diag_buffer_ptr;

//UBYTE                   user_cmd_flag;             //用户命令标志

UBYTE                   ident_numb_high;
UBYTE                   ident_numb_low;
UBYTE                   dps_chg_diag_srvc_byte_new;
UBYTE                   dps_chg_diag_srvc_byte_old;
UBYTE                   diag_service_code;          //诊断代码
UBYTE                   diag_len;                   //诊断长度
UWORD                   user_baud_value;            //用户通讯波特率
UBYTE                   user_diag_flag;             //用户诊断标志
UBYTE                   *io_byte_ptr;
UBYTE                   i;
UBYTE                   zyk_wd_state;              //看门狗状态
UBYTE                   zyk_dps_state;             //DPS状态
UBYTE                   ext_diag_buf[72]= {0};//外部诊断

UBYTE			old_glab_prm_flag = 0x0;

enum ERRORCODES error_code;
unsigned char station;
UBYTE spc3_result;
/* 定位 SPC3 的地址空间结构 */



SPC3 xdata spc3 _at_ 0x9000;


void    main ()
{
UBYTE data ii,j;
UBYTE xdata * xdata loesch;

UWORD zaehl;
SPC3_RESET = SPC3_RESET_SET;        /* 强制复位          */
for(ii = 500; ii;ii-- )		   //复位延时
 {
 j=ii;
 }

/******************************************/
init_buffer();//清BUF
dpram_interface_prog_init();//双口RAM接口程序的初始化函数
/******************************************/
if((EE_rdsr_cmd()&0x30) == 0x30)	//如果看门狗还没起动
	{
		EE_wren_cmd();			//写使能
		EE_wrsr_cmd(0);			//打开看门狗并设置为1.4秒
	}
EE_rst_wdog();

//HW_WATCHDOG_TRIGGER = 1;            /* 触发 HW-WD    */
//HW_WATCHDOG_TRIGGER = 0;
SPC3_RESET = !SPC3_RESET_SET;


SPC3_INTERRUPT_ENABLE = !I_ENABLE;  /* Set the interrupt parameters */
SPC3_INTERRUPT_PRIOR = P_HIGH;      /* of the microprocessor        */

loesch = &spc3.int_req.b[0];        /*  SPC3 RAM 清零 */


for (zaehl=0x16; zaehl<=0x05ff; zaehl++)/*前0X16字节内部寄存器*/
	{
	*(loesch + zaehl) = 00;
	}

/*设置SPC3的中断屏蔽寄存器*/
/* activate the indication functions */
SPC3_SET_IND(GO_LEAVE_DATA_EX | WD_DP_MODE_TIMEOUT | NEW_GC_COMMAND |\
			   NEW_SSA_DATA | NEW_CFG_DATA | NEW_PRM_DATA | BAUDRATE_DETECT);
/* SPC3内部看门狗定时器设置*/
/* set the watchdog value in the SPC3, which checks the microprocessor */
DPS2_SET_USER_WD_VALUE(20000);

/*设置IO的起使地址*/
/* In this example the input and output bytes are transfered to the
   IO area, which is addressed by the io_byte_ptr. In the case of the IM183 there is RAM. */


/*io_byte_ptr = ((UBYTE*) 0x2E000L);
for (ii=0; ii<244; ii++)     //协议规定数据最长为488字节
	{
	(*(io_byte_ptr + ii)) = 0;
	}*/

/* fetch the station address, in this case the station address
   is fixed in EPROM*/

 this_station = get_addr();// /*获取从站地址0X3   2001.2.09修改*/

/* get the Identnumber    */
/*获取从站用户标志*/
ident_numb_high = IDENT_HIGH;
ident_numb_low =  IDENT_LOW;

/*不许主站动态改变从站地址*/
/* Allow not the change of the slave address by the PROFIBUS DP      */
/* Attention: The set_slave_address service is not blocked by this */
real_no_add_chg = TRUE;


/* Reset the User und DPS */
/*复位所有的缓存以及所有的寄存器*/
do
	{
	user_dps_reset();
	}while (spc3_result !=0xaa);
/*使能单片机的中断和SPC3的中断*/
/* Enable the SPC3 interrupt in the interupt controller of the microprocessor */


SPC3_INTERRUPT_ENABLE = I_ENABLE;
GLOBAL_INTERRUPT_ENABLE = I_ENABLE;


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

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


        /*复位SPC3的用户看门狗*/
	DPS2_RESET_USER_WD();           /* Trigger the user watchdog of the SPC3 */

        /*复位单片机的看门狗*/
        EE_rst_wdog();
	//HW_WATCHDOG_TRIGGER = 1;        /* Retrigger the HW Watchdog of the IM183*/
	//HW_WATCHDOG_TRIGGER = 0;



/*============ Handling of the output data =================*/
/*输出数据是指主站输出数据给从站*/


	if (DPS2_POLL_IND_DX_OUT()) /* are new output date available? */
		{
		glab_diag_flag ++;
		/* 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++)
		   {
                         out_data_map[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) =	in_data_map[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                           */



	if(user_diag_flag)
	{
		if(glab_diag_flag  == 0x05)
		{
                        glab_diag_flag  = 0x0;
		        diag_service_code = 0x01;
		        diag_len = 80;   // max. value of the IM308B
		        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;






		}

	}


/*================ 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      */
		}

                inteface_flag_p();
 		//判断参数化的数据是否OK
       if(p_link_data_buf1->interface_flag !=LINK_ERR )
       	{

         update_data(p_link_data_buf1);

⌨️ 快捷键说明

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