亚洲春色中文字幕久久久-三上亚,一吻二脱三床四吻胸,国产真实伦对白视频全集,在线毛片观看,精品成品入口黄网,国产毛aⅴ片久久久,亚洲AV色香蕉一区二区三区老师,萧皇后A级艳片,色情日本视频更新,99久久亚洲精品日本无码
標(biāo)題:
電腦鼠程序 MicroMouse615上的無(wú)記憶功能的走迷宮實(shí)驗(yàn)
[打印本頁(yè)]
作者:
sbbrqfgu
時(shí)間:
2018-4-2 21:39
標(biāo)題:
電腦鼠程序 MicroMouse615上的無(wú)記憶功能的走迷宮實(shí)驗(yàn)
這個(gè)是我們電腦鼠的程序,可以用IAR下的流明諾瑞驅(qū)動(dòng)庫(kù)進(jìn)行編譯調(diào)試
單片機(jī)源程序如下:
/****************************************Copyright (c)****************************************************
** Guangzhou ZHIYUAN electronics Co.,LTD.
**--------------File Info---------------------------------------------------------------------------------
** File Name: main.c
** Last modified Date: 2008/02/14
** Last Version: V1.00
** Description: MicroMouse615上的無(wú)記憶功能的走迷宮實(shí)驗(yàn)
**
**--------------------------------------------------------------------------------------------------------
** 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 "adc.h"
#include "Pwm.h"
#include "Type.h"
#include "Zlg7289.h"
/*********************************************************************************************************
PB端口定義
*********************************************************************************************************/
#define IRSEND_BEVEL GPIO_PIN_0 /* 驅(qū)動(dòng)斜角紅外的調(diào)制信號(hào) */
#define LEFTSIDE GPIO_PIN_1 /* 左方傳感器輸出的信號(hào) */
#define FRONTSIDE_L GPIO_PIN_2 /* 左前方傳感器輸出的信號(hào) */
#define FRONTSIDE GPIO_PIN_3 /* 前方傳感器輸出的信號(hào) */
#define FRONTSIDE_R GPIO_PIN_4 /* 右前方傳感器輸出的信號(hào) */
#define RIGHTSIDE GPIO_PIN_5 /* 右方傳感器輸出的信號(hào) */
/*********************************************************************************************************
PC端口定義
*********************************************************************************************************/
#define KEY GPIO_PIN_4 /* 按鍵連接的端口 */
/*********************************************************************************************************
PD端口定義
*********************************************************************************************************/
#define PHRA1 GPIO_PIN_0 /* 右側(cè)步進(jìn)電機(jī)的A1相 */
#define PHRA2 GPIO_PIN_1 /* 右側(cè)步進(jìn)電機(jī)的A2相 */
#define PHRB1 GPIO_PIN_2 /* 右側(cè)步進(jìn)電機(jī)的B1相 */
#define PHRB2 GPIO_PIN_3 /* 右側(cè)步進(jìn)電機(jī)的B2相 */
#define PHLA1 GPIO_PIN_4 /* 左側(cè)步進(jìn)電機(jī)的A1相 */
#define PHLA2 GPIO_PIN_5 /* 左側(cè)步進(jìn)電機(jī)的A2相 */
#define PHLB1 GPIO_PIN_6 /* 左側(cè)步進(jìn)電機(jī)的B1相 */
#define PHLB2 GPIO_PIN_7 /* 左側(cè)步進(jìn)電機(jī)的B2相 */
/*********************************************************************************************************
PE端口定義
*********************************************************************************************************/
#define IRSEND_SIDE GPIO_PIN_0 /* 驅(qū)動(dòng)左前右正向紅外發(fā)射 */
/*********************************************************************************************************
常量宏定義 -- 迷宮類型
*********************************************************************************************************/
#define MAZETYPE 8 /* 8: 四分之一迷宮;16: 全迷宮 */
/*********************************************************************************************************
常量宏定義--前進(jìn)一個(gè)迷宮格步進(jìn)電機(jī)需要走的步數(shù)
*********************************************************************************************************/
#define ONEBLOCK 125
/*********************************************************************************************************
常量宏定義--電腦鼠狀態(tài)
*********************************************************************************************************/
#define STOP 0 /* 電腦鼠停止 */
#define GOAHEAD 1 /* 電腦鼠前進(jìn) */
#define TURNLEFT 3 /* 電腦鼠向左轉(zhuǎn) */
#define TURNRIGHT 4 /* 電腦鼠向右轉(zhuǎn) */
#define TURNBACK 5 /* 電腦鼠向后轉(zhuǎn) */
/*********************************************************************************************************
常量宏定義--傳感器
*********************************************************************************************************/
#define LEFT 0 /* 左方傳感器 */
#define FRONTL 1 /* 左前方傳感器 */
#define FRONT 2 /* 前方傳感器 */
#define FRONTR 3 /* 右前方傳感器 */
#define RIGHT 4 /* 右方傳感器 */
/*********************************************************************************************************
常量宏定義--電機(jī)狀態(tài)
*********************************************************************************************************/
#define MOTORSTOP 0 /* 電機(jī)停止 */
#define WAITONESTEP 1 /* 電機(jī)暫停一步 */
#define MOTORRUN 2 /* 電機(jī)運(yùn)行 */
/*********************************************************************************************************
常量宏定義--電機(jī)運(yùn)行方向
*********************************************************************************************************/
#define MOTORGOAHEAD 0 /* 電機(jī)前進(jìn) */
#define MOTORGOBACK 1 /* 電機(jī)后退 */
/*********************************************************************************************************
結(jié)構(gòu)體定義
*********************************************************************************************************/
struct motor {
int8 cState; /* 電機(jī)運(yùn)行狀態(tài) */
int8 cDir; /* 電機(jī)運(yùn)行方向 */
uint32 uiPulse; /* 電機(jī)需要轉(zhuǎn)動(dòng)的步數(shù) */
uint32 uiPulseCtr; /* 電機(jī)已轉(zhuǎn)動(dòng)的步數(shù) */
int32 iSpeed; /* 電機(jī)轉(zhuǎn)動(dòng)速度 */
};
typedef struct motor MOTOR;
/*********************************************************************************************************
定義全局變量
*********************************************************************************************************/
static MOTOR GmRight = {MOTORSTOP, MOTORGOAHEAD, 0, 0, 0}; /* 定義并初始化右電機(jī)狀態(tài) */
static MOTOR GmLeft = {MOTORSTOP, MOTORGOAHEAD, 0, 0, 0}; /* 定義并初始化左電機(jī)狀態(tài) */
static uint8 GucMouseState = STOP; /* 保存電腦鼠當(dāng)前運(yùn)行狀態(tài) */
static uint32 GuiAccelTable[300] = {0}; /* 電機(jī)加減速各階段定時(shí)器值 */
static int32 GiMaxSpeed = 70; /* 保存允許運(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: /* 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: 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)行 */
if (GucMouseState == GOAHEAD) { /* 根據(jù)傳感器狀態(tài)微調(diào)電機(jī)位置 */
if (GucDistance[FRONTL] && (GucDistance[FRONTR] == 0)) { /* 左前方有障礙,且前方無(wú)擋板 */
if (n == 1) {
GmRight.cState = WAITONESTEP; /* 下一次電機(jī)暫停 */
}
n++;
n %= 2;
} else {
n = 0;
}
if ((GucDistance[RIGHT] == 1) && (GucDistance[LEFT] == 0)) {/* 離右擋板太遠(yuǎn),且左方無(wú)擋板 */
if(m == 3) {
GmRight.cState = WAITONESTEP; /* 下一次電機(jī)暫停 */
}
m++;
m %= 6;
} else {
m = 0;
}
}
rightMotorContr(); /* 推動(dòng)電機(jī)轉(zhuǎn)動(dòng)一步 */
break;
default:
break;
}
/*
* 是否完成任務(wù)判斷
*/
if (GmRight.cState != MOTORSTOP) {
GmRight.uiPulseCtr++; /* 運(yùn)行脈沖計(jì)數(shù) */
speedContrR(); /* 速度調(diào)節(jié) */
if (GmRight.uiPulseCtr >= GmRight.uiPulse) {
GmRight.cState = MOTORSTOP;
GmRight.uiPulseCtr = 0;
GmRight.uiPulse = 0;
GmRight.iSpeed = 0;
}
}
}
/*********************************************************************************************************
** Function name: Timer1A_ISR
** Descriptions: Timer1中斷服務(wù)函數(shù)
** input parameters: GmLeft.cState :驅(qū)動(dòng)步進(jìn)電機(jī)的時(shí)序狀態(tài)
** GmLeft.cDir :步進(jìn)電機(jī)運(yùn)動(dòng)的方向
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void Timer1A_ISR(void)
{
static int8 n = 0, m = 0;
TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT); /* 清除定時(shí)器1中斷。 */
switch (GmLeft.cState) {
case MOTORSTOP: /* 停止,同時(shí)清零速度和脈沖值 */
GmLeft.iSpeed = 0;
GmLeft.uiPulse = 0;
GmLeft.uiPulseCtr = 0;
break;
case WAITONESTEP: /* 暫停一步 */
GmLeft.cState = MOTORRUN;
break;
case MOTORRUN: /* 電機(jī)運(yùn)行 */
if (GucMouseState == GOAHEAD) { /* 根據(jù)傳感器狀態(tài)微調(diào)電機(jī)位置 */
if (GucDistance[FRONTR] &&(GucDistance[FRONTL]==0)) {
if (n == 1) {
GmLeft.cState = WAITONESTEP;
}
n++;
n %= 2;
} else {
n = 0;
}
if ((GucDistance[LEFT] == 1) && (GucDistance[RIGHT] == 0)) {
if(m == 3) {
GmLeft.cState = WAITONESTEP;
}
m++;
m %= 6;
} else {
m = 0;
}
}
leftMotorContr(); /* 電機(jī)驅(qū)動(dòng)程序 */
break;
default:
break;
}
/*
* 是否完成任務(wù)判斷
*/
if (GmLeft.cState != MOTORSTOP) {
GmLeft.uiPulseCtr++; /* 運(yùn)行脈沖計(jì)數(shù) */
speedContrL(); /* 速度調(diào)節(jié) */
if (GmLeft.uiPulseCtr >= GmLeft.uiPulse) {
GmLeft.cState = MOTORSTOP;
GmLeft.uiPulseCtr = 0;
GmLeft.uiPulse = 0;
GmLeft.iSpeed = 0;
}
}
}
/*********************************************************************************************************
** Function name: irSendFreq
** Descriptions: 發(fā)送紅外線。
** input parameters: uiFreq: 紅外線調(diào)制頻率
** cNumber: 選擇需要設(shè)置的PWM模塊
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void irSendFreq (uint32 uiFreq, int8 cNumber)
{
uiFreq = SysCtlClockGet() / uiFreq;
switch (cNumber) {
case 1:
PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, uiFreq); /* 設(shè)置PWM發(fā)生器1的周期 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, uiFreq / 2); /* 設(shè)置PWM2輸出的脈沖寬度 */
PWMGenEnable(PWM_BASE, PWM_GEN_1); /* 使能PWM發(fā)生器1 */
break;
case 2:
PWMGenPeriodSet(PWM_BASE, PWM_GEN_2, uiFreq); /* 設(shè)置PWM發(fā)生器2的周期 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_4, uiFreq / 2); /* 設(shè)置PWM4輸出的脈沖寬度 */
PWMGenEnable(PWM_BASE, PWM_GEN_2); /* 使能PWM發(fā)生器2 */
break;
default:
break;
}
}
/*********************************************************************************************************
** Function name: irCheck
** Descriptions: 紅外線傳感器檢測(cè)。
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void irCheck (void)
{
static uint8 ucState = 0;
static uint8 ucIRCheck;
switch (ucState) {
case 0:
irSendFreq(32200, 2); /* 探測(cè)左右兩側(cè)近距 */
irSendFreq(35000, 1); /* 驅(qū)動(dòng)斜角上的傳感器檢測(cè) */
break;
case 1:
ucIRCheck = GPIOPinRead(GPIO_PORTB_BASE, 0x3e); /* 讀取傳感器狀態(tài) */
PWMGenDisable(PWM_BASE, PWM_GEN_2); /* 禁止PWM發(fā)生器2 */
PWMGenDisable(PWM_BASE, PWM_GEN_1); /* 禁止PWM發(fā)生器1 */
if (ucIRCheck & RIGHTSIDE) {
GucDistance[RIGHT] &= 0xfd;
} else {
GucDistance[RIGHT] |= 0x02;
}
if (ucIRCheck & LEFTSIDE) {
GucDistance[LEFT] &= 0xfd;
} else {
GucDistance[LEFT] |= 0x02;
}
if (ucIRCheck & FRONTSIDE_R) {
GucDistance[FRONTR] = 0x00;
} else {
GucDistance[FRONTR] = 0x01;
}
if (ucIRCheck & FRONTSIDE_L) {
GucDistance[FRONTL] = 0x00;
} else {
GucDistance[FRONTL] = 0x01;
}
break;
case 2:
irSendFreq(36000, 2); /* 驅(qū)動(dòng)檢測(cè)左前右三個(gè)方向遠(yuǎn)距 */
break;
case 3:
ucIRCheck = GPIOPinRead(GPIO_PORTB_BASE, 0x2a); /* 讀取傳感器狀態(tài) */
PWMGenDisable(PWM_BASE, PWM_GEN_2); /* 禁止PWM發(fā)生器2 */
break;
case 4:
irSendFreq(36000, 2); /* 重復(fù)檢測(cè)左前右三個(gè)方向遠(yuǎn)距 */
break;
case 5:
ucIRCheck &= GPIOPinRead(GPIO_PORTB_BASE, 0x2a); /* 讀取傳感器狀態(tài) */
PWMGenDisable(PWM_BASE, PWM_GEN_2); /* 禁止PWM發(fā)生器2 */
if (ucIRCheck & RIGHTSIDE) {
GucDistance[RIGHT] &= 0xfe;
} else {
GucDistance[RIGHT] |= 0x01;
}
if (ucIRCheck & LEFTSIDE) {
GucDistance[LEFT] &= 0xfe;
} else {
GucDistance[LEFT] |= 0x01;
}
if (ucIRCheck & FRONTSIDE) {
GucDistance[FRONT] &= 0xfe;
} else {
GucDistance[FRONT] |= 0x01;
}
break;
default:
break;
}
ucState = (ucState + 1) % 6; /* 循環(huán)檢測(cè) */
}
/*********************************************************************************************************
** Function name: mazeSearch
** Descriptions: 迷宮搜索,若發(fā)現(xiàn)分支路或者前方有擋板則退出
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void mazeSearch(void)
{
int8 cL = 0, cR = 0;
/*
* 設(shè)定運(yùn)行任務(wù)
*/
GucMouseState = GOAHEAD;
GmRight.cDir = MOTORGOAHEAD;
GmLeft.cDir = MOTORGOAHEAD;
GmRight.uiPulse = MAZETYPE * ONEBLOCK;
GmLeft.uiPulse = MAZETYPE * ONEBLOCK;
GmRight.cState = MOTORRUN;
GmLeft.cState = MOTORRUN;
while (GmLeft.cState != MOTORSTOP) {
if (GucDistance[FRONT]) { /* 前方有墻 */
/*
* 設(shè)置繼續(xù)前進(jìn)的步數(shù),以實(shí)現(xiàn)能停止在單元格中心的目的
*/
GmRight.uiPulse = GmRight.uiPulseCtr + 70;
GmLeft.uiPulse = GmLeft.uiPulseCtr + 70;
/*
* 反復(fù)檢測(cè)前方傳感器的狀態(tài),以消除誤判
*/
while (GucDistance[FRONT]) {
if ((GmLeft.uiPulseCtr + 50) > GmLeft.uiPulse) {
goto End; /* 跳出循環(huán),跳到程序結(jié)束處 */
}
}
/*
* 程序執(zhí)行到此步表明出現(xiàn)誤判,則重新設(shè)定電機(jī)運(yùn)行任務(wù)
*/
GmRight.uiPulse = MAZETYPE * ONEBLOCK;
GmLeft.uiPulse = MAZETYPE * ONEBLOCK;
}
if (cL) { /* 是否允許檢測(cè)左邊 */
if ((GucDistance[LEFT] & 0x01) == 0) { /* 左邊有支路 */
/*
* 設(shè)置繼續(xù)前進(jìn)的步數(shù),以實(shí)現(xiàn)能停止在單元格中心的目的
*/
GmRight.uiPulse = GmRight.uiPulseCtr + 74;
GmLeft.uiPulse = GmLeft.uiPulseCtr + 74;
/*
* 反復(fù)檢測(cè)前方傳感器的狀態(tài),以消除誤判
*/
while ((GucDistance[LEFT] & 0x01) == 0) {
if ((GmLeft.uiPulseCtr + 50) > GmLeft.uiPulse) {
goto End; /* 跳出循環(huán),跳到程序結(jié)束處 */
}
}
/*
* 程序執(zhí)行到此步表明出現(xiàn)誤判,則重新設(shè)定電機(jī)運(yùn)行任務(wù)
*/
GmRight.uiPulse = MAZETYPE * ONEBLOCK;
GmLeft.uiPulse = MAZETYPE * ONEBLOCK;
}
} else { /* 左邊有墻時(shí)開始允許檢測(cè)左邊 */
if ( GucDistance[LEFT] & 0x01) {
cL = 1;
}
}
if (cR) { /* 是否允許檢測(cè)右邊 */
if ((GucDistance[RIGHT] & 0x01) == 0) { /* 右邊有支路 */
/*
* 設(shè)置繼續(xù)前進(jìn)的步數(shù),以實(shí)現(xiàn)能停止在單元格中心的目的
*/
GmRight.uiPulse = GmRight.uiPulseCtr + 74;
GmLeft.uiPulse = GmLeft.uiPulseCtr + 74;
/*
* 反復(fù)檢測(cè)前方傳感器的狀態(tài),以消除誤判
*/
while ((GucDistance[ RIGHT] & 0x01) == 0) {
if ((GmLeft.uiPulseCtr + 50) > GmLeft.uiPulse) {
goto End; /* 跳出循環(huán),跳到程序結(jié)束處 */
}
}
/*
* 程序執(zhí)行到此步表明出現(xiàn)誤判,則重新設(shè)定電機(jī)運(yùn)行任務(wù)
*/
GmRight.uiPulse = MAZETYPE * ONEBLOCK;
GmLeft.uiPulse = MAZETYPE * ONEBLOCK;
}
} else {
if ( GucDistance[RIGHT] & 0x01) { /* 右邊有墻時(shí)開始允許檢測(cè)右邊 */
cR = 1;
}
}
}
End:;
}
/*********************************************************************************************************
** Function name: mouseTurnright
** Descriptions: 右轉(zhuǎn)
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void mouseTurnright(void)
{
/*
* 等待停止
*/
while (GmLeft.cState != MOTORSTOP);
while (GmRight.cState != MOTORSTOP);
GucMouseState = TURNRIGHT; /* 標(biāo)記電腦鼠的運(yùn)行狀態(tài) */
/*
* 開始右轉(zhuǎn)
*/
GmRight.cDir = MOTORGOBACK; /* 控制右輪向后轉(zhuǎn)動(dòng) */
GmRight.uiPulse = 41; /* 設(shè)定右輪轉(zhuǎn)動(dòng)的步數(shù) */
GmLeft.cDir = MOTORGOAHEAD; /* 控制左輪向前轉(zhuǎn)動(dòng) */
GmLeft.uiPulse = 41; /* 設(shè)定左輪轉(zhuǎn)動(dòng)的步數(shù) */
GmRight.cState = MOTORRUN; /* 使能右輪轉(zhuǎn)動(dòng) */
GmLeft.cState = MOTORRUN; /* 使能左輪轉(zhuǎn)動(dòng) */
/*
* 等待右轉(zhuǎn)完成
*/
while (GmLeft.cState != MOTORSTOP);
while (GmRight.cState != MOTORSTOP);
}
/*********************************************************************************************************
** Function name: mouseTurnleft
** Descriptions: 左轉(zhuǎn)
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void mouseTurnleft(void)
{
/*
* 等待停止
*/
while (GmLeft.cState != MOTORSTOP);
while (GmRight.cState != MOTORSTOP);
GucMouseState = TURNLEFT; /* 標(biāo)記電腦鼠的運(yùn)行狀態(tài) */
/*
* 開始左轉(zhuǎn)
*/
GmRight.cDir = MOTORGOAHEAD; /* 控制右輪向前轉(zhuǎn)動(dòng) */
GmRight.uiPulse = 41; /* 設(shè)定右輪轉(zhuǎn)動(dòng)的步數(shù) */
GmLeft.cDir = MOTORGOBACK; /* 控制左輪向后轉(zhuǎn)動(dòng) */
GmLeft.uiPulse = 41; /* 設(shè)定左輪轉(zhuǎn)動(dòng)的步數(shù) */
GmRight.cState = MOTORRUN; /* 使能右輪轉(zhuǎn)動(dòng) */
GmLeft.cState = MOTORRUN; /* 使能左輪轉(zhuǎn)動(dòng) */
/*
* 等待左轉(zhuǎn)完成
*/
while (GmLeft.cState != MOTORSTOP);
while (GmRight.cState != MOTORSTOP);
}
/*********************************************************************************************************
** Function name: mouseTurnback
** Descriptions: 后轉(zhuǎn)
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void mouseTurnback(void)
{
/*
* 等待停止
*/
while (GmLeft.cState != MOTORSTOP);
while (GmRight.cState != MOTORSTOP);
GucMouseState = TURNBACK;
/*
* 開始后轉(zhuǎn)
*/
GmRight.cDir = MOTORGOBACK;
GmRight.uiPulse = 81;
GmLeft.cDir = MOTORGOAHEAD;
GmLeft.uiPulse = 81;
GmLeft.cState = MOTORRUN;
GmRight.cState = MOTORRUN;
/*
* 等待后轉(zhuǎn)完成
*/
while (GmLeft.cState != MOTORSTOP);
while (GmRight.cState != MOTORSTOP);
}
/*********************************************************************************************************
** Function name: keyCheck
** Descriptions: 讀取按鍵
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: true: 按鍵已按下
** false: 按鍵未按下
*********************************************************************************************************/
uint8 keyCheck (void)
{
if (GPIOPinRead(GPIO_PORTC_BASE, KEY) == 0) {
delay(50);
while(GPIOPinRead(GPIO_PORTC_BASE, KEY) == 0);
return(true);
}else {
return(false);
}
}
/*********************************************************************************************************
** Function name: SysTick_ISR
** Descriptions: 定時(shí)中斷掃描。
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void SysTick_ISR(void)
{
static int32 iL = 0, iR = 0;
/*
* 如果左電機(jī)長(zhǎng)時(shí)間停止,則斷電
*/
if (GmLeft.cState == MOTORSTOP) {
iL++;
} else {
iL = 0;
}
if (iL >= 500) {
GPIOPinWrite(GPIO_PORTD_BASE,
PHLA1 | PHLA2 | PHLB1 | PHLB2,
0x00);
}
/*
* 如果右電機(jī)長(zhǎng)時(shí)間停止,則斷電
*/
if (GmRight.cState == MOTORSTOP) {
iR++;
} else {
iR = 0;
}
if (iR >= 500) {
GPIOPinWrite(GPIO_PORTD_BASE,
PHRA1 | PHRA2 | PHRB1 | PHRB2,
0x00);
}
/*
* 紅外線檢測(cè)
*/
irCheck();
}
/*********************************************************************************************************
** Function name: wallCheck
** Descriptions: 根據(jù)傳感器檢測(cè)結(jié)果判斷是否存在墻壁
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: ucWall: 低三位從左到右一次代表左前右。1為有墻,0為沒(méi)墻。
*********************************************************************************************************/
uint8 wallCheck (void)
{
uint8 ucWall = 0;
if (GucDistance[LEFT] & 0x01) {
ucWall |= 0x04;
}else {
ucWall &= ~0x04;
}
if (GucDistance[FRONT] & 0x01) {
ucWall |= 0x02;
}else {
ucWall &= ~0x02;
}
if (GucDistance[RIGHT] & 0x01) {
ucWall |= 0x01;
}else {
ucWall &= ~0x01;
}
return(ucWall);
}
/*********************************************************************************************************
** Function name: voltageDetect
** Descriptions: 電壓檢測(cè),檢測(cè)結(jié)果在7289 EX BOARD 上顯示出來(lái)
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void voltageDetect (void)
{
unsigned long ulVoltage;
ADCProcessorTrigger(ADC_BASE, 0); /* 處理器觸發(fā)一次A/D轉(zhuǎn)換 */
while (!ADCIntStatus(ADC_BASE, 0, false)); /* 等待轉(zhuǎn)換結(jié)束 */
ADCIntClear(ADC_BASE, 0); /* 清除中斷標(biāo)準(zhǔn)位 */
ADCSequenceDataGet(ADC_BASE, 0, &ulVoltage); /* 讀取轉(zhuǎn)換結(jié)果 */
ulVoltage = ulVoltage * 3000 / 1023; /* 計(jì)算實(shí)際檢測(cè)到的電壓值(mV) */
ulVoltage = ulVoltage * 3 + 350; /* 計(jì)算電池電壓值(mV) */
zlg7289Download(0,6,1,(ulVoltage % 10000) / 1000); /* 顯示電壓值整數(shù)部分,單位V */
zlg7289Download(0,7,0,(ulVoltage % 1000 ) / 100 ); /* 顯示電壓值小數(shù)部分,單位V */
}
/*********************************************************************************************************
** Function name: sensorInit
** Descriptions: 傳感器控制初始化
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void sensorInit (void)
{
/*
* 設(shè)置連接到傳感器信號(hào)輸出腳的I/O口為輸入模式
*/
GPIODirModeSet(GPIO_PORTB_BASE,
LEFTSIDE |
FRONTSIDE_L |
FRONTSIDE |
FRONTSIDE_R |
RIGHTSIDE,
GPIO_DIR_MODE_IN);
/*
* 用PWM驅(qū)動(dòng)紅外線發(fā)射頭產(chǎn)生調(diào)制的紅外線信號(hào)
*/
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM); /* 使能PWM模塊 */
SysCtlPWMClockSet(SYSCTL_PWMDIV_1); /* PWM時(shí)鐘配置:不分頻 */
/*
* 初始化PWM2,該P(yáng)WM驅(qū)動(dòng)斜角紅外發(fā)射頭
*/
GPIOPinTypePWM(GPIO_PORTB_BASE, IRSEND_BEVEL); /* PB0配置為PWM功能 */
PWMGenConfigure(PWM_BASE, PWM_GEN_1, /* 配置PWM發(fā)生器1 */
PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); /* 加計(jì)數(shù),立即更新 */
PWMOutputState(PWM_BASE, PWM_OUT_2_BIT, true); /* 使能PWM2輸出 */
PWMGenDisable(PWM_BASE, PWM_GEN_1); /* 禁止PWM發(fā)生器1 */
/*
* 初始化PWM4,該P(yáng)WM驅(qū)動(dòng)左前右正方向紅外發(fā)射頭
*/
GPIOPinTypePWM(GPIO_PORTE_BASE, IRSEND_SIDE); /* PE0配置為PWM功能 */
PWMGenConfigure(PWM_BASE, PWM_GEN_2, /* 配置PWM發(fā)生器2 */
PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC); /* 加計(jì)數(shù),立即更新 */
PWMOutputState(PWM_BASE, PWM_OUT_4_BIT, true); /* 使能PWM4輸出 */
PWMGenDisable(PWM_BASE, PWM_GEN_2); /* 禁止PWM發(fā)生器2 */
}
/*********************************************************************************************************
** Function name: stepMotorIint
** Descriptions: 步進(jìn)電機(jī)控制初始化
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void stepMotorIint (void)
{
uint16 n = 0;
/*
* 設(shè)置驅(qū)動(dòng)步進(jìn)電機(jī)的八個(gè)I/O口為輸出模式
*/
GPIODirModeSet(GPIO_PORTD_BASE,
PHRA1 |
PHRA2 |
PHRB1 |
PHRB2 |
PHLA1 |
PHLA2 |
PHLB1 |
PHLB2,
GPIO_DIR_MODE_OUT);
/*
* 對(duì)左右電機(jī)轉(zhuǎn)動(dòng)的位置初始化
*/
GPIOPinWrite(GPIO_PORTD_BASE,
PHRA1 | PHRA2 | PHRB1 | PHRB2,
PHRA1 | PHRA2 | PHRB1 | PHRB2);
GPIOPinWrite(GPIO_PORTD_BASE,
PHLA1 | PHLA2 | PHLB1 | PHLB2,
PHLA1 | PHLA2 | PHLB1 | PHLB2);
/*
* 初始化加速/減速時(shí)定時(shí)器加載值的數(shù)據(jù)表
*/
GuiAccelTable[0] = 2236068;
GuiAccelTable[1] = 926179;
for(n = 2; n < 300; n++) {
GuiAccelTable[n] = GuiAccelTable[n - 1] - (2 * GuiAccelTable[n - 1] / (4 * n + 1));
}
/*
* 初始化定時(shí)器0,用來(lái)控制右電機(jī)的轉(zhuǎn)速
*/
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); /* 使能定時(shí)器0模塊 */
TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER); /* 配置為32位周期計(jì)數(shù)模式 */
TimerLoadSet(TIMER0_BASE, TIMER_A, GuiAccelTable[0]); /* 設(shè)置定時(shí)時(shí)間 */
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); /* 設(shè)置為溢出中斷 */
IntEnable(INT_TIMER0A); /* 使能定時(shí)器0中斷 */
TimerEnable(TIMER0_BASE, TIMER_A); /* 使能定時(shí)器0 */
/*
* 初始化定時(shí)器1,用來(lái)控制電機(jī)的轉(zhuǎn)速
*/
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); /* 使能定時(shí)器1模塊 */
TimerConfigure(TIMER1_BASE, TIMER_CFG_32_BIT_PER); /* 配置為32位周期計(jì)數(shù)模式 */
TimerLoadSet(TIMER1_BASE, TIMER_A, GuiAccelTable[0]); /* 設(shè)置定時(shí)時(shí)間 */
TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT); /* 設(shè)置為溢出中斷 */
IntEnable(INT_TIMER1A); /* 使能定時(shí)器1中斷 */
TimerEnable(TIMER1_BASE, TIMER_A); /* 使能定時(shí)器1 */
}
/*********************************************************************************************************
** Function name: keyInit
** Descriptions: 對(duì)連接按鍵的GPIO口初始化
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void keyInit (void)
{
GPIODirModeSet(GPIO_PORTC_BASE, KEY, GPIO_DIR_MODE_IN); /* 設(shè)置按鍵口為輸入 */
}
/*********************************************************************************************************
** Function name: sysTickInit
** Descriptions: 系統(tǒng)節(jié)拍定時(shí)器初始化。
** input parameters: 無(wú)
** output parameters: 無(wú)
** Returned value: 無(wú)
*********************************************************************************************************/
void sysTickInit (void)
{
SysTickPeriodSet(SysCtlClockGet() / 1600); /* 設(shè)置定時(shí)時(shí)鐘為625us */
SysTickEnable(); /* 使能系統(tǒng)時(shí)鐘 */
SysTickIntEnable(); /* 使能系統(tǒng)時(shí)鐘中斷 */
……………………
…………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
電腦鼠程序.rar
(74.78 KB, 下載次數(shù): 35)
2018-4-2 21:36 上傳
點(diǎn)擊文件名下載附件
電腦鼠程序
下載積分: 黑幣 -5
作者:
皇叔
時(shí)間:
2019-5-16 21:09
感謝樓主的分享,最近正好需要
作者:
micdot
時(shí)間:
2019-11-29 11:06
非常不錯(cuò)的東東,感謝樓主的分享!
歡迎光臨 (http://www.denmoz.com/bbs/)
Powered by Discuz! X3.1