智能车制作
标题:
基于07组委会的(简化)四驱源程序代码(未完)(武汉工程大学kcmxi)
[打印本页]
作者:
制作爱好者
时间:
2011-1-31 09:59
标题:
基于07组委会的(简化)四驱源程序代码(未完)(武汉工程大学kcmxi)
/****************************************************************************/
/* "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;
复制代码
作者:
制作爱好者
时间:
2011-1-31 10:00
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;
}
复制代码
作者:
四月份先生
时间:
2012-8-2 14:17
感谢,收下!!
作者:
苏小贱
时间:
2012-12-12 21:49
顶一个
欢迎光临 智能车制作 (http://111.231.132.190/)
Powered by Discuz! X3.2