📄 lpc_lib_motor.c
字号:
/*
**********************************************************************************************
*
* Embest Info&Tech Co., Ltd. All rights reserved.
* www.embedinfo.com
*
*file: xxx.c/xxx.h
*author: embest
*establish: 2006.xx.xx
*modify: xx
*notes: xx
**********************************************************************************************
*/
/*
**********************************************************************************************
* include files
**********************************************************************************************
*/
#include "..\..\com\lpc_lib_motor\lpc_lib_motor.h"
/*
**********************************************************************************************
* extern function/variable declare
**********************************************************************************************
*/
/*
**********************************************************************************************
* global variable define
**********************************************************************************************
*/
/*
**********************************************************************************************
* name: motor_init
* func: initialize the motor relative pins
* para: none
* ret: none
* modify:
* comment:
**********************************************************************************************
*/
void motor_init(void)
{
INT32U i;
//set pwm to gpio :pwm1,pwm3,pwm4,pwm6,
i = PINSEL0;
i &= ~((0x3)+(0x3<<2)+(0x3<<16)+(0x3<<18));
PINSEL0 = i;
//set pwm5 to gpio
i = PINSEL1;
i &= ~(0x3<<10);
PINSEL1 = i;
//set these pin to output
i = IODIR0;
i |= ((0x1)+(0x1<<1)+(0x1<<8)+(0x1<<9)+(0x1<<21));
IODIR0=i;
//pwm1_pin:p0.0, pwm3_pin:p0.1, pwm4_pin:p0.8, pwm6_pin:p0.9, pwm5_pin:p0.21
i = IOCLR0;
i |= ((0x1)+(0x1<<1)+(0x1<<8)+(0x1<<9)+(0x1<<21));
IOCLR0 = i;
//set motor pins to gpio
i=PINSEL0;
i&=0x003fffff;
PINSEL0=i;
//set output
i=IODIR0;
i|=0x0000f800;
IODIR0=i;
//motor1:p0.14 motor2:p0.15 motor3:p0.11 motor4:p0.12 motor5:p0.13
i=IOSET0;
i |= ((0x1<<14)+(0x1<<15)+(0x1<<11)+(0x1<<12)+(0x1<<13));
IOSET0=i;
//open motor module
spi_extend_set(0,module_c_MOTOR_EN);
}
/*
**********************************************************************************************
* name: motor_close
* func: off all the motor relative pins
* para: none
* ret: none
* modify:
* comment:
**********************************************************************************************
*/
void motor_close(void)
{ //off all motor and pwm pins
INT32U i;
//set value 0 to pwm pins,that as close value
//pwm1_pin:p0.0, pwm3_pin:p0.1, pwm4_pin:p0.8, pwm6_pin:p0.9, pwm5_pin:p0.21
i = IOCLR0;
i |= ((0x1)+(0x1<<1)+(0x1<<8)+(0x1<<9)+(0x1<<21));
IOCLR0 = i;
//set value 1 to motor pins
//motor1:p0.14 motor2:p0.15 motor3:p0.11 motor4:p0.12 motor5:p0.13
i=IOSET0;
i |= ((0x1<<14)+(0x1<<15)+(0x1<<11)+(0x1<<12)+(0x1<<13));
IOSET0=i;
//close motor module
spi_extend_set(1,module_c_MOTOR_EN);
}
/*
**********************************************************************************************
* name: motor_sel_en
* func: enable the selected motor
* para: onoff: 0 off, 1 on; motor:motor1,motor2,motor3,motor4,motor5
* ret: none
* modify:
* comment:
**********************************************************************************************
*/
void motor_sel_en(INT8U onoff,motor_sel motor)
{ //onoff: 0 off, 1 on
INT32U i,value=0;
switch(motor)
{
case motor1: value=0x1<<14;
break;
case motor2: value=0x1<<15;
break;
case motor3: value=0x1<<11;
break;
case motor4: value=0x1<<12;
break;
case motor5: value=0x1<<13;
break;
default: break;
}
if (onoff==0) //off motor
{
i=IOSET0;
i|=value;
IOSET0=i;
}
else //on motor
{
i=IOCLR0;
i|=value;
IOCLR0=i;
}
}
/*
**********************************************************************************************
* name: pwm_pin_en
* func: enable the selected pwm pin
* para: onoff: 0 off, 1 on; pwm:select the pwm pins
* ret: none
* modify:
* comment:
**********************************************************************************************
*/
void pwm_pin_en(INT8U onoff,pwm_pin pwm)
{ //onoff: 0 off, 1 on
INT32U i,value=0;
switch(pwm)
{
case pwm1_pin: value=0x1;
break;
case pwm3_pin: value=0x1<<1;
break;
case pwm4_pin: value=0x1<<8;
break;
case pwm5_pin: value=0x1<<21;
break;
case pwm6_pin: value=0x1<<9;
break;
default: break;
}
if (onoff==0) //off motor
{
i=IOCLR0;
i|=value;
IOCLR0=i;
}
else //on motor
{
i=IOSET0;
i|=value;
IOSET0=i;
}
}
/*
**********************************************************************************************
* name: a_set
* func: setup 'a' phase, onoff and the polarity
* para: //onoff: 0 off,1 on; polarity:0 positive,1 negative
* ret: none
* modify:
* comment:
a: q4 q8 -a: q5 q7
motor5 pwm3 motor2 pwm6
**********************************************************************************************
*/
void a_set(INT8U onoff,INT8U polarity)
{ //onoff: 0 off,1 on; polarity:0 positive,1 negative
if(polarity==0)
{
motor_sel_en(onoff,motor5);
pwm_pin_en(onoff,pwm3_pin);
}
else
{
motor_sel_en(onoff,motor2);
pwm_pin_en(onoff,pwm6_pin);
}
}
/*
**********************************************************************************************
* name: b_set
* func: setup 'b' phase, onoff and the polarity
* para: //onoff: 0 off,1 on; polarity:0 positive,1 negative
* ret: none
* modify:
* comment:
b: q10 q13 -b: q11 q12
motor4 pwm4 motor3 pwm5
**********************************************************************************************
*/
void b_set(INT8U onoff,INT8U polarity)
{ //onoff: 0 off,1 on; polarity:0 positive,1 negative
if(polarity==0)
{
motor_sel_en(onoff,motor4);
pwm_pin_en(onoff,pwm4_pin);
}
else
{
motor_sel_en(onoff,motor3);
pwm_pin_en(onoff,pwm5_pin);
}
}
/*
**********************************************************************************************
* name: motor_test
* func: direct motor or step motor can be selected
* para: motor_type motor: dir_motor ,step_motor
direct_sel dir: select direction with deasil,contrarotate
* ret: none
* modify:
* comment:
**********************************************************************************************
*/
void motor_test(motor_type motor,direct_sel dir)
{ //motor type and rotating direction
INT32U i,delay_time;
//dir_motor
if(motor==dir_motor)
{
//deasil rotate
if(dir==deasil)
{
motor_sel_en(1,motor1);
pwm_pin_en(1,pwm3_pin);
}
//contrarotate
else
{
motor_sel_en(1,motor2);
pwm_pin_en(1,pwm1_pin);
}
}
//step_motor
else
{
//deasil rotate
if(dir==deasil)
{
//a ba b -ab -a -b-a -b a-b
//a
b_set(0,1);
for(delay_time=0;delay_time<1000;delay_time++);
//ba
b_set(1,0);
for(delay_time=0;delay_time<1000;delay_time++);
//b
a_set(0,0);
for(delay_time=0;delay_time<1000;delay_time++);
//-ab
a_set(1,1);
for(delay_time=0;delay_time<1000;delay_time++);
//-a
b_set(0,0);
for(delay_time=0;delay_time<1000;delay_time++);
//-b-a
b_set(1,1);
for(delay_time=0;delay_time<1000;delay_time++);
//-b
a_set(0,1);
for(delay_time=0;delay_time<1000;delay_time++);
//a-b
a_set(1,0);
for(delay_time=0;delay_time<1000;delay_time++);
}
//contrarotate
else
{
//a a-b -b -b-a -a -ab b ba contrarotate
//a
b_set(0,0);
for(delay_time=0;delay_time<1000;delay_time++);
//a-b
b_set(1,1);
for(delay_time=0;delay_time<1000;delay_time++);
//-b
a_set(0,0);
for(delay_time=0;delay_time<1000;delay_time++);
//-b-a
a_set(1,1);
for(delay_time=0;delay_time<1000;delay_time++);
//-a
b_set(0,1);
for(delay_time=0;delay_time<1000;delay_time++);
//-ab
b_set(1,0);
for(delay_time=0;delay_time<1000;delay_time++);
//b
a_set(0,1);
for(delay_time=0;delay_time<1000;delay_time++);
//ba
a_set(1,0);
for(delay_time=0;delay_time<1000;delay_time++);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -