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

📄 main.c

📁 电子鼠走迷宫无记忆源码----------------
💻 C
📖 第 1 页 / 共 4 页
字号:
            GucDistance[FRONTR]  = 0x01;
        }
        if (ucIRCheck & FRONTSIDE_L) {
            GucDistance[FRONTL]  = 0x00;
        } else {
            GucDistance[FRONTL]  = 0x01;
        }
        break;

    case 2:
        irSendFreq(36000, 2);                                           /*  驱动检测左前右三个方向远距  */
        break;
        
    case 3:
        ucIRCheck = GPIOPinRead(GPIO_PORTB_BASE, 0x2a);                 /*  读取传感器状态              */
        PWMGenDisable(PWM_BASE, PWM_GEN_2);                             /*  禁止PWM发生器2              */
        break;

    case 4:
        irSendFreq(36000, 2);                                           /*  重复检测左前右三个方向远距  */
        break;
        
    case 5:
        ucIRCheck &= GPIOPinRead(GPIO_PORTB_BASE, 0x2a);                /*  读取传感器状态              */
        PWMGenDisable(PWM_BASE, PWM_GEN_2);                             /*  禁止PWM发生器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;                                        /*  循环检测                    */
}


/*********************************************************************************************************
** Function name:       mazeSearch
** Descriptions:        迷宫搜索,若发现分支路或者前方有挡板则退出
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void mazeSearch(void)
{
    int8 cL = 0, cR = 0;
    /*
     *  设定运行任务
     */
    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]) {                                       /*  前方有墙                    */
            /*
             *  设置继续前进的步数,以实现能停止在单元格中心的目的
             */
            GmRight.uiPulse = GmRight.uiPulseCtr + 70;
            GmLeft.uiPulse  = GmLeft.uiPulseCtr  + 70;
            /*
             *  反复检测前方传感器的状态,以消除误判
             */
            while (GucDistance[FRONT]) {
                if ((GmLeft.uiPulseCtr + 50) > 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 + 50) > 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 + 50) > GmLeft.uiPulse) {
                        goto End;                                       /*  跳出循环,跳到程序结束处    */
                    }
                }
                /*
                 *  程序执行到此步表明出现误判,则重新设定电机运行任务
                 */
                GmRight.uiPulse = MAZETYPE * ONEBLOCK;
                GmLeft.uiPulse  = MAZETYPE * ONEBLOCK;
            }
        } else {
            if ( GucDistance[RIGHT] & 0x01) {                           /*  右边有墙时开始允许检测右边  */
                cR = 1;
            }
        }
    }
End:;
}


/*********************************************************************************************************
** Function name:       mouseTurnright
** Descriptions:        右转
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void mouseTurnright(void)
{
    /*
     *  等待停止
     */
    while (GmLeft.cState  != MOTORSTOP);
    while (GmRight.cState != MOTORSTOP);
    
    GucMouseState   = TURNRIGHT;                                        /*  标记电脑鼠的运行状态        */
    /*
     *  开始右转
     */
    GmRight.cDir    = MOTORGOBACK;                                      /*  控制右轮向后转动            */
    GmRight.uiPulse = 41;                                               /*  设定右轮转动的步数          */
    
    GmLeft.cDir     = MOTORGOAHEAD;                                     /*  控制左轮向前转动            */
    GmLeft.uiPulse  = 41;                                               /*  设定左轮转动的步数          */
    
    GmRight.cState  = MOTORRUN;                                         /*  使能右轮转动                */
    GmLeft.cState   = MOTORRUN;                                         /*  使能左轮转动                */
    /*
     *  等待右转完成
     */
    while (GmLeft.cState  != MOTORSTOP);
    while (GmRight.cState != MOTORSTOP);
}


/*********************************************************************************************************
** Function name:       mouseTurnleft
** Descriptions:        左转
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void mouseTurnleft(void)
{
    /*
     *  等待停止
     */
    while (GmLeft.cState  != MOTORSTOP);
    while (GmRight.cState != MOTORSTOP);
    
    GucMouseState   = TURNLEFT;                                         /*  标记电脑鼠的运行状态        */
    /*
     *  开始左转
     */
    GmRight.cDir    = MOTORGOAHEAD;                                     /*  控制右轮向前转动            */
    GmRight.uiPulse = 41;                                               /*  设定右轮转动的步数          */
    
    GmLeft.cDir     = MOTORGOBACK;                                      /*  控制左轮向后转动            */
    GmLeft.uiPulse  = 41;                                               /*  设定左轮转动的步数          */
    
    GmRight.cState  = MOTORRUN;                                         /*  使能右轮转动                */
    GmLeft.cState   = MOTORRUN;                                         /*  使能左轮转动                */
    /*
     *  等待左转完成
     */
    while (GmLeft.cState  != MOTORSTOP);
    while (GmRight.cState != MOTORSTOP);
}


/*********************************************************************************************************
** Function name:       mouseTurnback
** Descriptions:        后转
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void mouseTurnback(void)
{
    /*
     *  等待停止
     */
    while (GmLeft.cState  != MOTORSTOP);
    while (GmRight.cState != MOTORSTOP);
    
    GucMouseState   = TURNBACK;
    /*
     *  开始后转
     */
    GmRight.cDir    = MOTORGOBACK;
    GmRight.uiPulse = 81;
    
    GmLeft.cDir     = MOTORGOAHEAD;
    GmLeft.uiPulse  = 81;
    
    GmLeft.cState   = MOTORRUN;
    GmRight.cState  = MOTORRUN;
    /*
     *  等待后转完成
     */
    while (GmLeft.cState  != MOTORSTOP);
    while (GmRight.cState != MOTORSTOP);
}


/*********************************************************************************************************
** Function name:       keyCheck
** Descriptions:        读取按键
** input parameters:    无
** output parameters:   无
** 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:        定时中断扫描。
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void SysTick_ISR(void)
{
    static int32 iL = 0, iR = 0;
    
    /*
     *  如果左电机长时间停止,则断电
     */
    if (GmLeft.cState == MOTORSTOP) {
        iL++;
    } else {

⌨️ 快捷键说明

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