大红鹰娱乐官网风格切换切换到宽版
  • 14830阅读
  • 17回复

【申精】基于07组委会的(简化)四驱源程序代码(未完)(武汉工程大学kcmxi) [复制链接]

上一主题 下一主题
离线kcmxi
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2009-10-08
— 本帖被 admin 从 H8/H8S 移动到本区(2009-12-25) —
/****************************************************************************/
/*                        "kit07_4wd.c"       (武汉工程大学-copyright )                 */
/*                                                                                      2009.07                      */
/****************************************************************************/

/*======================================*/
/*                         Include                                                 */
/*======================================*/
#include    <machine.h>
#include    "h8_3048.h"


#define         PWM_CYCLE       49151   /* PWM 16ms       */
                                                                     /* PWM_CYCLE =              */
                                                                     /*      16[ms] / 325.5[ns]  */
                                                                     /*               = 49152    */
#define         F_PWM_CYCLE       3071                                         
#define         SERVO_CENTER    5000   
#define         HANDLE_STEP         26     



#define         MASK2_2         0x66   
#define         MASK2_0         0x60   
#define         MASK0_2         0x06   
#define         MASK3_3         0xe7   
#define         MASK0_3         0x07   
#define         MASK3_0         0xe0   
#define         MASK4_0         0xf0   
#define         MASK0_4         0x0f   
#define         MASK4_4         0xff   

#define         speed_r(x,y)    speed(x,y)  /* speed  = speed_r     */
#define         speed2_r(x,y)   speed2(x,y) /* speed2 = speed2_r    */

void init( void );
void timer( unsigned long timer_set );
int check_crossline( void );
int check_rightline( void );
int check_leftline( void );
unsigned char sensor_inp( unsigned char mask );
unsigned char dipsw_get( void );
unsigned char pushsw_get( void );
unsigned char startbar_get( void );
void led_out( unsigned char led );
void speed( int accele_l, int accele_r );
void speed2( int accele_l, int accele_r );
void speed_f( int accele_l, int accele_r );
void speed2_f( int accele_l, int accele_r );
void handle( int angle );

unsigned long   cnt0;                  
unsigned long   cnt1;                 
int             pattern;               

void main( void )
{
    int     i;
    int     m1, m2, m3, m4;
    unsigned char c;

    init();                            
    set_ccr( 0x00 );                 

    handle( 0 );
    speed( 0, 0 );

    while( 1 ) {
        if( cnt1 < 1000 ) {
            led_out( 0x1 );
            i = 50;
        } else if( cnt1 < 2000 ) {
            led_out( 0x0 );
            i = 0;
        } else if( cnt1 < 3000 ) {
            led_out( 0x2 );
            i = -50;
        } else if( cnt1 < 4000 ) {
            led_out( 0x0 );
            i = 0;
        } else {
            cnt1 = 0;
        }
        c = dipsw_get();
        if( c & 0x8) m1 = i; else m1 = 0;
        if( c & 0x4) m2 = i; else m2 = 0;
        if( c & 0x2) m3 = i; else m3 = 0;
        if( c & 0x1) m4 = i; else m4 = 0;
        speed2_f( m1, m2 );         
        speed2_r( m3, m4 );         
    }

    while( 1 ) {
    switch( pattern ) {

       case 0:
        if( pushsw_get() ) {
            pattern = 1;
            cnt1 = 0;
            break;
        }
        if( cnt1 < 100 ) {           
            led_out( 0x1 );
        } else if( cnt1 < 200 ) {
            led_out( 0x2 );
        } else {
            cnt1 = 0;
        }
        break;
    case 1:
     
        if( !startbar_get() ) {
            
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
            break;
        }
        if( cnt1 < 50 ) {      
            led_out( 0x1 );
        } else if( cnt1 < 100 ) {
            led_out( 0x2 );
        } else {
            cnt1 = 0;
        }
        break;
    case 11:
    
        if( check_crossline() ) {      
            pattern = 21;
            break;
        }
        if( check_rightline() ) {     
            pattern = 51;
            break;
        }
        if( check_leftline() ) {      
            pattern = 61;
            break;
        }
        switch( sensor_inp(MASK3_3) ) {
            case 0x00:
                
                handle( 0 );
                speed( 100 ,100 );
                break;
            case 0x04:
              
                handle( 5 );
                speed( 100 ,100 );
                break;
            case 0x06:
          
                handle( 10 );
                speed( 80 ,67 );
                break;
            case 0x07:
             
                handle( 15 );
                speed( 50 ,38 );
                break;
            case 0x03:
                handle( 25 );
                speed( 30 ,19 );
                pattern = 12;
                break;
            case 0x20:
                handle( -5 );
                speed( 100 ,100 );
                break;
            case 0x60:
                
                handle( -10 );
                speed( 67 ,80 );
                break;
            case 0xe0:
               handle( -15 );
                speed( 38 ,50 );
                break;
            case 0xc0:
                
                handle( -25 );
                speed( 19 ,30 );
                pattern = 13;
                break;
            default:
                break;
        }
        break;
    case 12:
               if( check_crossline() ) {      
            pattern = 21;
            break;
        }
        if( check_rightline() ) {       
            pattern = 51;
            break;
        }
        if( check_leftline() ) {       
            pattern = 61;
            break;
        }
        if( sensor_inp(MASK3_3) == 0x06 ) {
            pattern = 11;
        }
        break;
    case 13:
                if( check_crossline() ) {                pattern = 21;
            break;
        }
        if( check_rightline() ) {       
            pattern = 51;
            break;
        }
        if( check_leftline() ) {        
            pattern = 61;
            break;
        }
        if( sensor_inp(MASK3_3) == 0x60 ) {
            pattern = 11;
        }
        break;
    case 21:
        
        led_out( 0x3 );
        handle( 0 );
        speed( 0 ,0 );
        pattern = 22;
        cnt1 = 0;
        break;
    case 22:
        
        if( cnt1 > 100 ) {
            pattern = 23;
            cnt1 = 0;
        }
        break;
    case 23:
        
        if( sensor_inp(MASK4_4)==0xf8 ) {
            
            led_out( 0x1 );
            handle( -38 );
            speed( 10 ,50 );
            pattern = 31;
            cnt1 = 0;
            break;
        }
        if( sensor_inp(MASK4_4)==0x1f ) {
                        led_out( 0x2 );
            handle( 38 );
            speed( 50 ,10 );
            pattern = 41;
            cnt1 = 0;
            break;
        }
        switch( sensor_inp(MASK3_3) ) {
            case 0x00:
          
                handle( 0 );
                speed( 40 ,40 );
                break;
            case 0x04:
            case 0x06:
            case 0x07:
            case 0x03:
              
                handle( 8 );
                speed( 40 ,35 );
                break;
            case 0x20:
            case 0x60:
            case 0xe0:
            case 0xc0:
          
                handle( -8 );
                speed( 35 ,40 );
                break;
        }
        break;
    case 31:
      
        if( cnt1 > 200 ) {
            pattern = 32;
            cnt1 = 0;
        }
        break;
    case 32:
        
        if( sensor_inp(MASK3_3) == 0x60 ) {
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
        }
        break;
    case 41:
             if( cnt1 > 200 ) {
            pattern = 42;
            cnt1 = 0;
        }
        break;
    case 42:
     
        if( sensor_inp(MASK3_3) == 0x06 ) {
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
        }
        break;
    case 51:
  
        led_out( 0x2 );
        handle( 0 );
        speed( 0 ,0 );
        pattern = 52;
        cnt1 = 0;
        break;
    case 52:
    
        if( cnt1 > 100 ) {
            pattern = 53;
            cnt1 = 0;
        }
        break;
    case 53:
     
        if( sensor_inp(MASK4_4) == 0x00 ) {
            handle( 15 );
            speed( 40 ,31 );
            pattern = 54;
            cnt1 = 0;
            break;
        }
        switch( sensor_inp(MASK3_3) ) {
            case 0x00:
           
                handle( 0 );
                speed( 40 ,40 );
                break;
            case 0x04:
            case 0x06:
            case 0x07:
            case 0x03:
           
                handle( 8 );
                speed( 40 ,35 );
                break;
            case 0x20:
            case 0x60:
            case 0xe0:
            case 0xc0:
         
                handle( -8 );
                speed( 35 ,40 );
                break;
            default:
                break;
        }
        break;
    case 54:
       
        if( sensor_inp( MASK4_4 ) == 0x3c ) {
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
        }
        break;
    case 61:
      
        led_out( 0x1 );
        handle( 0 );
        speed( 0 ,0 );
        pattern = 62;
        cnt1 = 0;
        break;
    case 62:
      
        if( cnt1 > 100 ) {
            pattern = 63;
            cnt1 = 0;
        }
        break;
    case 63:
      
        if( sensor_inp(MASK4_4) == 0x00 ) {
            handle( -15 );
            speed( 31 ,40 );
            pattern = 64;
            cnt1 = 0;
            break;
        }
        switch( sensor_inp(MASK3_3) ) {
            case 0x00:
            
                handle( 0 );
                speed( 40 ,40 );
                break;
            case 0x04:
            case 0x06:
            case 0x07:
            case 0x03:
            
                handle( 8 );
                speed( 40 ,35 );
                break;
            case 0x20:
            case 0x60:
            case 0xe0:
            case 0xc0:
              
                handle( -8 );
                speed( 35 ,40 );
                break;
            default:
                break;
        }
        break;
    case 64:
        
        if( sensor_inp( MASK4_4 ) == 0x3c ) {
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
        }
        break;
    default:
       
        pattern = 0;
        break;
    }
    }
}

void init( void )
{
   
    P1DDR = 0xff;
    P2DDR = 0xff;
    P3DDR = 0xff;
    P4DDR = 0xff;
    P5DDR = 0xff;
    P6DDR = 0xf0;                       
    P8DDR = 0xff;
    P9DDR = 0xf7;                       
    PADDR = 0xff;
    PBDR  = 0xc0;
    PBDDR = 0xfe;                      
 
    ITU0_TCR = 0x23;                   
    ITU0_GRA = F_PWM_CYCLE;           
    ITU0_GRB = 0;                      
    ITU0_IER = 0x01;                   
    ITU1_TCR = 0x23;                
    ITU1_GRA = F_PWM_CYCLE;            
    ITU1_GRB = 0;                     
    
    ITU3_TCR = 0x23;
    ITU_FCR  = 0x3e;
    ITU3_GRA = PWM_CYCLE;           
    ITU3_GRB = ITU3_BRB = 0;          
    ITU4_GRA = ITU4_BRA = 0;           
    ITU4_GRB = ITU4_BRB = SERVO_CENTER;
    ITU_TOER = 0x38;

    ITU_MDR = 0x03;                   
    ITU_STR = 0x1f;                   
}

#pragma interrupt( interrupt_timer0 )
void interrupt_timer0( void )
{
    ITU0_TSR &= 0xfe;                 
    cnt0++;
    cnt1++;
}

void timer( unsigned long timer_set )
{
    cnt0 = 0;
    while( cnt0 < timer_set );
}

unsigned char sensor_inp( unsigned char mask )
{
    unsigned char sensor;
    sensor  = ~P7DR;
    sensor  &= 0xef;
    if( sensor & 0x08 ) sensor |= 0x10;
    sensor &= mask;
    return sensor;
}

int check_crossline( void )
{
    unsigned char b;
    int ret;
    ret = 0;
    b = sensor_inp(MASK3_3);
    if( b==0xe7 ) {
        ret = 1;
    }
    return ret;
}

int check_rightline( void )
{
    unsigned char b;
    int ret;
    ret = 0;
    b = sensor_inp(MASK4_4);
    if( b==0x1f ) {
        ret = 1;
    }
    return ret;
}

int check_leftline( void )
{
    unsigned char b;
    int ret;
    ret = 0;
    b = sensor_inp(MASK4_4);
    if( b==0xf8 ) {
        ret = 1;
    }
    return ret;
}

unsigned char dipsw_get( void )
{
    unsigned char sw;
    sw  = ~P6DR;                      
    return  sw;
}

unsigned char pushsw_get( void )
{
    unsigned char sw;
    sw  = ~PBDR;                     
    sw &= 0x01;
    return  sw;
}

1条评分
admin 铜币 +5 优秀文章 2009-10-08
离线renesasrulz

只看该作者 沙发  发表于: 2009-10-08
如果添加些注释和讲解的话,那就更完美了

广告招租

email: admin@renesas-mcu.com
QQ: 2020832894
离线kcmxi

只看该作者 板凳  发表于: 2009-10-08
回 1楼(admin) 的帖子
呵呵 有时间写上去
离线快乐水牛

只看该作者 地板  发表于: 2009-10-09
要注意代码风格,变量命名,括号,语法规则等。
主循环太长了,可以设置几个模块,分别调用。
注释也没有。。。

        if( c & 0x8) m1 = i; else m1 = 0;
        if( c & 0x4) m2 = i; else m2 = 0;
        if( c & 0x2) m3 = i; else m3 = 0;
        if( c & 0x1) m4 = i; else m4 = 0;

像这种代码在企业里是要枪毙的。
离线darksword6
只看该作者 4楼 发表于: 2009-10-09
把省下的也发一下啊  别吊人胃口吗!
离线求真
只看该作者 5楼 发表于: 2009-10-14
支持。。。
离线求真
只看该作者 6楼 发表于: 2009-11-29
四区 该 怎么弄???
离线kcmxi

只看该作者 7楼 发表于: 2009-11-29
回 6楼(求真) 的帖子
你指的是哪方面?
离线求真
只看该作者 8楼 发表于: 2009-11-29
回 7楼(kcmxi) 的帖子
车体 怎么改造?该加上什么???
要 增加 什么函数???
离线happyalan

只看该作者 9楼 发表于: 2010-05-25
H8?靠,很贵的,据说Renesas中已经没有H8的未来位置了.
博聚网