?? 34
字號(hào):
/****************************************Copyright (c)****************************************************
** Guangzhou ZHIYUAN electronics Co.,LTD.
**
** http://www.embedtools.com
**
**--------------File Info---------------------------------------------------------------------------------
** File Name: Mouse_Drive.c
** Last modified Date:
** Last Version:
** Description: 電腦鼠底層驅(qū)動(dòng)
**
**--------------------------------------------------------------------------------------------------------
** Created By:
** Created date:
** Version:
** Descriptions:
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Description:
**
*********************************************************************************************************/
/*********************************************************************************************************
包含頭文件
*********************************************************************************************************/
#include "Mouse_Drive.h"
/*********************************************************************************************************
定義全局變量
*********************************************************************************************************/
MAZECOOR GmcMouse = {0,0}; /* 保存電腦鼠當(dāng)前位置坐標(biāo) */
uint8 GucMouseDir = UP; /* 保存電腦鼠當(dāng)前方向 */
uint8 GucMapBlock[MAZETYPE][MAZETYPE] = {0}; /* GucMapBlock[x][y] */
/* x,橫坐標(biāo);y,縱坐標(biāo); */
/* bit3~bit0分別代表左下右上 */
/* 0:該方向無(wú)路,1:該方向有路 */
static __MOTOR __GmLeft = {0, 0, 0, 0, 0}; /* 定義并初始化左電機(jī)狀態(tài) */
static __MOTOR __GmRight = {0, 0, 0, 0, 0}; /* 定義并初始化右電機(jī)狀態(tài) */
static uint8 __GucMouseState = __STOP; /* 保存電腦鼠當(dāng)前運(yùn)行狀態(tài) */
static uint32 __GuiAccelTable[400] = {0}; /* 電機(jī)加減速各階段定時(shí)器值 */
static int32 __GiMaxSpeed = SEARCHSPEED; /* 保存允許運(yùn)行的最大速度 */
static uint8 __GucDistance[5] = {0}; /* 記錄傳感器狀態(tài) */
/*********************************************************************************************************
** Function name: __delay
** Descriptions: 延時(shí)函數(shù)
** input parameters: uiD :延時(shí)參數(shù),值越大,延時(shí)越久
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void __delay (uint32 uiD)
{
for (; uiD; uiD--);
}
/*********************************************************************************************************
** Function name: __rightMotorContr
** Descriptions: 右步進(jìn)電機(jī)驅(qū)動(dòng)時(shí)序
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void __rightMotorContr (void)
{
static int8 cStep = 0; /* 保存電機(jī)當(dāng)前位置 */
switch (__GmRight.cDir) {
case __MOTORGOAHEAD: /* 向前步進(jìn) */
cStep = (cStep + 1) % 8;
break;
case __MOTORGOBACK: /* 向后步進(jìn) */
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: /* B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHRA1 | __PHRA2 | __PHRB1 | __PHRB2,
__PHRA1 | __PHRA2);
break;
case 2: /* A1B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHRA1 | __PHRA2 | __PHRB1 | __PHRB2,
__PHRA1 | __PHRA2 | __PHRB2);
break;
case 3: /* A1 */
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: /* B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHRA1 | __PHRA2 | __PHRB1 | __PHRB2,
__PHRA2);
break;
case 6: /* A2B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHRA1 | __PHRA2 | __PHRB1 | __PHRB2,
__PHRA2 | __PHRB1 | __PHRB2);
break;
case 7: /* A2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHRA1 | __PHRA2 | __PHRB1 | __PHRB2,
__PHRB1 | __PHRB2);
break;
default:
break;
}
}
/*********************************************************************************************************
** Function name: __leftMotorContr
** Descriptions: 左步進(jìn)電機(jī)驅(qū)動(dòng)時(shí)序
** input parameters: __GmLeft.cDir :電機(jī)運(yùn)行方向
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void __leftMotorContr (void)
{
static int8 cStep = 0; /* 保存電機(jī)當(dāng)前位置 */
switch (__GmLeft.cDir) {
case __MOTORGOAHEAD: /* 向前步進(jìn) */
cStep = (cStep + 1) % 8;
break;
case __MOTORGOBACK: /* 向后步進(jìn) */
cStep = (cStep + 7) % 8;
break;
default:
break;
}
switch (cStep) {
case 0: /* A2B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2);
break;
case 1: /* B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLB1 | __PHLB2);
break;
case 2: /* A1B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA2 | __PHLB1 | __PHLB2);
break;
case 3: /* A1 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA2);
break;
case 4: /* A1B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA2 | __PHLB2);
break;
case 5: /* B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLB2);
break;
case 6: /* A2B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA1 | __PHLA2 | __PHLB2);
break;
case 7: /* A2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA1 | __PHLA2);
break;
default:
break;
}
}
/*********************************************************************************************************
** Function name: __speedContrR
** Descriptions: 右電機(jī)速度調(diào)節(jié)
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void __speedContrR (void)
{
int32 iDPusle;
iDPusle = __GmRight.uiPulse - __GmRight.uiPulseCtr; /* 統(tǒng)計(jì)電機(jī)還剩余的步數(shù) */
if (iDPusle <= __GmRight.iSpeed) {
__GmRight.iSpeed--;
} else { /* 非減速區(qū)間,則加速到最大值 */
if (__GmRight.iSpeed < __GiMaxSpeed) {
__GmRight.iSpeed++;
} else {
__GmRight.iSpeed--;
}
}
if (__GmRight.iSpeed < 0) { /* 設(shè)置速度下限 */
__GmRight.iSpeed = 0;
}
TimerLoadSet(TIMER0_BASE,TIMER_A,__GuiAccelTable[__GmRight.iSpeed]);/* 設(shè)置定時(shí)時(shí)間 */
}
/*********************************************************************************************************
** Function name: __speedContrL
** Descriptions: 左電機(jī)速度調(diào)節(jié)
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void __speedContrL (void)
{
int32 iDPusle;
iDPusle = __GmLeft.uiPulse - __GmLeft.uiPulseCtr; /* 統(tǒng)計(jì)電機(jī)還剩余的步數(shù) */
if (iDPusle <= __GmLeft.iSpeed) {
__GmLeft.iSpeed--;
} else { /* 非減速區(qū)間,則加速到最大值 */
if (__GmLeft.iSpeed < __GiMaxSpeed) {
__GmLeft.iSpeed++;
}
}
if (__GmLeft.iSpeed < 0) { /* 設(shè)置速度下限 */
__GmLeft.iSpeed = 0;
}
TimerLoadSet(TIMER1_BASE,TIMER_A,__GuiAccelTable[__GmLeft.iSpeed]); /* 設(shè)置定時(shí)時(shí)間 */
}
/*********************************************************************************************************
** Function name: Timer0A_ISR
** Descriptions: Timer0中斷服務(wù)函數(shù)
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void Timer0A_ISR(void)
{
static int8 n = 0,m = 0;
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); /* 清除定時(shí)器0中斷。 */
switch (__GmRight.cState) {
case __MOTORSTOP: /* 停止,同時(shí)清零速度和脈沖值 */
__GmRight.iSpeed = 0;
__GmRight.uiPulse = 0;
__GmRight.uiPulseCtr = 0;
break;
case __WAITONESTEP: /* 暫停一步 */
__GmRight.cState = __MOTORRUN;
break;
case __MOTORRUN: /* 電機(jī)運(yùn)行 */
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -