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

📄 dp_user.c

📁 PROFIBUS SLAVE PROGRAMS PRO FIBUS SLAVE PROGRAMS
💻 C
📖 第 1 页 / 共 3 页
字号:
        error = vpc3_initialization( (*READ_PORT0 & 0x7F), user.real_cfg );     // address of slave; PORT0
    #else
        error = vpc3_initialization( DP_ADDR, user.real_cfg );                  // address of slave
    #endif//#ifdef EvaBoard_AT89C5132

    if( DP_OK == error )
    {
        // set microcontroller interrupt to falling edge
        SET_FALLING_EDGE__;
        // enable microcontroller interrupt
        ENABLE_VPC3_INT__;

        //todo: before startup the VPC3+, make here your own initialzations

        START_VPC3();
    }
    else
    {
        vpc3_errcb.error_code = error;
        fatal_error( _DP_USER, __LINE__, &vpc3_errcb );
    }//if( DP_OK == error )

    /*-----------------------------------------------------------------------*/
    /* endless demo operation loop                                           */
    /*-----------------------------------------------------------------------*/
    while( 1 )
    {
        /*-------------------------------------------------------------------*/
        /* trigger watchdogs                                                 */
        /*-------------------------------------------------------------------*/
        // toggle user watchdog
        VPC3_RESET_USER_WD();   // toggle user watchdog

        #ifdef RS232_SERIO
            if(rec_counter > 0)
            {
                DISABLE_ALL_INTERRUPT_BIT__;

                print_serial_inputs();

                ENABLE_ALL_INTERRUPT_BIT__;
            }
        #endif//#ifdef RS232_SERIO


        /*-------------------------------------------------------------------*/
        /* internal state machine                                            */
        /*-------------------------------------------------------------------*/
        if( user.state == USER_STATE_CLEAR )
        {
            // clear data
            memset( &user.input[0] , 0, 2 );
            memset( &user.output[0], 0, 2 );

            #ifdef EvaBoard_AT89C5132
                *WRITE_PORT1 = 0x00;
                *WRITE_PORT2 = 0x00;
            #endif//#ifdef EvaBoard_AT89C5132

            user.state = USER_STATE_RUN;
        }//if( user.state == USER_STATE_CLEAR )

        /*-------------------------------------------------------------------*/
        /* dpv1 statemachines                                                */
        /*-------------------------------------------------------------------*/
        vpc3_dpv1_statemachine();

        /*-------------------------------------------------------------------*/
        /* VPC3+ DP-state                                                    */
        /*-------------------------------------------------------------------*/
        switch(VPC3_GET_DP_STATE())
        {
            case WAIT_PRM:
            {
                // set LED's
                CLR_LED_YLW__;
                SET_LED_RED__;

                //BugFix 502
                if( ( vpc3_get_master() != 0xFF ) && (( p_Vpc3->isreg.rd.status_L == 0x85 ) || ( p_Vpc3->isreg.rd.status_L == 0x81 )) )
                {
                    GO_OFFLINE_VPC3();
                    do
                    {
                        //wait, for offline
                    }while( VPC3_GET_OFF_PASS() );

                    START_VPC3();
                }

                if( user.event & VPC3_EV_DX_OUT )
                {
                    user.event &= ~VPC3_EV_DX_OUT;       // clear event
                }

                break;
            }//case WAIT_PRM:

            case WAIT_CFG:
            case DP_ERROR:
            {
                // set LED's
                CLR_LED_YLW__;
                SET_LED_RED__;

                if( user.event & VPC3_EV_DX_OUT )
                {
                    user.event &= ~VPC3_EV_DX_OUT;       // clear event
                }
                break;
            }

            case DATA_EX:
            {
                // set LED's
                SET_LED_YLW__;
                CLR_LED_RED__;

                if(    ( user.application_ready == TRUE )
                    && ( user.state == USER_STATE_RUN   )
                  )
                {
                    /*-------------------------------------------------------------------*/
                    /* user application                                                  */
                    /*-------------------------------------------------------------------*/
                    #ifdef EvaBoard_AT89C5132
                        if( user.input[0] != *READ_PORT1 )
                        {
                            user.input[0] = *READ_PORT1;
                            user.event |= VPC3_EV_NEW_INPUT_DATA;
                        }

                        if( user.input[1] != *READ_PORT2 )
                        {
                            user.input[1] = *READ_PORT2;
                            user.event |= VPC3_EV_NEW_INPUT_DATA;
                        }
                    #endif//#ifdef EvaBoard_AT89C5132

                    /*-------------------------------------------------------------------*/
                    /* profibus input                                                    */
                    /*-------------------------------------------------------------------*/
                    if( user.event & VPC3_EV_NEW_INPUT_DATA )
                    {
                        read_input_data();
                        user.event &= ~VPC3_EV_NEW_INPUT_DATA;
                    }
                }//if(    ( user.application_ready == TRUE ) ...

                break;
            }//case DATA_EX:

            default:
            {
                vpc3_errcb.error_code = VPC3_GET_DP_STATE();
                fatal_error( _DP_USER, __LINE__, &vpc3_errcb );
                break;
            }
        }//switch(VPC3_GET_DP_STATE())

        /*-------------------------------------------------------------------*/
        /* profibus output                                                   */
        /*-------------------------------------------------------------------*/
        if( user.event & VPC3_EV_DX_OUT )
        {
            user.event &= ~VPC3_EV_DX_OUT;       // clear event
            if( output_buf_ptr = vpc3_get_doutbufptr (&outp_state) )
            {
                //todo:
                //handle here data (master --> slave)
                memcpy( &user.output[0], output_buf_ptr, dp_sys.outp_data_len);

                /*-----------------------------------------------------------*/
                /* user application                                          */
                /*-----------------------------------------------------------*/
                //copy output data to input data
                memcpy( &user.input[0], &user.output[0], dp_sys.outp_data_len);

                #ifdef EvaBoard_AT89C5132
                    *WRITE_PORT1 = user.output[0];
                    *WRITE_PORT2 = user.output[1];
                #endif//#ifdef EvaBoard_AT89C5132

                user.event |= VPC3_EV_NEW_INPUT_DATA;
            }//if( user.output_buf_ptr = vpc3_get_doutbufptr (&outp_state) )
        }//if( user.event & VPC3_EV_DX_OUT )

        /*-------------------------------------------------------------------*/
        /* handle here profibus interrupt events                             */
        /*-------------------------------------------------------------------*/
        if( user.event & VPC3_EV_NEW_CFG_DATA )
        {
            application_ready();
            user.event &= ~VPC3_EV_NEW_CFG_DATA;    // clear event
        }

    }//while(1)
}//void user_main( void )

/*---------------------------------------------------------------------------*/
/* function: delay                                                           */
/*---------------------------------------------------------------------------*/
void delay( void )
{
UBYTE i,j;

    for(i = 0; i < 255; i++)
    {
        for(j = 0; j < 255; j++);
    }
}//void delay( void )

/*---------------------------------------------------------------------------*/
/* function: fatal_error                                                     */
/*---------------------------------------------------------------------------*/
void fatal_error( DP_ERROR_FILE file, UWORD line, VPC3_ERRCB_PTR errcb_ptr )
{
    #ifdef RS232_SERIO
        do
        {
            // wait!
        }
        while( snd_counter > 80);

        print_string("\r\nFATAL_ERROR:");
        print_string("\r\nFile: ");
        print_hexbyte(file);
        print_string("\r\nLine: ");
        print_hexword(line);
        print_string("\r\nFunction: ");
        print_hexbyte(errcb_ptr->function);
        print_string("\r\nError_Code: ");
        print_hexbyte(errcb_ptr->error_code);
        print_string("\r\nDetail: ");
        print_hexbyte(errcb_ptr->detail);
        print_string("\r\ncn_id: ");
        print_hexbyte(errcb_ptr->cn_id);
    #endif//#ifdef RS232_SERIO

    #ifdef EvaBoard_AT89C5132
        *WRITE_PORT0 = errcb_ptr->error_code;
        *WRITE_PORT1 = file;
        *WRITE_PORT2 = (UBYTE)line;
    #endif//#ifdef EvaBoard_AT89C5132

    SET_LED_YLW__;
    SET_LED_RED__;

    while(1)
    {

⌨️ 快捷键说明

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