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

📄 kit05.c

📁 瑞萨智能车大赛源码。High-performance Embedded Workshop
💻 C
📖 第 1 页 / 共 2 页
字号:
                /* Judgment of left crank -> To the left crank clearing process */
                led_out( 0x1 );
                handle( -38 );
                speed( 10 ,50 );
                pattern = 31;
                cnt1 = 0;
                break;
            case 0x07:
                /* Judgment of right crank -> The process for getting over right crank */
                led_out( 0x2 );
                handle( 38 );
                speed( 50 ,10 );
                pattern = 41;
                cnt1 = 0;
                break;
            case 0x00:
                /* Center -> Straight */
                handle( 0 );
                speed( 40 ,40 );
                break;
            case 0x04:
            case 0x06:
            case 0x03:
                /* Tending to the left -> Turn to the right */
                handle( 8 );
                speed( 40 ,36 );
                break;
            case 0x20:
            case 0x60:
            case 0xc0:
                /* Tending to the right -> Turn to the left */
                handle( -8 );
                speed( 36 ,40 );
                break;
            default:
                break;
        }
        break;

    case 31:
        /* Left crank clear process Wait a little till it becomes stable. */
        if( cnt1 > 200 ) {
            pattern = 32;
            cnt1 = 0;
        }
        break;

    case 32:
        /* Left crank clear process Check of end of turn */
        if( sensor_inp(MASK3_3) == 0x60 ) {
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
        }
        break;

    case 41:
        /* Right crank clear processing Waits a little until stabilizing */
        if( cnt1 > 200 ) {
            pattern = 42;
            cnt1 = 0;
        }
        break;

    case 42:
        /* Right crank clear processing Turning completion check */
        if( sensor_inp(MASK3_3) == 0x06 ) {
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
        }
        break;

    default:
        /* When the pattern is not applied to any case, return to waiting state */
        pattern = 0;
        break;
    }
    }
}

/************************************************************************/
/* H8/3048F-ONE Built in Peripheral Function Initialization             */
/************************************************************************/
void init( void )
{
    /* I/O port Setting */
    P1DDR = 0xff;
    P2DDR = 0xff;
    P3DDR = 0xff;
    P4DDR = 0xff;
    P5DDR = 0xff;
    P6DDR = 0xf0;                       /* DIP SW on CPU board          */
    P8DDR = 0xff;
    P9DDR = 0xf7;                       /* Communication Port           */
    PADDR = 0xff;
    PBDR  = 0xc0;
    PBDDR = 0xfe;                       /* Motor Drive Board Vol.3      */
    /* As P7 of the sensor board is an exclusive input, there are no input output settings. */

    /* ITU0 Interrupt at every 1ms */
    ITU0_TCR = 0x23;
    ITU0_GRA = TIMER_CYCLE;
    ITU0_IER = 0x01;

    /* ITU3, 4 reset-synchronized PWM mode for right-left motor and servo */
    ITU3_TCR = 0x23;
    ITU_FCR  = 0x3e;
    ITU3_GRA = PWM_CYCLE;                   /* Setting of cycle         */
    ITU3_GRB = ITU3_BRB = 0;                /* PWM Setting of left  motor*/
    ITU4_GRA = ITU4_BRA = 0;                /* PWM Setting of right motor*/
    ITU4_GRB = ITU4_BRB = SERVO_CENTER;     /* PWM Setting of servo     */
    ITU_TOER = 0x38;

    /* Count start of ITU */
    ITU_STR = 0x09;
}

/************************************************************************/
/* ITU0 Interrupt process                                               */
/************************************************************************/
#pragma interrupt( interrupt_timer0 )
void interrupt_timer0( void )
{
    ITU0_TSR &= 0xfe;                   /* Flag clear                   */
    cnt0++;
    cnt1++;
}

/************************************************************************/
/* Timer main unit                                                      */
/* Argument Timer value 1=1ms                                           */
/************************************************************************/
void timer( unsigned long timer_set )
{
    cnt0 = 0;
    while( cnt0 < timer_set );
}

/************************************************************************/
/* Sensor state detection                                               */
/* AArgument    Mask value                                              */
/* RReturn value Sensor value                                           */
/************************************************************************/
unsigned char sensor_inp( unsigned char mask )
{
    unsigned char sensor;

    sensor  = P7DR;

    /* Since bit0 is for left and bit7 is for right in the new sensor board which is reversed  */
    /* to the previous sensor board, the bit has been replaced to maintain compatibility.   */
    sensor  = bit_change( sensor );     /* Bit replacement */
    sensor &= mask;

    return sensor;
}

/************************************************************************/
/* Crossline detection processing                                       */
/* Return value 0: no crossline 1: crossline exists                     */
/************************************************************************/
int check_crossline( void )
{
    unsigned char b;
    int ret;

    ret = 0;
    b = sensor_inp(MASK2_2);
    if( b==0x66 || b==0x64 || b==0x26 || b==0x62 || b==0x46 ) {
        ret = 1;
    }
    return ret;
}

/************************************************************************/
/* Reading of DIP switch value                                          */
/* Return value  Switch value  0 - 15                                   */
/************************************************************************/
unsigned char dipsw_get( void )
{
    unsigned char sw;

    sw  = ~P6DR;                        /* Reading of DIP switch */
    sw &= 0x0f;

    return  sw;
}

/************************************************************************/
/* Push switch value reading                                            */
/* Return value  Switch value ON: "1" and OFF: "0"                      */
/************************************************************************/
unsigned char pushsw_get( void )
{
    unsigned char sw;

    sw  = ~PBDR;                        /* Reading of port having a push switch */
    sw &= 0x01;

    return  sw;
}

/************************************************************************/
/* LED control                                                          */
/* Argument Switch value (LED1,LED0)=(bit1,bit0)  "0":OFF , "1":ON      */
/* Example  0x3->(LED1,LED0)=(ON,ON) : 0x2->(LED1,LED0)=(ON,OFF)        */
/************************************************************************/
void led_out( unsigned char led )
{
    unsigned char data;

    led = ~led;
    led <<= 6;
    data = PBDR & 0x3f;
    PBDR = data | led;
}

/************************************************************************/
/* Speed Control                                                        */
/* Argument  Left motor: -100 - 100 , Right motor: -100 - 100           */
/*           0:Stop,100:normal rotation 100%,-100:Reverse 100%          */
/************************************************************************/
void speed( int accele_l, int accele_r )
{
    unsigned char   sw_data;
    unsigned long   speed_max;

    sw_data  = dipsw_get()  +  5;         /* DIP switch read */
    speed_max = (unsigned long)(PWM_CYCLE-1) * sw_data / 20;

    /* Left motor */
    if( accele_l >= 0 ) {
        PBDR &= 0xfb;
        ITU3_BRB = speed_max * accele_l / 100;
    } else {
        PBDR |= 0x04;
        accele_l = -accele_l;
        ITU3_BRB = speed_max * accele_l / 100;
    }

    /* Right motor */
    if( accele_r >= 0 ) {
        PBDR &= 0xf7;
        ITU4_BRA = speed_max * accele_r / 100;
    } else {
        PBDR |= 0x08;
        accele_r = -accele_r;
        ITU4_BRA = speed_max * accele_r / 100;
    }
}

/************************************************************************/
/* Servo steering operation                                             */
/* Argument Servo operation angle: -90 ~ 90                             */
/*          -90:90 to the left turn , 0:straight ,90:90 to the right turn */
/************************************************************************/
void handle( int angle )
{
    ITU4_BRB = SERVO_CENTER - angle * HANDLE_STEP;
}

/************************************************************************/
/* Bit change                                                           */
/* Argument         Value to be changed                                 */
/* RReturn value    Changed value                                       */
/************************************************************************/
char unsigned bit_change( char unsigned in )
{
    unsigned char ret;
    int i;

    for( i=0; i<8; i++ ) {
        ret >>= 1;                      /* Right shift of return value  */
        ret |= in & 0x80;               /* Ret bit7 = in bit7           */
        in  <<= 1;                      /* Left shift of argument       */
    }
    return ret;
}

/************************************************************************/
/* end of file                                                          */
/************************************************************************/

⌨️ 快捷键说明

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