📄 motor_control.c
字号:
#include "derivative.h" /* include peripheral declarations */
#define LOWER 0
#define UPPER 1
#define MIDDLE 2
#define OFF 0
#define ON 1
static byte motor_pos;
void delay(int time){
int i;
for(i=0;i<time;i++);
}
void LED1Dis(int isOn)
{
if(isOn){
PTED_PTED4 = 1;
PTEDD_PTEDD4 = 1;
}
else{
PTED_PTED4 = 0;
PTEDD_PTEDD4 = 1;
}
}
void LED2Dis(int isOn)
{
if(isOn){
PTFD_PTFD1 = 1;
PTFDD_PTFDD1 = 1;
}
else{
PTFD_PTFD1 = 0;
PTFDD_PTFDD1 = 1;
}
}
void motorCtrIOInit(){
PTCD_PTCD7 = 0;
PTDD_PTDD2 = 0;
PTCDD_PTCDD7 = 1;
PTDDD_PTDDD2 = 1;
PTCPE_PTCPE1 = 1;
PTCDD_PTCDD1 = 0;
}
void motorUp() {
motorCtrIOInit();
if (!(JudgeMotorPos() == UPPER)){
PTCD_PTCD7 = 0;
PTDD_PTDD2 = 1;
}
}
void motorDown(){
motorCtrIOInit();
if (!(JudgeMotorPos() == LOWER)){
PTCD_PTCD7 = 1;
PTDD_PTDD2 = 0;
}
}
void motorStop(){
PTCD_PTCD7 = 0;
PTDD_PTDD2 = 0;
}
int JudgeMotorPos(){
//judge motor pos
PTCPE_PTCPE1 = 1;
PTCDD_PTCDD1 = 0;
PTCD_PTCD6 = 1;
PTGD_PTGD7 = 1;
PTCDD_PTCDD6 = 1;
PTGDD_PTGDD7 = 1;
delay(0xFF);
if(PTCD_PTCD1 == 0)
{
PTCD_PTCD6 = 0;
delay(0xFF);
if(PTCD_PTCD1 == 0){
motor_pos = LOWER;
PTCD_PTCD6 = 1;
return LOWER;
}
else {
motor_pos = UPPER;
PTCD_PTCD6 = 1;
return UPPER;
}
}
else{
motor_pos = MIDDLE;
return MIDDLE;
}
}
void motorCtr()
{
motorCtrIOInit();
// motor at upper/ lower position
if(JudgeMotorPos() == UPPER){
PTCD_PTCD6 = 0;
delay(0xFF);
while(PTCD_PTCD1 == 1){
}
PTCD_PTCD6 = 1;
}
else if(JudgeMotorPos() == MIDDLE){
while(PTCD_PTCD1 == 1){
}
}
// motor motion from lowerPos to upperPos
delay(0xffff);
PTGD_PTGD7 = 0;
delay(0xff);
while (PTCD_PTCD1 == 1){
}
PTGD_PTGD7 = 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -