📄 kit05test.c
字号:
} else {
speed( 0, 0 );
}
break;
/* Direct Advance test Advances by PWM 50%, Stop after 5 seconds */
case 13:
if( cnt1 < 2000 ) {
speed( 0, 0 );
} else if( cnt1 < 7000 ) {
speed( 50, 50 );
} else {
speed( 0, 0 );
}
break;
/* Direct Advance test Advances by PWM 100%, Stop after 2 seconds */
case 14:
if( cnt1 < 2000 ) {
speed( 0, 0 );
} else if( cnt1 < 4000 ) {
speed( 100, 100 );
} else {
speed( 0, 0 );
}
break;
/* Direct Advance test Advances by PWM 100%, Stop after 5 seconds */
case 15:
if( cnt1 < 2000 ) {
speed( 0, 0 );
} else if( cnt1 < 7000 ) {
speed( 100, 100 );
} else {
speed( 0, 0 );
}
break;
/* If neither */
default:
break;
}
}
}
/************************************************************************/
/* Module built-in H8/3048F Initialization */
/************************************************************************/
void init( void )
{
/* Input/Output setting of Port */
P1DDR = 0xff;
P2DDR = 0xff;
P3DDR = 0xff;
P4DDR = 0xff;
P5DDR = 0xff;
P6DDR = 0xf0; /* DIP SW on CPU board */
P8DDR = 0xff;
P9DDR = 0xf7; /* Signal communication port */
PADDR = 0xff;
PBDR = 0xc0;
PBDDR = 0xfe; /* Motor Drive Board Vol.3 */
/* 仸 There is no input/output setting since Port 7 which is connected with Sensor board ,is exclu for input. */
/* ITU0 Interrupt of each 1ms */
ITU0_TCR = 0x23;
ITU0_GRA = TIMER_CYCLE;
ITU0_IER = 0x01;
/* ITU3,4 for reset synchronization PWM mode and they are for right and 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 Processing */
/************************************************************************/
#pragma interrupt( interrupt_timer0 )
void interrupt_timer0( void )
{
ITU0_TSR &= 0xfe; /* Flag clear */
cnt0++;
cnt1++;
}
/************************************************************************/
/* Sensor state detection */
/* Argument mask value */
/* Return value Sensor value */
/************************************************************************/
unsigned char sensor_inp( unsigned char mask )
{
unsigned char sensor;
sensor = P7DR;
/* For the new sensor board, the leftmost is bit0 and rightmost is bit7 and this */
/* is opposite to the previous sensor board. The bit is replaced in order to maintain compatibility. */
sensor = bit_change( sensor ); /* replace bit */
sensor &= mask;
return sensor;
}
/************************************************************************/
/* DIP switch value reading */
/* Return value Switch value 0 - 15 */
/************************************************************************/
unsigned char dipsw_get( void )
{
unsigned char sw;
sw = ~P6DR; /* DIP switch read */
sw &= 0x0f;
return sw;
}
/************************************************************************/
/* Push switch value read */
/* Return value Switch value ON:1, OFF:0 */
/************************************************************************/
unsigned char pushsw_get( void )
{
unsigned char sw;
sw = ~PBDR; /* Port reading with switch */
sw &= 0x01;
return sw;
}
/************************************************************************/
/* LED contorl */
/* Argument switch value LED0:bit0 LED1:bit1 "0":OFF "1":ON */
/* Example) 0x3仺LED1:ON LED0:ON 0x2仺LED1:ON LED0: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 rotation100%, -100: reverse100% */
/************************************************************************/
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 handle operation (Handle means steering) */
/* Argument Servo operation angle丗-90乣+90 */
/* -90: 90亱 to left丄0: straight, 0: 90亱 to right rotation */
/************************************************************************/
void handle( int angle )
{
ITU4_BRB = SERVO_CENTER - angle * HANDLE_STEP;
}
/************************************************************************/
/* Bit replace */
/* Argument Replaced value */
/* Return value Value after replacing */
/************************************************************************/
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 + -