📄 main.c
字号:
/****************************************Copyright (c)****************************************************
** Guangzhou ZHIYUAN electronics Co.,LTD.
**
** http://www.embedtools.com
**
**--------------File Info---------------------------------------------------------------------------------
** File Name: main.c
** Last modified Date: 2008/01/15
** Last Version: V1.00
** Description: MicroMouse615上的步进电机实验
**
**--------------------------------------------------------------------------------------------------------
** Created By: 廖茂刚
** Created date:
** Version:
** Descriptions:
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Description:
**
*********************************************************************************************************/
/*********************************************************************************************************
包含头文件
*********************************************************************************************************/
#include "hw_memmap.h"
#include "hw_ints.h"
#include "hw_types.h"
#include "interrupt.h"
#include "gpio.h"
#include "sysctl.h"
#include "Systick.h"
#include "Timer.h"
#include "Pwm.h"
#include "Type.h"
/*********************************************************************************************************
PC端口定义
*********************************************************************************************************/
#define KEY GPIO_PIN_4 /* 按键连接的端口 */
/*********************************************************************************************************
PD端口定义
*********************************************************************************************************/
#define PHRA1 GPIO_PIN_0 /* 右侧步进电机的A1相 */
#define PHRA2 GPIO_PIN_1 /* 右侧步进电机的A2相 */
#define PHRB1 GPIO_PIN_2 /* 右侧步进电机的B1相 */
#define PHRB2 GPIO_PIN_3 /* 右侧步进电机的B2相 */
/*********************************************************************************************************
常量宏定义--电机状态
*********************************************************************************************************/
#define STOP 0 /* 电机停止 */
#define RUN 1 /* 电机运行 */
/*********************************************************************************************************
常量宏定义--电机运行方向
*********************************************************************************************************/
#define GOAHEAD 0 /* 电机前进 */
#define GOBACK 1 /* 电机后退 */
/*********************************************************************************************************
结构体定义
*********************************************************************************************************/
struct motor {
int8 cState; /* 电机运行状态 */
int8 cDir; /* 电机运行方向 */
uint32 uiPulse; /* 电机需要转动的步数 */
uint32 uiPulseCtr; /* 电机已转动的步数 */
int32 iSpeed; /* 电机转动速度 */
};
typedef struct motor MOTOR;
/*********************************************************************************************************
定义全局变量
*********************************************************************************************************/
static MOTOR GmRight = {STOP, GOAHEAD, 0, 0, 0}; /* 定义并初始化右电机状态 */
static uint32 GuiAccelTable[300] = {0}; /* 电机加减速各阶段定时器值 */
static int32 GiMaxSpeed = 200; /* 保存允许运行的最大速度 */
/*********************************************************************************************************
** Function name: delay
** Descriptions: 延时函数
** input parameters: uiD :延时参数,值越大,延时越久
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void delay (uint32 uiD)
{
for (; uiD; uiD--);
}
/*********************************************************************************************************
** Function name: rightMotorContr
** Descriptions: 右步进电机驱动时序
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void rightMotorContr (void)
{
static int8 cStep = 0; /* 保存电机当前位置 */
switch (GmRight.cDir) {
case GOAHEAD: /* 向前步进 */
cStep = (cStep + 1) % 8;
break;
case GOBACK: /* 向后步进 */
cStep = (cStep + 7) % 8;
break;
default:
break;
}
switch (cStep) {
case 0: /* A2B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHRA1 | PHRA2 | PHRB1 | PHRB2,
PHRA1 | PHRA2 | PHRB1 | PHRB2);
break;
case 1: /* A2 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHRA1 | PHRA2 | PHRB1 | PHRB2,
PHRA1 | PHRA2);
break;
case 2: /* A2B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHRA1 | PHRA2 | PHRB1 | PHRB2,
PHRA1 | PHRA2 | PHRB2);
break;
case 3: /* B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHRA1 | PHRA2 | PHRB1 | PHRB2,
PHRB2);
break;
case 4: /* A1B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHRA1 | PHRA2 | PHRB1 | PHRB2,
PHRA2 | PHRB2);
break;
case 5: /* A1 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHRA1 | PHRA2 | PHRB1 | PHRB2,
PHRA2);
break;
case 6: /* A1B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHRA1 | PHRA2 | PHRB1 | PHRB2,
PHRA2 | PHRB1 | PHRB2);
break;
case 7: /* B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
PHRA1 | PHRA2 | PHRB1 | PHRB2,
PHRB1 | PHRB2);
break;
default:
break;
}
}
/*********************************************************************************************************
** Function name: __speedContrR
** Descriptions: 右电机速度调节
** input parameters: 无
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void speedContrR (void)
{
int32 iDPusle;
iDPusle = GmRight.uiPulse - GmRight.uiPulseCtr; /* 统计电机还剩余的步数 */
if (iDPusle <= GmRight.iSpeed) {
GmRight.iSpeed--;
} else { /* 非减速区间,则加速到最大值 */
if (GmRight.iSpeed < GiMaxSpeed) {
GmRight.iSpeed++;
} else {
GmRight.iSpeed--;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -