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

📄 userspc3.c

📁 profibus通信协议
💻 C
📖 第 1 页 / 共 2 页
字号:

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

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

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

#include "spc3dps2.h"
#include "iom64v.h"
#include <macros.h>
#include "eeprom.h"  
				
#define   Clr_run_led()      PORTB&=~BIT(PB2)	   /* show SPC3'states				*/
#define   Set_run_led()      PORTB|=BIT(PB2)	   /* show SPC3'states				*/

#define   Clr_SPC3_1_RESET()   PORTB&=~BIT(PB0)    /* SPC3-reset-Pin               */
#define   Set_SPC3_1_RESET()   PORTB|= BIT(PB0)    /* SPC3-reset-Pin               */

#define   Clr_WD()      PORTB &= ~BIT(PB3)	       /* 			*/
#define   Set_WD()      PORTB |= BIT(PB3)	       /* 			*/


#define BR_NUM	1					/* for serial baudrate		    */
#define SPC3_RESET_SET 1            /* reset-polarity               */
#define SPC3_INTERRUPT_ENABLE  EX0  /* external interrupt of SPC3   */
#define I_ENABLE 1                  /* enable interrupt             */
#define GLOBAL_INTERRUPT_ENABLE EA  /* global interrupt             */




/********************The time-out of WDT at 12 MHz ***********
		16MS 	WDTPRG=0x0
		32MS 	WDTPRG=0x1
		65MS 	WDTPRG=0x2
		131MS 	WDTPRG=0x3
		262MS 	WDTPRG=0x4
		542MS 	WDTPRG=0x5
		1MS 	WDTPRG=0x6
		2MS 	WDTPRG=0x7
**************************************************************/
/*
#define WDT_INIT() WDTPRG=0x5;\
		WDTRST=0x1E;\
		WDTRST=0xE1

#define WDT_RE() WDTRST=0x1E;\
		WDTRST=0xE1
*/


/*----------------------------------------------------------------------*/
/*                       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;

  /*------------------ extern diag data  -----------------*/
  UBYTE                               header;
  UBYTE                               ext_diag_0;
  UBYTE                               ext_diag_1;
  UBYTE                               ext_diag_2;
  UBYTE                               ext_diag_3;
  UBYTE                               ext_diag_4;
  UBYTE                               ext_diag_5;
  UBYTE                               ext_diag_6;
  UBYTE                               ext_diag_7;
  UBYTE                               ext_diag_8;
  UBYTE                               ext_diag_9;
  UBYTE                               ext_diag_10;
  UBYTE                               ext_diag_11;
  UBYTE                               ext_diag_12;
  UBYTE                               ext_diag_13;
  UBYTE                               ext_diag_14;
  UBYTE                               ext_diag_15;
  UBYTE                               ext_diag_16;
  UBYTE                               ext_diag_17;
  };

enum ERRORCODES {IO_LENGTH_ERROR,INIT_ERROR,UNUSED};

/*----------------------------------------------------------------------*/
/*                       INTERNAL FUNCTIONS                             */
/*----------------------------------------------------------------------*/
void main (void);

void user_dps_reset (void);
void build_diag_data_blk (struct diag_data_blk*);
void user_error_function (enum ERRORCODES);
/*
void init_serial(void);
void send(UBYTE *str,UBYTE len);
*/
/*----------------------------------------------------------------------*/
/*                       LITERALS                                       */
/*----------------------------------------------------------------------*/


//  #define OWN_ADDRESS             19 //
#define IDENT_HIGH            0x0B    // 0x06         //
#define IDENT_LOW             0x9F    // 0xFA         // 


#define CFG_LEN                 2
#define CONFIG_DATA_INP         0x17   //  0x16(7)  0x13(4)
#define CONFIG_DATA_OUTP        0x25   //4

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

UBYTE                   real_no_add_chg;
UBYTE                   this_station;
UBYTE                   cfg_akt [20];
UBYTE                   cfg_len_akt;
DPS2_IO_DATA_LEN   *user_io_data_len_ptr;

struct diag_data_blk    *diag_ptr;
DPS2_BUFINIT            dps2_buf;

UBYTE      *user_output_buffer_ptr;

UBYTE      *user_input_buffer_ptr;
UBYTE      *real_config_data_ptr;
void       *user_diag_buffer_ptr;

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;
UBYTE                   prm_tst_buf[32]= {0,0,0,0,0,0,0,0,0,0,
											0,0,0,0,0,0,0,0,0,0,
											0,0,0,0,0,0,0,0,0,0,0,0};
UBYTE                   WD_Flag0;
UBYTE                   Isr_Flag1;
UBYTE                 	OWN_ADDRESS;	
UBYTE                 	T_OWN_ADDRESS;	
							
										
extern UBYTE            Profi_S_Data[8];
extern UBYTE            CKD_R_Data[14];
extern UBYTE            CKD_S_Data[9];
extern UBYTE            Profi_R_Data[8];
	
											
enum ERRORCODES error_code;


void twi_init(void);

unsigned char CKD_R_Data_Process(void);
void PROFI_R_Data_Process(void);

unsigned char  I2c_RS_Data_Comm(void);

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0xff;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
 PORTE = 0x00;
 DDRE  = 0x00;
 PORTF = 0x00;
 DDRF  = 0x00;
 PORTG = 0x00;
 DDRG  = 0x00;
}

/*
void watchdog_ini(void)
{
  WDR();       //喂狗,清0,防止狗定时器益处
  WDTCR=0x0b;  //狗使能,欲分频设为010,VCC=5V益处时间为65。2毫秒
 }
 
void watchdog_off(void) 
{
  WDTCR=BIT(WDCE);
  WDTCR=BIT(WDCE);  //对WDCE 和WDE 写1
  WDTCR=0x00;            //WDE 写0;禁止看门狗
 // WDTCR=0x00; 
} 
*/

void Send_watchdog(void)
{
     if (WD_Flag0)  
	 {
	         Clr_WD();
			 WD_Flag0 = 0;
     }
	 else
	 {
	         Set_WD();
			 WD_Flag0 = 1;
	 }
}
  

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts

 port_init();

 MCUCR = 0x80;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x01; //extended ext ints
 EIMSK = 0x10;
 
 twi_init();
 
 SEI();
 
 //TIMSK = 0x00; //timer interrupt sources
 //ETIMSK = 0x00; //extended timer interrupt sources*/
 //re-enable interrupts
 //all peripherals are now initialized
}

void Terminal_Address_Ini(void)
{
 
   while(!CKD_R_Data_Process())
   {
       Send_watchdog();
   }
   
   
   if ((CKD_R_Data[1] >= 0x01) && (CKD_R_Data[1] <= 0x7d))
   {
       OWN_ADDRESS = CKD_R_Data[1];
   }
   else
   {
       OWN_ADDRESS = 0xff;
   }
 
}



void Spc3_Terminal_Ini(void)
{

volatile UBYTE  *loesch;
UWORD zaehl;

Set_SPC3_1_RESET();
//watchdog_ini();
for(i = 100; i ; i--);

//SPC3_RESET = !SPC3_RESET_SET;
Clr_SPC3_1_RESET();

//Set_run_led();

//SPC3_INTERRUPT_ENABLE = !I_ENABLE;  /* Set the interrupt parameters */

loesch = &spc3.int_req.b[0];        /* Delete the SPC3 internal RAM */

for (zaehl=0x16; zaehl<=0x05ff; zaehl++)
{
	*(loesch + zaehl) = 0;
}
//Clr_run_led(); 
//init_serial();	

/* activate the indication functions */
SPC3_SET_IND(GO_LEAVE_DATA_EX | WD_DP_MODE_TIMEOUT | NEW_GC_COMMAND |\
			   NEW_CFG_DATA | NEW_PRM_DATA | BAUDRATE_DETECT);

/* set the watchdog value in the SPC3, which checks the microprocessor */
//DPS2_SET_USER_WD_VALUE(20000);  // 
DPS2_SET_USER_WD_VALUE(SWAP_W(20000));

/* 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  *) 0x110);

for (i=0; i<20; i++)
	{
	(*(io_byte_ptr + i)) = 3;
	}

this_station = OWN_ADDRESS;
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 */
user_dps_reset();

}


void main()
{


init_devices();


Terminal_Address_Ini();

Spc3_Terminal_Ini();

⌨️ 快捷键说明

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