⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 34

📁 电脑鼠走迷宫程序
💻
📖 第 1 页 / 共 4 页
字号:
        if (__GucMouseState == __GOAHEAD) {                             /*  根据传感器状态微调电机位置  */
            if (__GucDistance[__FRONTL] && (__GucDistance[__FRONTR] == 0)) {
                if (n == 1) {
                    __GmRight.cState = __WAITONESTEP;
                }
                n++;
                n %= 2;
            } else {
                n = 0;
            }
            
            if ((__GucDistance[__RIGHT] == 1) && (__GucDistance[__LEFT] == 0)) {
                if(m == 3) {
                    __GmRight.cState = __WAITONESTEP;
                }
                m++;
                m %= 6;
            } else {
                m  = 0;
            }
        }
        __rightMotorContr();                                            /*  电机驱动程序                */
        break;

    default:
        break;
    }
    /*
     *  是否完成任务判断
     */
    if (__GmRight.cState != __MOTORSTOP) {
        __GmRight.uiPulseCtr++;                                         /*  运行脉冲计数                */
        __speedContrR();                                                /*  速度调节                    */
        if (__GmRight.uiPulseCtr >= __GmRight.uiPulse) {
            __GmRight.cState      = __MOTORSTOP;
            __GmRight.uiPulseCtr  = 0;
            __GmRight.uiPulse     = 0;
            __GmRight.iSpeed      = 0;
        }
    }
}


/*********************************************************************************************************
** Function name:       Timer1A_ISR
** Descriptions:        Timer1中断服务函数
** input parameters:    __GmLeft.cState :驱动步进电机的时序状态
**                      __GmLeft.cDir   :步进电机运动的方向
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void Timer1A_ISR(void)
{
    static int8 n = 0, m = 0;
    
    TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);                     /*  清除定时器1中断。           */
    switch (__GmLeft.cState) {
        
    case __MOTORSTOP:                                                   /*  停止,同时清零速度和脉冲值  */
        __GmLeft.iSpeed     = 0;
        __GmLeft.uiPulse    = 0;
        __GmLeft.uiPulseCtr = 0;
        break;
        
    case __WAITONESTEP:                                                 /*  暂停一步                    */
        __GmLeft.cState     = __MOTORRUN;
        break;

    case __MOTORRUN:                                                    /*  电机运行                    */
        if (__GucMouseState == __GOAHEAD) {                             /*  根据传感器状态微调电机位置  */
            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();                                             /*  电机驱动程序                */
        break;

    default:
        break;
    }
    /*
     *  是否完成任务判断
     */
    if (__GmLeft.cState != __MOTORSTOP) {
        __GmLeft.uiPulseCtr++;                                          /*  运行脉冲计数                */
        __speedContrL();                                                /*  速度调节                    */
        if (__GmLeft.uiPulseCtr >= __GmLeft.uiPulse) {
            __GmLeft.cState      = __MOTORSTOP;
            __GmLeft.uiPulseCtr  = 0;
            __GmLeft.uiPulse     = 0;
            __GmLeft.iSpeed      = 0;
        }
    }
}


/*********************************************************************************************************
** Function name:       mazeSearch
** Descriptions:        前进N格
** input parameters:    iNblock: 前进的格数
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void mouseGoahead (int8  cNBlock)
{
    int8 cL = 0, cR = 0, cCoor = 1;
    if (__GmLeft.cState) {
        cCoor = 0;
    }
    /*
     *  设定运行任务
     */
    __GucMouseState   = __GOAHEAD;
    __GiMaxSpeed      =   MAXSPEED;
    __GmRight.cDir    = __MOTORGOAHEAD;
    __GmLeft.cDir     = __MOTORGOAHEAD;
    __GmRight.uiPulse = __GmRight.uiPulse + cNBlock * ONEBLOCK - 6;
    __GmLeft.uiPulse  = __GmLeft.uiPulse  + cNBlock * ONEBLOCK - 6;
    __GmRight.cState  = __MOTORRUN;
    __GmLeft.cState   = __MOTORRUN;
    
    while (__GmLeft.cState != __MOTORSTOP) {
        if (__GmLeft.uiPulseCtr >= ONEBLOCK) {                          /*  判断是否走完一格            */
            __GmLeft.uiPulse    -= ONEBLOCK;
            __GmLeft.uiPulseCtr -= ONEBLOCK;
            if (cCoor) {
                cNBlock--;
                __mouseCoorUpdate();                                    /*  更新坐标                    */
            } else {
                cCoor = 1;
            }
        }
        if (__GmRight.uiPulseCtr >= ONEBLOCK) {                         /*  判断是否走完一格            */
            __GmRight.uiPulse    -= ONEBLOCK;
            __GmRight.uiPulseCtr -= ONEBLOCK;
        }
        if (__GucDistance[__FRONT]) {                                   /*  前方有墙,则跳出程序        */
            __GmRight.uiPulse = __GmRight.uiPulseCtr + 70;
            __GmLeft.uiPulse  = __GmLeft.uiPulseCtr  + 70;
            while (__GucDistance[ __FRONT]) {
                if ((__GmLeft.uiPulseCtr + 20) > __GmLeft.uiPulse) {
                    goto End;
                }
            }
        }
        if (cNBlock < 2) {
            if (cL) {                                                   /*  是否允许检测左边            */
                if ((__GucDistance[ __LEFT] & 0x01) == 0) {             /*  左边有支路,跳出程序        */
                    __GmRight.uiPulse = __GmRight.uiPulseCtr + 74;
                    __GmLeft.uiPulse  = __GmLeft.uiPulseCtr  + 74;
                    while ((__GucDistance[ __LEFT] & 0x01) == 0) {
                        if ((__GmLeft.uiPulseCtr + 20) > __GmLeft.uiPulse) {
                            goto End;
                        }
                    }
                }
            } else {                                                    /*  左边有墙时开始允许检测左边  */
                if ( __GucDistance[ __LEFT] & 0x01) {
                    cL = 1;
                }
            }
            if (cR) {                                                   /*  是否允许检测右边            */
                if ((__GucDistance[__RIGHT] & 0x01) == 0) {             /*  右边有支路,跳出程序        */
                    __GmRight.uiPulse = __GmRight.uiPulseCtr + 74;
                    __GmLeft.uiPulse  = __GmLeft.uiPulseCtr  + 74;
                    while ((__GucDistance[ __RIGHT] & 0x01) == 0) {
                        if ((__GmLeft.uiPulseCtr + 20) > __GmLeft.uiPulse) {
                            goto End;
                        }
                    }
                }
            } else {
                if ( __GucDistance[__RIGHT] & 0x01) {                   /*  右边有墙时开始允许检测右边  */
                    cR = 1;
                }
            }
        }
    }
    /*
     *  设定运行任务,让电脑鼠走到支路的中心位置
     */
End:    __mouseCoorUpdate();                                            /*  更新坐标                    */
}

/*********************************************************************************************************
** Function name:       mazeSearch
** Descriptions:        前进N格
** input parameters:    iNblock: 前进的格数
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void mazeSearch(void)
{
    int8 cL = 0, cR = 0, cCoor = 1;
    if (__GmLeft.cState) {
        cCoor = 0;
    }
    /*
     *  设定运行任务
     */
    __GucMouseState   = __GOAHEAD;
    __GiMaxSpeed      =   SEARCHSPEED;
    __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 (__GmLeft.uiPulseCtr >= ONEBLOCK) {                          /*  判断是否走完一格            */
            __GmLeft.uiPulse    -= ONEBLOCK;
            __GmLeft.uiPulseCtr -= ONEBLOCK;
            if (cCoor) {
                __mouseCoorUpdate();                                    /*  更新坐标                    */
            } else {
                cCoor = 1;
            }
        }
        if (__GmRight.uiPulseCtr >= ONEBLOCK) {                         /*  判断是否走完一格            */
            __GmRight.uiPulse    -= ONEBLOCK;
            __GmRight.uiPulseCtr -= ONEBLOCK;
        }
        if (__GucDistance[__FRONT]) {                                   /*  前方有墙,则跳出程序        */
            __GmRight.uiPulse = __GmRight.uiPulseCtr + 70;
            __GmLeft.uiPulse  = __GmLeft.uiPulseCtr  + 70;
            while (__GucDistance[ __FRONT]) {
                if ((__GmLeft.uiPulseCtr + 20) > __GmLeft.uiPulse) {
                    goto End;
                }
            }
            __GmRight.uiPulse = MAZETYPE * ONEBLOCK;
            __GmLeft.uiPulse  = MAZETYPE * ONEBLOCK;
        }
        if (cL) {                                                       /*  是否允许检测左边            */
            if ((__GucDistance[ __LEFT] & 0x01) == 0) {                 /*  左边有支路,跳出程序        */
                __GmRight.uiPulse = __GmRight.uiPulseCtr + 74;
                __GmLeft.uiPulse  = __GmLeft.uiPulseCtr  + 74;
                while ((__GucDistance[ __LEFT] & 0x01) == 0) {
                    if ((__GmLeft.uiPulseCtr + 20) > __GmLeft.uiPulse) {
                        goto End;
                    }
                }
                __GmRight.uiPulse = MAZETYPE * ONEBLOCK;
                __GmLeft.uiPulse  = MAZETYPE * ONEBLOCK;
            }
        } else {                                                        /*  左边有墙时开始允许检测左边  */
            if ( __GucDistance[ __LEFT] & 0x01) {
                cL = 1;
            }
        }
        if (cR) {                                                       /*  是否允许检测右边            */
            if ((__GucDistance[__RIGHT] & 0x01) == 0) {                 /*  右边有支路,跳出程序        */
                __GmRight.uiPulse = __GmRight.uiPulseCtr + 74;
                __GmLeft.uiPulse  = __GmLeft.uiPulseCtr  + 74;
                while ((__GucDistance[ __RIGHT] & 0x01) == 0) {
                    if ((__GmLeft.uiPulseCtr + 20) > __GmLeft.uiPulse) {
                        goto End;
                    }
                }
                __GmRight.uiPulse = MAZETYPE * ONEBLOCK;
                __GmLeft.uiPulse  = MAZETYPE * ONEBLOCK;
            }
        } else {
            if ( __GucDistance[__RIGHT] & 0x01) {                       /*  右边有墙时开始允许检测右边  */
                cR = 1;
            }
        }
    }
End:    __mouseCoorUpdate();                                            /*  更新坐标                    */
}


/*********************************************************************************************************
** Function name:       SysTick_ISR
** Descriptions:        定时中断扫描。
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void SysTick_ISR(void)
{
    static int32 iL = 0, iR = 0;
    
    /*
     *  如果左电机长时间停止,则断电
     */
    if (__GmLeft.cState == __MOTORSTOP) {
        iL++;
    } else {
        iL = 0;
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -