?? 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; /* 電機已轉動的步數 */
uint32 uiSpeed; /* 電機轉動速度 */
};
typedef struct motor MOTOR;
/*********************************************************************************************************
定義全局變量
*********************************************************************************************************/
static MOTOR GmRight = {STOP, GOAHEAD, 0, 0, 0}; /* 定義并初始化右電機狀態 */
/*********************************************************************************************************
** 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;
}
}
/*********************************************************************************************************
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -